2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/lexical_cast.hpp>
33 #include "imagemagick_encoder.h"
36 #include "transcoder.h"
38 #include "job_manager.h"
39 #include "ab_transcode_job.h"
40 #include "transcode_job.h"
41 #include "scp_dcp_job.h"
42 #include "copy_from_dvd_job.h"
43 #include "make_dcp_job.h"
44 #include "film_state.h"
47 #include "exceptions.h"
48 #include "examine_content_job.h"
50 #include "decoder_factory.h"
52 #include "check_hashes_job.h"
56 using namespace boost;
58 /** Construct a Film object in a given directory, reading any metadata
59 * file that exists in that directory. An exception will be thrown if
60 * must_exist is true, and the specified directory does not exist.
62 * @param d Film directory.
63 * @param must_exist true to throw an exception if does not exist.
66 Film::Film (string d, bool must_exist)
68 /* Make state.directory a complete path without ..s (where possible)
69 (Code swiped from Adam Bowen on stackoverflow)
72 filesystem::path p (filesystem::system_complete (d));
73 filesystem::path result;
74 for (filesystem::path::iterator i = p.begin(); i != p.end(); ++i) {
76 if (filesystem::is_symlink (result) || result.filename() == "..") {
79 result = result.parent_path ();
81 } else if (*i != ".") {
86 set_directory (result.string ());
88 if (!filesystem::exists (directory())) {
90 throw OpenFileError (directory());
92 filesystem::create_directory (directory());
98 _log = new FileLog (file ("log"));
106 /** @return The path to the directory to write JPEG2000 files to */
108 Film::j2k_dir () const
117 pair<string, string> f = Filter::ffmpeg_strings (filters());
119 /* Write stuff to specify the filter / post-processing settings that are in use,
120 so that we don't get confused about J2K files generated using different
125 << "_" << content_digest()
126 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
127 << "_" << f.first << "_" << f.second
128 << "_" << scaler()->id();
132 /* Similarly for the A/B case */
135 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
136 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
140 return dir (p.string());
143 /** Add suitable Jobs to the JobManager to create a DCP for this Film.
144 * @param true to transcode, false to use the WAV and J2K files that are already there.
147 Film::make_dcp (bool transcode)
149 if (dcp_name().find ("/") != string::npos) {
150 throw BadSettingError ("name", "cannot contain slashes");
153 log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
157 gethostname (buffer, sizeof (buffer));
158 log()->log (String::compose ("Starting to make DCP on %1", buffer));
162 throw MissingSettingError ("format");
165 if (content().empty ()) {
166 throw MissingSettingError ("content");
169 if (dcp_content_type() == 0) {
170 throw MissingSettingError ("content type");
173 if (name().empty()) {
174 throw MissingSettingError ("name");
177 shared_ptr<const FilmState> fs = state_copy ();
178 shared_ptr<Options> o (new Options (j2k_dir(), ".j2c", dir ("wavs")));
179 o->out_size = format()->dcp_size ();
180 if (dcp_frames() == 0) {
181 /* Decode the whole film, no blacking */
184 switch (dcp_trim_action()) {
186 /* Decode only part of the film, no blacking */
190 /* Decode the whole film, but black some frames out */
191 o->black_after = dcp_frames ();
195 o->padding = format()->dcp_padding (this);
196 o->ratio = format()->ratio_as_float (this);
197 o->decode_subtitles = with_subtitles ();
203 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (fs, o, log(), shared_ptr<Job> ())));
205 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (fs, o, log(), shared_ptr<Job> ())));
209 r = JobManager::instance()->add (shared_ptr<Job> (new CheckHashesJob (fs, o, log(), r)));
210 JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (fs, o, log(), r)));
214 Film::copy_from_dvd_post_gui ()
216 const string dvd_dir = dir ("dvd");
219 uintmax_t largest_size = 0;
220 for (filesystem::directory_iterator i = filesystem::directory_iterator (dvd_dir); i != filesystem::directory_iterator(); ++i) {
221 uintmax_t const s = filesystem::file_size (*i);
222 if (s > largest_size) {
224 #if BOOST_FILESYSTEM_VERSION == 3
225 largest_file = filesystem::path(*i).generic_string();
227 largest_file = i->string ();
233 set_content (largest_file);
236 /** Start a job to examine our content file */
238 Film::examine_content ()
240 if (_examine_content_job) {
244 set_thumbs (vector<int> ());
245 filesystem::remove_all (dir ("thumbs"));
247 /* This call will recreate the directory */
250 _examine_content_job.reset (new ExamineContentJob (state_copy (), log(), shared_ptr<Job> ()));
251 _examine_content_job->Finished.connect (sigc::mem_fun (*this, &Film::examine_content_post_gui));
252 JobManager::instance()->add (_examine_content_job);
256 Film::examine_content_post_gui ()
258 set_length (_examine_content_job->last_video_frame ());
259 _examine_content_job.reset ();
261 string const tdir = dir ("thumbs");
264 for (filesystem::directory_iterator i = filesystem::directory_iterator (tdir); i != filesystem::directory_iterator(); ++i) {
266 /* Aah, the sweet smell of progress */
267 #if BOOST_FILESYSTEM_VERSION == 3
268 string const l = filesystem::path(*i).leaf().generic_string();
270 string const l = i->leaf ();
273 size_t const d = l.find (".png");
274 if (d != string::npos) {
275 thumbs.push_back (atoi (l.substr (0, d).c_str()));
279 sort (thumbs.begin(), thumbs.end());
284 /** @return full paths to any audio files that this Film has */
286 Film::audio_files () const
289 for (filesystem::directory_iterator i = filesystem::directory_iterator (dir("wavs")); i != filesystem::directory_iterator(); ++i) {
290 f.push_back (i->path().string ());
296 /** Start a job to send our DCP to the configured TMS */
298 Film::send_dcp_to_tms ()
300 shared_ptr<Job> j (new SCPDCPJob (state_copy (), log(), shared_ptr<Job> ()));
301 JobManager::instance()->add (j);
305 Film::copy_from_dvd ()
307 shared_ptr<Job> j (new CopyFromDVDJob (state_copy (), log(), shared_ptr<Job> ()));
308 j->Finished.connect (sigc::mem_fun (*this, &Film::copy_from_dvd_post_gui));
309 JobManager::instance()->add (j);
312 /** Count the number of frames that have been encoded for this film.
313 * @return frame count.
316 Film::encoded_frames () const
323 for (filesystem::directory_iterator i = filesystem::directory_iterator (j2k_dir ()); i != filesystem::directory_iterator(); ++i) {
325 this_thread::interruption_point ();
331 /** Return the filename of a subtitle image if one exists for a given thumb index.
332 * @param Thumbnail index.
333 * @return Position of the image within the source frame, and the image filename, if one exists.
334 * Otherwise the filename will be empty.
336 pair<Position, string>
337 Film::thumb_subtitle (int n) const
339 string sub_file = thumb_base(n) + ".sub";
340 if (!filesystem::exists (sub_file)) {
341 return pair<Position, string> ();
344 pair<Position, string> sub;
346 ifstream f (sub_file.c_str ());
347 multimap<string, string> kv = read_key_value (f);
348 for (map<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
349 if (i->first == "x") {
350 sub.first.x = lexical_cast<int> (i->second);
351 } else if (i->first == "y") {
352 sub.first.y = lexical_cast<int> (i->second);
353 sub.second = String::compose ("%1.sub.png", thumb_base(n));
361 Film::set_content (string c)
363 FilmState::set_content (c);