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 /** The pre-processing GUI part of a thumbs update.
107 * Must be called from the GUI thread.
110 Film::update_thumbs_pre_gui ()
112 set_thumbs (vector<int> ());
113 filesystem::remove_all (dir ("thumbs"));
115 /* This call will recreate the directory */
119 /** The post-processing GUI part of a thumbs update.
120 * Must be called from the GUI thread.
123 Film::update_thumbs_post_gui ()
125 string const tdir = dir ("thumbs");
128 for (filesystem::directory_iterator i = filesystem::directory_iterator (tdir); i != filesystem::directory_iterator(); ++i) {
130 /* Aah, the sweet smell of progress */
131 #if BOOST_FILESYSTEM_VERSION == 3
132 string const l = filesystem::path(*i).leaf().generic_string();
134 string const l = i->leaf ();
137 size_t const d = l.find (".png");
138 if (d != string::npos) {
139 thumbs.push_back (atoi (l.substr (0, d).c_str()));
143 sort (thumbs.begin(), thumbs.end());
147 /** @return The path to the directory to write JPEG2000 files to */
149 Film::j2k_dir () const
158 pair<string, string> f = Filter::ffmpeg_strings (filters());
160 /* Write stuff to specify the filter / post-processing settings that are in use,
161 so that we don't get confused about J2K files generated using different
166 << "_" << content_digest()
167 << "_" << crop().left << "_" << crop().right << "_" << crop().top << "_" << crop().bottom
168 << "_" << f.first << "_" << f.second
169 << "_" << scaler()->id();
173 /* Similarly for the A/B case */
176 pair<string, string> fa = Filter::ffmpeg_strings (Config::instance()->reference_filters());
177 s << "ab_" << Config::instance()->reference_scaler()->id() << "_" << fa.first << "_" << fa.second;
181 return dir (p.string());
184 /** Add suitable Jobs to the JobManager to create a DCP for this Film.
185 * @param true to transcode, false to use the WAV and J2K files that are already there.
188 Film::make_dcp (bool transcode, int freq)
190 if (dcp_name().find ("/") != string::npos) {
191 throw BadSettingError ("name", "cannot contain slashes");
194 log()->log (String::compose ("DVD-o-matic %1 git %2 using %3", dvdomatic_version, dvdomatic_git_commit, dependency_version_summary()));
198 gethostname (buffer, sizeof (buffer));
199 log()->log (String::compose ("Starting to make DCP on %1", buffer));
203 throw MissingSettingError ("format");
206 if (content().empty ()) {
207 throw MissingSettingError ("content");
210 if (dcp_content_type() == 0) {
211 throw MissingSettingError ("content type");
214 if (name().empty()) {
215 throw MissingSettingError ("name");
218 shared_ptr<const FilmState> fs = state_copy ();
219 shared_ptr<Options> o (new Options (j2k_dir(), ".j2c", dir ("wavs")));
220 o->out_size = format()->dcp_size ();
221 if (dcp_frames() == 0) {
222 /* Decode the whole film, no blacking */
225 switch (dcp_trim_action()) {
227 /* Decode only part of the film, no blacking */
231 /* Decode the whole film, but black some frames out */
232 o->black_after = dcp_frames ();
236 o->decode_video_frequency = freq;
237 o->padding = format()->dcp_padding (this);
238 o->ratio = format()->ratio_as_float (this);
239 o->decode_subtitles = with_subtitles ();
245 r = JobManager::instance()->add (shared_ptr<Job> (new ABTranscodeJob (fs, o, log(), shared_ptr<Job> ())));
247 r = JobManager::instance()->add (shared_ptr<Job> (new TranscodeJob (fs, o, log(), shared_ptr<Job> ())));
251 r = JobManager::instance()->add (shared_ptr<Job> (new CheckHashesJob (fs, o, log(), r)));
252 JobManager::instance()->add (shared_ptr<Job> (new MakeDCPJob (fs, o, log(), r)));
256 Film::copy_from_dvd_post_gui ()
258 const string dvd_dir = dir ("dvd");
261 uintmax_t largest_size = 0;
262 for (filesystem::directory_iterator i = filesystem::directory_iterator (dvd_dir); i != filesystem::directory_iterator(); ++i) {
263 uintmax_t const s = filesystem::file_size (*i);
264 if (s > largest_size) {
266 #if BOOST_FILESYSTEM_VERSION == 3
267 largest_file = filesystem::path(*i).generic_string();
269 largest_file = i->string ();
275 set_content (largest_file);
279 Film::examine_content ()
281 if (_examine_content_job) {
285 _examine_content_job.reset (new ExamineContentJob (state_copy (), log(), shared_ptr<Job> ()));
286 _examine_content_job->Finished.connect (sigc::mem_fun (*this, &Film::examine_content_post_gui));
287 JobManager::instance()->add (_examine_content_job);
291 Film::examine_content_post_gui ()
293 set_length (_examine_content_job->last_video_frame ());
294 _examine_content_job.reset ();
298 /** @return full paths to any audio files that this Film has */
300 Film::audio_files () const
303 for (filesystem::directory_iterator i = filesystem::directory_iterator (dir("wavs")); i != filesystem::directory_iterator(); ++i) {
304 f.push_back (i->path().string ());
311 Film::send_dcp_to_tms ()
313 shared_ptr<Job> j (new SCPDCPJob (state_copy (), log(), shared_ptr<Job> ()));
314 JobManager::instance()->add (j);
318 Film::copy_from_dvd ()
320 shared_ptr<Job> j (new CopyFromDVDJob (state_copy (), log(), shared_ptr<Job> ()));
321 j->Finished.connect (sigc::mem_fun (*this, &Film::copy_from_dvd_post_gui));
322 JobManager::instance()->add (j);
326 Film::encoded_frames () const
333 for (filesystem::directory_iterator i = filesystem::directory_iterator (j2k_dir ()); i != filesystem::directory_iterator(); ++i) {
335 this_thread::interruption_point ();
341 pair<Position, string>
342 Film::thumb_subtitle (int n) const
344 string sub_file = thumb_base(n) + ".sub";
345 if (!filesystem::exists (sub_file)) {
346 return pair<Position, string> ();
349 pair<Position, string> sub;
351 ifstream f (sub_file.c_str ());
352 multimap<string, string> kv = read_key_value (f);
353 for (map<string, string>::const_iterator i = kv.begin(); i != kv.end(); ++i) {
354 if (i->first == "x") {
355 sub.first.x = lexical_cast<int> (i->second);
356 } else if (i->first == "y") {
357 sub.first.y = lexical_cast<int> (i->second);
358 sub.second = String::compose ("%1.sub.png", thumb_base(n));