swaroop: allow output container specification.
[dcpomatic.git] / src / tools / dcpomatic_ecinema.cc
index a3f2288e3ff1533c8cf49aee4343450de2b07abe..a5ba115d1b6a91ec4b34710a52cd01d653d045be 100644 (file)
@@ -21,6 +21,7 @@
 #include "lib/version.h"
 #include "lib/decrypted_ecinema_kdm.h"
 #include "lib/config.h"
+#include "lib/util.h"
 #include <dcp/key.h>
 extern "C" {
 #include <libavformat/avformat.h>
@@ -47,6 +48,7 @@ help (string n)
             << "  -v, --version        show DCP-o-matic version\n"
             << "  -h, --help           show this help\n"
             << "  -o, --output         output directory\n"
+            << "  -f, --format         output format (mov or mp4; defaults to mov)\n"
             << "\n"
             << "<FILE> is the unencrypted .mp4 file.\n";
 }
@@ -55,6 +57,7 @@ int
 main (int argc, char* argv[])
 {
        optional<boost::filesystem::path> output;
+       optional<boost::filesystem::path> format;
 
        int option_index = 0;
        while (true) {
@@ -62,9 +65,10 @@ main (int argc, char* argv[])
                        { "version", no_argument, 0, 'v'},
                        { "help", no_argument, 0, 'h'},
                        { "output", required_argument, 0, 'o'},
+                       { "format", required_argument, 0, 'f'},
                };
 
-               int c = getopt_long (argc, argv, "vho:", long_options, &option_index);
+               int c = getopt_long (argc, argv, "vho:f:", long_options, &option_index);
 
                if (c == -1) {
                        break;
@@ -80,6 +84,9 @@ main (int argc, char* argv[])
                case 'o':
                        output = optarg;
                        break;
+               case 'f':
+                       format = optarg;
+                       break;
                }
        }
 
@@ -93,6 +100,15 @@ main (int argc, char* argv[])
                exit (EXIT_FAILURE);
        }
 
+       if (!format) {
+               format = "mov";
+       }
+
+       if (*format != "mov" && *format != "mp4") {
+               cerr << "Invalid format specified: must be mov or mp4\n";
+               exit (EXIT_FAILURE);
+       }
+
        boost::filesystem::path input = argv[optind];
        boost::filesystem::path output_mp4 = *output / (input.filename().string() + ".ecinema");
 
@@ -114,7 +130,7 @@ main (int argc, char* argv[])
        }
 
        AVFormatContext* output_fc;
-       avformat_alloc_output_context2 (&output_fc, av_guess_format("mp4", 0, 0), 0, 0);
+       avformat_alloc_output_context2 (&output_fc, av_guess_format(format->c_str(), 0, 0), 0, 0);
 
        for (uint32_t i = 0; i < input_fc->nb_streams; ++i) {
                AVStream* is = input_fc->streams[i];
@@ -155,6 +171,15 @@ main (int argc, char* argv[])
        dcp::Key key (AES_CTR_KEY_SIZE);
        AVDictionary* options = 0;
        av_dict_set (&options, "encryption_key", key.hex().c_str(), 0);
+       /* XXX: is this OK? */
+       av_dict_set (&options, "encryption_kid", "00000000000000000000000000000000", 0);
+       av_dict_set (&options, "encryption_scheme", "cenc-aes-ctr", 0);
+
+       string id = dcp::make_uuid ();
+       if (av_dict_set(&output_fc->metadata, SWAROOP_ID_TAG, id.c_str(), 0) < 0) {
+               cerr << "Could not write ID to output.\n";
+               exit (EXIT_FAILURE);
+       }
 
        if (avformat_write_header (output_fc, &options) < 0) {
                cerr << "Could not write header to output.\n";
@@ -180,7 +205,7 @@ main (int argc, char* argv[])
        avformat_free_context (input_fc);
        avformat_free_context (output_fc);
 
-       DecryptedECinemaKDM decrypted_kdm (key);
+       DecryptedECinemaKDM decrypted_kdm (id, output_mp4.filename().string(), key);
        EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf());
        cout << encrypted_kdm.as_xml() << "\n";
 }