Add fps count to the log on transcode finish (#786).
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/transcode_job.cc
21  *  @brief A job which transcodes from one format to another.
22  */
23
24 #include "transcode_job.h"
25 #include "film.h"
26 #include "transcoder.h"
27 #include "log.h"
28 #include "safe_stringstream.h"
29 #include "compose.hpp"
30 #include <iostream>
31 #include <iomanip>
32
33 #include "i18n.h"
34
35 #define LOG_GENERAL(...) _film->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_GENERAL);
36 #define LOG_GENERAL_NC(...) _film->log()->log (__VA_ARGS__, LogEntry::TYPE_GENERAL);
37 #define LOG_ERROR_NC(...)   _film->log()->log (__VA_ARGS__, LogEntry::TYPE_ERROR);
38
39 using std::string;
40 using std::fixed;
41 using std::setprecision;
42 using std::cout;
43 using boost::shared_ptr;
44
45 /** @param s Film to use.
46  */
47 TranscodeJob::TranscodeJob (shared_ptr<const Film> film)
48         : Job (film)
49 {
50
51 }
52
53 string
54 TranscodeJob::name () const
55 {
56         return String::compose (_("Transcode %1"), _film->name());
57 }
58
59 string
60 TranscodeJob::json_name () const
61 {
62         return N_("transcode");
63 }
64
65 void
66 TranscodeJob::run ()
67 {
68         try {
69                 struct timeval start;
70                 gettimeofday (&start, 0);
71                 LOG_GENERAL_NC (N_("Transcode job starting"));
72
73                 _transcoder.reset (new Transcoder (_film, shared_from_this ()));
74                 _transcoder->go ();
75                 set_progress (1);
76                 set_state (FINISHED_OK);
77
78                 struct timeval finish;
79                 gettimeofday (&finish, 0);
80
81                 float fps = 0;
82                 if (finish.tv_sec != start.tv_sec) {
83                         fps = _transcoder->video_frames_out() / (finish.tv_sec - start.tv_sec);
84                 }
85
86                 LOG_GENERAL (N_("Transcode job completed successfully: %1 fps"), fps);
87                 _transcoder.reset ();
88
89         } catch (...) {
90                 _transcoder.reset ();
91                 throw;
92         }
93 }
94
95 string
96 TranscodeJob::status () const
97 {
98         if (!_transcoder) {
99                 return Job::status ();
100         }
101
102         float const fps = _transcoder->current_encoding_rate ();
103         if (fps == 0) {
104                 return Job::status ();
105         }
106
107         SafeStringStream s;
108
109         s << Job::status ();
110
111         if (!finished () && !_transcoder->finishing ()) {
112                 /// TRANSLATORS: fps here is an abbreviation for frames per second
113                 s << "; " << _transcoder->video_frames_out() << "/"
114                   << _film->length().frames_round (_film->video_frame_rate ()) << " " << _("frames") << "; "
115                   << fixed << setprecision (1) << fps << " " << _("fps");
116         }
117
118         return s.str ();
119 }
120
121 /** @return Approximate remaining time in seconds */
122 int
123 TranscodeJob::remaining_time () const
124 {
125         /* _transcoder might be destroyed by the job-runner thread */
126         shared_ptr<Transcoder> t = _transcoder;
127
128         if (!t) {
129                 return 0;
130         }
131
132         float fps = t->current_encoding_rate ();
133
134         if (fps == 0) {
135                 return 0;
136         }
137
138         /* Compute approximate proposed length here, as it's only here that we need it */
139         return (_film->length().frames_round (_film->video_frame_rate ()) - t->video_frames_out()) / fps;
140 }