Rename Encoder -> FilmEncoder, and subclasses.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
index 5e55b4da273d93e93ff1ad749a228d046d896269..9eec498f975d72d2dad73552cd2987c3d8fa71f4 100644 (file)
 */
 
 
+#include "lib/ansi.h"
 #include "lib/audio_content.h"
 #include "lib/config.h"
 #include "lib/cross.h"
 #include "lib/dcpomatic_log.h"
 #include "lib/encode_server_finder.h"
-#include "lib/ffmpeg_encoder.h"
+#include "lib/ffmpeg_film_encoder.h"
 #include "lib/film.h"
 #include "lib/filter.h"
+#ifdef DCPOMATIC_GROK
+#include "lib/grok/context.h"
+#endif
+#include "lib/hints.h"
 #include "lib/job_manager.h"
 #include "lib/json_server.h"
 #include "lib/log.h"
 #include "lib/signal_manager.h"
 #include "lib/transcode_job.h"
 #include "lib/util.h"
+#include "lib/variant.h"
 #include "lib/version.h"
 #include "lib/video_content.h"
+#include <dcp/filesystem.h>
 #include <dcp/version.h>
 #include <getopt.h>
 #include <iostream>
@@ -60,7 +67,7 @@ static void
 help (string n)
 {
        cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
-            << "  -v, --version                     show DCP-o-matic version\n"
+            << variant::insert_dcpomatic("  -v, --version                     show %1 version\n")
             << "  -h, --help                        show this help\n"
             << "  -f, --flags                       show flags passed to C++ compiler on build\n"
             << "  -n, --no-progress                 do not print progress to stdout\n"
@@ -69,13 +76,14 @@ help (string n)
             << "  -j, --json <port>                 run a JSON server on the specified port\n"
             << "  -k, --keep-going                  keep running even when the job is complete\n"
             << "  -s, --servers <file>              specify servers to use in a text file\n"
-            << "  -l, --list-servers                just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
+            << variant::insert_dcpomatic("  -l, --list-servers                just display a list of encoding servers that %1 is configured to use; don't encode\n")
             << "  -d, --dcp-path                    echo DCP's path to stdout on successful completion (implies -n)\n"
             << "  -c, --config <dir>                directory containing config.xml and cinemas.xml\n"
             << "      --dump                        just dump a summary of the film's settings; don't encode\n"
             << "      --no-check                    don't check project's content files for changes before making the DCP\n"
             << "      --export-format <format>      export project to a file, rather than making a DCP: specify mov or mp4\n"
             << "      --export-filename <filename>  filename to export to with --export-format\n"
+            << "      --hints                       analyze film for hints before encoding and abort if any are found\n"
             << "\n"
             << "<FILM> is the film directory.\n";
 }
@@ -87,19 +95,20 @@ print_dump (shared_ptr<Film> film)
        cout << film->dcp_name (true) << "\n"
             << film->container()->container_nickname() << " at " << ((film->resolution() == Resolution::TWO_K) ? "2K" : "4K") << "\n"
             << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
+            << "Duration " << (film->length().timecode(film->video_frame_rate())) << "\n"
             << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
             << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
 
        for (auto c: film->content()) {
                cout << "\n"
-                    << c->path(0) << "\n"
+                    << c->path(0).string() << "\n"
                     << "\tat " << c->position().seconds ()
                     << " length " << c->full_length(film).seconds ()
                     << " start trim " << c->trim_start().seconds ()
                     << " end trim " << c->trim_end().seconds () << "\n";
 
-               if (c->video) {
-                       cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
+               if (c->video && c->video->size()) {
+                       cout << "\t" << c->video->size()->width << "x" << c->video->size()->height << "\n"
                             << "\t" << c->active_video_frame_rate(film) << "fps\n"
                             << "\tcrop left " << c->video->requested_left_crop()
                             << " right " << c->video->requested_right_crop()
@@ -209,7 +218,7 @@ show_jobs_on_console (bool progress)
 
                if (!first && progress) {
                        for (size_t i = 0; i < jobs.size(); ++i) {
-                               cout << "\033[1A\033[2K";
+                               cout << UP_ONE_LINE_AND_ERASE;
                        }
                        cout.flush ();
                }
@@ -269,6 +278,7 @@ main (int argc, char* argv[])
        bool check = true;
        optional<string> export_format;
        optional<boost::filesystem::path> export_filename;
+       bool hints = false;
 
        int option_index = 0;
        while (true) {
@@ -290,10 +300,11 @@ main (int argc, char* argv[])
                        { "no-check", no_argument, 0, 'B' },
                        { "export-format", required_argument, 0, 'C' },
                        { "export-filename", required_argument, 0, 'D' },
+                       { "hints", no_argument, 0, 'E' },
                        { 0, 0, 0, 0 }
                };
 
-               int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:BC:D:", long_options, &option_index);
+               int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ldc:BC:D:E", long_options, &option_index);
 
                if (c == -1) {
                        break;
@@ -349,6 +360,9 @@ main (int argc, char* argv[])
                case 'D':
                        export_filename = optarg;
                        break;
+               case 'E':
+                       hints = true;
+                       break;
                }
        }
 
@@ -404,7 +418,7 @@ main (int argc, char* argv[])
        signal_manager = new SignalManager ();
 
        if (no_remote || export_format) {
-               EncodeServerFinder::instance()->stop ();
+               EncodeServerFinder::drop();
        }
 
        if (json_port) {
@@ -434,13 +448,64 @@ main (int argc, char* argv[])
        for (auto i: film->content()) {
                auto paths = i->paths();
                for (auto j: paths) {
-                       if (!boost::filesystem::exists(j)) {
+                       if (!dcp::filesystem::exists(j)) {
                                cerr << argv[0] << ": content file " << j << " not found.\n";
                                exit (EXIT_FAILURE);
                        }
                }
        }
 
+       if (!export_format && hints) {
+               string const prefix = "Checking project for hints";
+               bool pulse_phase = false;
+               vector<string> hints;
+               bool finished = false;
+
+               Hints hint_finder(film);
+               hint_finder.Progress.connect([prefix](string progress) {
+                                            std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << progress << "\n";
+                                            std::cout.flush();
+                                            });
+               hint_finder.Pulse.connect([prefix, &pulse_phase]() {
+                                         std::cout << UP_ONE_LINE_AND_ERASE << prefix << ": " << (pulse_phase ? "X" : "x") << "\n";
+                                         std::cout.flush();
+                                         pulse_phase = !pulse_phase;
+                                         });
+               hint_finder.Hint.connect([&hints](string hint) {
+                                        hints.push_back(hint);
+                                        });
+               hint_finder.Finished.connect([&finished]() {
+                                            finished = true;
+                                            });
+
+               std::cout << prefix << ":\n";
+               std::cout.flush();
+
+               hint_finder.start();
+               while (!finished) {
+                       signal_manager->ui_idle();
+                       dcpomatic_sleep_milliseconds(200);
+               }
+
+               std::cout << UP_ONE_LINE_AND_ERASE;
+
+               if (!hints.empty()) {
+                       std::cerr << "Hints:\n\n";
+                       for (auto hint: hints) {
+                               std::cerr << word_wrap("* " + hint, 70) << "\n";
+                       }
+                       std::cerr << "*** Encoding aborted because hints were found ***\n\n";
+                       std::cerr << "Modify your settings and run the command again, or run without\n";
+                       std::cerr << "the `--hints' option to ignore these hints and encode anyway.\n";
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+#ifdef DCPOMATIC_GROK
+       grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] "));
+       setup_grok_library_path();
+#endif
+
        if (progress) {
                if (export_format) {
                        cout << "\nExporting " << film->name() << "\n";
@@ -454,7 +519,7 @@ main (int argc, char* argv[])
        if (export_format) {
                auto job = std::make_shared<TranscodeJob>(film, behaviour);
                job->set_encoder (
-                       std::make_shared<FFmpegEncoder> (
+                       std::make_shared<FFmpegFilmEncoder>(
                                film, job, *export_filename, *export_format == "mp4" ? ExportFormat::H264_AAC : ExportFormat::PRORES_HQ, false, false, false, 23
                                )
                        );