2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 /** @file src/transcode_job.cc
23 * @brief A job which transcodes from one format to another.
27 #include "analytics.h"
28 #include "compose.hpp"
31 #include "dcp_encoder.h"
32 #include "dcpomatic_log.h"
34 #include "examine_content_job.h"
36 #include "job_manager.h"
38 #include "transcode_job.h"
39 #include "upload_job.h"
48 using std::make_shared;
49 using std::setprecision;
50 using std::shared_ptr;
52 using boost::optional;
53 using std::dynamic_pointer_cast;
56 /** @param film Film to use */
57 TranscodeJob::TranscodeJob (shared_ptr<const Film> film, ChangedBehaviour changed)
65 TranscodeJob::~TranscodeJob ()
72 TranscodeJob::name () const
74 return String::compose (_("Transcoding %1"), _film->name());
79 TranscodeJob::json_name () const
81 return N_("transcode");
86 TranscodeJob::set_encoder (shared_ptr<Encoder> e)
96 auto content = _film->content();
97 std::vector<shared_ptr<Content>> changed;
98 std::copy_if (content.begin(), content.end(), std::back_inserter(changed), [](shared_ptr<Content> c) { return c->changed(); });
100 if (!changed.empty()) {
102 case ChangedBehaviour::EXAMINE_THEN_STOP:
103 for (auto i: changed) {
104 JobManager::instance()->add(make_shared<ExamineContentJob>(_film, i));
107 set_message (_("Some files have been changed since they were added to the project.\n\nThese files will now be re-examined, so you may need to check their settings before trying again."));
108 set_error (_("Files have changed since they were added to the project."), _("Check their new settings, then try again."));
109 set_state (FINISHED_ERROR);
111 case ChangedBehaviour::STOP:
113 set_error (_("Files have changed since they were added to the project."), _("Open the project in DCP-o-matic, check the settings, then save it before trying again."));
114 set_state (FINISHED_ERROR);
117 LOG_GENERAL_NC (_("Some files have been changed since they were added to the project."));
122 struct timeval start;
123 gettimeofday (&start, 0);
124 LOG_GENERAL_NC (N_("Transcode job starting"));
126 DCPOMATIC_ASSERT (_encoder);
129 struct timeval finish;
130 gettimeofday (&finish, 0);
133 if (finish.tv_sec != start.tv_sec) {
134 fps = _encoder->frames_done() / (finish.tv_sec - start.tv_sec);
137 LOG_GENERAL (N_("Transcode job completed successfully: %1 fps"), dcp::locale_convert<string>(fps, 2, true));
139 if (dynamic_pointer_cast<DCPEncoder>(_encoder)) {
141 Analytics::instance()->successful_dcp_encode();
142 } catch (FileError& e) {
143 LOG_WARNING (N_("Failed to write analytics (%1)"), e.what());
147 /* XXX: this shouldn't be here */
148 if (Config::instance()->upload_after_make_dcp() && dynamic_pointer_cast<DCPEncoder>(_encoder)) {
149 JobManager::instance()->add(make_shared<UploadJob>(_film));
155 set_state (FINISHED_OK);
165 TranscodeJob::status () const
168 return Job::status ();
171 if (finished() || _encoder->finishing()) {
172 return Job::status();
175 auto status = String::compose(_("%1; %2/%3 frames"), Job::status(), _encoder->frames_done(), _film->length().frames_round(_film->video_frame_rate()));
176 if (auto const fps = _encoder->current_rate()) {
177 /// TRANSLATORS: fps here is an abbreviation for frames per second
178 status += String::compose(_("; %1 fps"), dcp::locale_convert<string>(*fps, 1, true));
185 /** @return Approximate remaining time in seconds */
187 TranscodeJob::remaining_time () const
189 /* _encoder might be destroyed by the job-runner thread */
192 if (!e || e->finishing()) {
193 /* We aren't doing any actual encoding so just use the job's guess */
194 return Job::remaining_time ();
197 /* We're encoding so guess based on the current encoding rate */
199 auto fps = e->current_rate ();
205 /* Compute approximate proposed length here, as it's only here that we need it */
206 return (_film->length().frames_round(_film->video_frame_rate()) - e->frames_done()) / *fps;