Merge master.
[dcpomatic.git] / src / lib / transcode_job.cc
1 /*
2     Copyright (C) 2012 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 <iostream>
25 #include <iomanip>
26 #include "transcode_job.h"
27 #include "film.h"
28 #include "transcoder.h"
29 #include "log.h"
30
31 #include "i18n.h"
32
33 using std::string;
34 using std::stringstream;
35 using std::fixed;
36 using std::setprecision;
37 using boost::shared_ptr;
38
39 /** @param s Film to use.
40  */
41 TranscodeJob::TranscodeJob (shared_ptr<const Film> f)
42         : Job (f)
43 {
44         
45 }
46
47 string
48 TranscodeJob::name () const
49 {
50         return String::compose (_("Transcode %1"), _film->name());
51 }
52
53 string
54 TranscodeJob::json_name () const
55 {
56         return N_("transcode");
57 }
58
59 void
60 TranscodeJob::run ()
61 {
62         try {
63
64                 _film->log()->log (N_("Transcode job starting"));
65
66                 _transcoder.reset (new Transcoder (_film, shared_from_this ()));
67                 _transcoder->go ();
68                 set_progress (1);
69                 set_state (FINISHED_OK);
70
71                 _film->log()->log (N_("Transcode job completed successfully"));
72                 _transcoder.reset ();
73
74         } catch (...) {
75                 set_progress (1);
76                 set_state (FINISHED_ERROR);
77                 _film->log()->log (N_("Transcode job failed or cancelled"));
78                 _transcoder.reset ();
79                 throw;
80         }
81 }
82
83 string
84 TranscodeJob::status () const
85 {
86         if (!_transcoder) {
87                 return Job::status ();
88         }
89
90         float const fps = _transcoder->current_encoding_rate ();
91         if (fps == 0) {
92                 return Job::status ();
93         }
94
95         stringstream s;
96
97         s << Job::status ();
98
99         if (!finished () && !_transcoder->finishing ()) {
100                 s << "; " << fixed << setprecision (1) << fps << " " << _("frames per second");
101         }
102         
103         return s.str ();
104 }
105
106 /** @return Approximate remaining time in seconds */
107 int
108 TranscodeJob::remaining_time () const
109 {
110         /* _transcoder might be destroyed by the job-runner thread */
111         shared_ptr<Transcoder> t = _transcoder;
112         
113         if (!t) {
114                 return 0;
115         }
116         
117         float fps = t->current_encoding_rate ();
118
119         if (fps == 0) {
120                 return 0;
121         }
122
123         /* Compute approximate proposed length here, as it's only here that we need it */
124         return (_film->length().frames (_film->video_frame_rate ()) - t->video_frames_out()) / fps;
125 }