Move things round a bit.
[dcpomatic.git] / src / lib / make_dcp_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/make_dcp_job.cc
21  *  @brief A job to create DCPs.
22  */
23
24 #include <boost/filesystem.hpp>
25 extern "C" {
26 #include <libavutil/pixdesc.h>
27 }
28 #include "make_dcp_job.h"
29 #include "film_state.h"
30 #include "dcp_content_type.h"
31 #include "exceptions.h"
32
33 using namespace std;
34 using namespace boost;
35
36 /** @param s FilmState of the Film we are making the DCP for.
37  *  @param o Options.
38  *  @param l Log.
39  */
40 MakeDCPJob::MakeDCPJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
41         : ShellCommandJob (s, o, l)
42 {
43         
44 }
45
46 string
47 MakeDCPJob::name () const
48 {
49         stringstream s;
50         s << "Make DCP for " << _fs->name;
51         return s.str ();
52 }
53
54 void
55 MakeDCPJob::run ()
56 {
57         set_progress_unknown ();
58
59         string const dcp_path = _fs->dir (_fs->name);
60         
61         /* Check that we have our prerequisites */
62
63         if (!filesystem::exists (filesystem::path (_fs->file ("video.mxf")))) {
64                 throw EncodeError ("missing video.mxf");
65         }
66
67         bool const have_audio = filesystem::exists (filesystem::path (_fs->file ("audio.mxf")));
68
69         /* Remove any old DCP */
70         filesystem::remove_all (dcp_path);
71
72         /* Re-make the DCP directory */
73         _fs->dir (_fs->name);
74         
75         stringstream c;
76         c << "cd \"" << dcp_path << "\" && "
77           << " opendcp_xml -d -a " << _fs->name
78           << " -t \"" << _fs->name << "\""
79           << " -k " << _fs->dcp_content_type->opendcp_name()
80           << " --reel \"" << _fs->file ("video.mxf") << "\"";
81         
82         if (have_audio) {
83                 c << " \"" << _fs->file ("audio.mxf") << "\"";
84         }
85
86         command (c.str ());
87
88         filesystem::rename (filesystem::path (_fs->file ("video.mxf")), filesystem::path (dcp_path + "/video.mxf"));
89         if (have_audio) {
90                 filesystem::rename (filesystem::path (_fs->file ("audio.mxf")), filesystem::path (dcp_path + "/audio.mxf"));
91         }
92
93         set_progress (1);
94 }