Gracefully handle templates that lack contents in their description or created_with...
[ardour.git] / gtk2_ardour / export_video_dialog.cc
index 0cc0a4d9091e001178204c818ad4c0de02d2dd7c..1972561ed2ca63238ed38814cdc8082786e3af64 100644 (file)
 
 #include <sigc++/bind.h>
 
-#include <glib/gstdio.h>
+#include <gtkmm/filechooserdialog.h>
+#include <gtkmm/stock.h>
+#include <gtkmm/table.h>
+
+#include "pbd/gstdio_compat.h"
 
 #include "pbd/error.h"
 #include "pbd/convert.h"
@@ -55,7 +59,7 @@
 #include "opts.h"
 #include "export_video_dialog.h"
 #include "utils_videotl.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace Gtk;
 using namespace std;
@@ -79,6 +83,7 @@ ExportVideoDialog::ExportVideoDialog ()
        , invid_browse_button (_("Browse"))
        , transcode_button (_("Export"))
        , abort_button (_("Abort"))
+       , progress_box (0)
        , scale_checkbox (_("Scale Video (W x H):"))
        , scale_aspect (_("Retain Aspect"))
        , width_adjustment (768, 128, 1920, 1, 16, 0)
@@ -109,14 +114,15 @@ ExportVideoDialog::ExportVideoDialog ()
        HBox* path_hbox;
 
        /* check if ffmpeg can be found */
-       _transcoder = new TranscodeFfmpeg("");
+       _transcoder = new TranscodeFfmpeg(X_(""));
        if (!_transcoder->ffexec_ok()) {
-               l = manage (new Label (_("No ffprobe or ffmpeg executables could be found on this system. Video Export is not possible until you install those tools. See the Log window for more information."), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
+               l = manage (new Label (_("ffmpeg installation was not found. Video Export is not possible. See the Log window for more information."), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
                l->set_line_wrap();
                vbox->pack_start (*l, false, false, 8);
                get_vbox()->pack_start (*vbox, false, false);
                add_button (Stock::OK, RESPONSE_CANCEL);
                show_all_children ();
+               delete _transcoder; _transcoder = 0;
                return;
        }
        delete _transcoder; _transcoder = 0;
@@ -148,7 +154,6 @@ ExportVideoDialog::ExportVideoDialog ()
        path_hbox->pack_start (*l, false, false, 2);
        vbox->pack_start (*path_hbox, false, false, 2);
 
-       insnd_combo.set_name ("PaddedButton");
        insnd_combo.append_text (string_compose (_("from the %1 session's start to the session's end"), PROGRAM_NAME));
        outfn_path_entry.set_width_chars(38);
 
@@ -200,7 +205,6 @@ ExportVideoDialog::ExportVideoDialog ()
        t->attach (debug_checkbox, 0, 4, ty, ty+1); ty++;
 #endif
 
-       preset_combo.set_name ("PaddedButton");
        preset_combo.append_text("none");
        preset_combo.append_text("dvd-mp2");
        preset_combo.append_text("dvd-NTSC");
@@ -212,7 +216,6 @@ ExportVideoDialog::ExportVideoDialog ()
        preset_combo.append_text("webm");
        preset_combo.append_text("you-tube");
 
-       audio_codec_combo.set_name ("PaddedButton");
        audio_codec_combo.append_text(_("(default for format)"));
        audio_codec_combo.append_text("ac3");
        audio_codec_combo.append_text("aac");
@@ -221,7 +224,6 @@ ExportVideoDialog::ExportVideoDialog ()
        audio_codec_combo.append_text("mp2");
        audio_codec_combo.append_text("pcm_s16le");
 
-       video_codec_combo.set_name ("PaddedButton");
        video_codec_combo.append_text(_("(default for format)"));
        video_codec_combo.append_text("flv");
        video_codec_combo.append_text("libtheora");
@@ -232,7 +234,6 @@ ExportVideoDialog::ExportVideoDialog ()
        video_codec_combo.append_text("vpx (webm)");
        video_codec_combo.append_text("copy");
 
-       audio_bitrate_combo.set_name ("PaddedButton");
        audio_bitrate_combo.append_text(_("(default)"));
        audio_bitrate_combo.append_text("64k");
        audio_bitrate_combo.append_text("128k");
@@ -240,12 +241,10 @@ ExportVideoDialog::ExportVideoDialog ()
        audio_bitrate_combo.append_text("256k");
        audio_bitrate_combo.append_text("320k");
 
-       audio_samplerate_combo.set_name ("PaddedButton");
        audio_samplerate_combo.append_text("22050");
        audio_samplerate_combo.append_text("44100");
        audio_samplerate_combo.append_text("48000");
 
-       video_bitrate_combo.set_name ("PaddedButton");
        video_bitrate_combo.append_text(_("(default)"));
        video_bitrate_combo.append_text(_("(retain)"));
        video_bitrate_combo.append_text("200k");
@@ -254,7 +253,6 @@ ExportVideoDialog::ExportVideoDialog ()
        video_bitrate_combo.append_text("5000k");
        video_bitrate_combo.append_text("8000k");
 
-       fps_combo.set_name ("PaddedButton");
        fps_combo.append_text("23.976");
        fps_combo.append_text("24");
        fps_combo.append_text("24.976");
@@ -264,7 +262,6 @@ ExportVideoDialog::ExportVideoDialog ()
        fps_combo.append_text("59.94");
        fps_combo.append_text("60");
 
-       aspect_combo.set_name ("PaddedButton");
        aspect_combo.append_text("4:3");
        aspect_combo.append_text("16:9");
 
@@ -373,25 +370,21 @@ ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
 
        float tcfps = _session->timecode_frames_per_second();
 
-       LocaleGuard lg (X_("C"));
-
        XMLNode* node = _session->extra_xml (X_("Videotimeline"));
+       bool filenameset = false;
        if (node) {
-               bool filenameset = false;
-               if (node->property(X_("OriginalVideoFile"))) {
-                       std::string filename = node->property(X_("OriginalVideoFile"))->value();
+               std::string filename;
+               if (node->get_property(X_("OriginalVideoFile"), filename)) {
                        if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
                                invid_path_entry.set_text (filename);
                                filenameset = true;
                        }
                }
-               if (!filenameset
-                               && node->property(X_("Filename"))
-                               && node->property(X_("LocalFile"))
-                               && node->property(X_("LocalFile"))->value() == X_("1")
-                  )
-               {
-                       std::string filename = node->property(X_("Filename"))->value();
+
+               bool local_file;
+
+               if (!filenameset && node->get_property (X_("Filename"), filename) &&
+                   node->get_property (X_("LocalFile"), local_file) && local_file) {
                        if (filename.at(0) != G_DIR_SEPARATOR)
                        {
                                filename = Glib::build_filename (_session->session_directory().video_path(), filename);
@@ -402,62 +395,85 @@ ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
                                filenameset = true;
                        }
                }
-               if (!filenameset) {
-                       invid_path_entry.set_text (X_(""));
-               }
+       }
+       if (!filenameset) {
+               invid_path_entry.set_text (X_(""));
        }
 
        node = _session->extra_xml (X_("Videoexport"));
        if (node) {
-               const XMLProperty* prop;
-               prop = node->property (X_("ChangeGeometry"));
-               if (prop) { scale_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("KeepAspect"));
-               if (prop) { scale_aspect.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("ChangeAspect"));
-               if (prop) { aspect_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("NormalizeAudio"));
-               if (prop) { normalize_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("TwoPassEncode"));
-               if (prop) { twopass_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("CodecOptimzations"));
-               if (prop) { optimizations_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("Deinterlace"));
-               if (prop) { deinterlace_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("BFrames"));
-               if (prop) { bframes_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("ChangeFPS"));
-               if (prop) { fps_checkbox.set_active(atoi(prop->value())?true:false); }
-               prop = node->property (X_("Metadata"));
-               if (prop) { meta_checkbox.set_active(atoi(prop->value())?true:false); }
-
-               prop = node->property (X_("Format"));
-               if (prop && !prop->value().empty()) { change_file_extension( "." + prop->value()); }
+               bool yn;
+               if (node->get_property (X_("ChangeGeometry"), yn)) {
+                       scale_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("KeepAspect"), yn)) {
+                       scale_aspect.set_active (yn);
+               }
+               if (node->get_property (X_("ChangeAspect"), yn)) {
+                       aspect_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("NormalizeAudio"), yn)) {
+                       normalize_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("TwoPassEncode"), yn)) {
+                       twopass_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("CodecOptimzations"), yn)) {
+                       optimizations_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("Deinterlace"), yn)) {
+                       deinterlace_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("BFrames"), yn)) {
+                       bframes_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("ChangeFPS"), yn)) {
+                       fps_checkbox.set_active (yn);
+               }
+               if (node->get_property (X_("Metadata"), yn)) {
+                       meta_checkbox.set_active (yn);
+               }
+
+               std::string str;
+               if (node->get_property (X_("Format"), str) && !str.empty()) {
+                       change_file_extension ("." + str);
+               }
 
                _suspend_signals = true;
-               prop = node->property (X_("Width"));
-               if (prop) { width_spinner.set_value(atoi(prop->value())); }
-               prop = node->property (X_("Height"));
-               if (prop) { height_spinner.set_value(atoi(prop->value())); }
+               double val;
+               if (node->get_property (X_("Width"), val)) {
+                       width_spinner.set_value (val);
+               }
+               if (node->get_property (X_("Height"), val)) {
+                       height_spinner.set_value (val);
+               }
                _suspend_signals = false;
 
-               prop = node->property (X_("FPS"));
-               if (prop && fps_checkbox.get_active()) { tcfps = atof(prop->value()); }
-
-               prop = node->property (X_("Preset"));
-               if (prop) { preset_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("VCodec"));
-               if (prop) { video_codec_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("ACodec"));
-               if (prop) { audio_codec_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("VBitrate"));
-               if (prop) { video_bitrate_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("ABitrate"));
-               if (prop) { audio_bitrate_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("AspectRatio"));
-               if (prop) { aspect_combo.set_active_text(prop->value()); }
-               prop = node->property (X_("SampleRate"));
-               if (prop) { audio_samplerate_combo.set_active_text(prop->value()); }
+               if (fps_checkbox.get_active () && node->get_property (X_("FPS"), val)) {
+                       tcfps = val;
+               }
+
+               if (node->get_property (X_("Preset"), str)) {
+                       preset_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("VCodec"), str)) {
+                       video_codec_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("ACodec"), str)) {
+                       audio_codec_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("VBitrate"), str)) {
+                       video_bitrate_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("ABitrate"), str)) {
+                       audio_bitrate_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("AspectRatio"), str)) {
+                       aspect_combo.set_active_text (str);
+               }
+               if (node->get_property (X_("SampleRate"), str)) {
+                       audio_samplerate_combo.set_active_text (str);
+               }
        }
 
        if      (fabs(tcfps - 23.976) < 0.01) { fps_combo.set_active(0); }
@@ -481,38 +497,39 @@ ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
        _suspend_dirty = false;
 
        show_all_children ();
-       progress_box->hide();
+       if (progress_box) {
+               progress_box->hide();
+       }
 }
 
 XMLNode&
 ExportVideoDialog::get_state ()
 {
-       LocaleGuard lg (X_("C"));
        XMLNode* node = new XMLNode (X_("Videoexport"));
-       node->add_property (X_("ChangeGeometry"), scale_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("KeepAspect"), scale_aspect.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("ChangeAspect"), aspect_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("NormalizeAudio"), normalize_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("TwoPassEncode"), twopass_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("CodecOptimzations"), optimizations_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("Deinterlace"), deinterlace_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("BFrames"), bframes_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("ChangeFPS"), fps_checkbox.get_active() ? X_("1") : X_("0"));
-       node->add_property (X_("Metadata"), meta_checkbox.get_active() ? X_("1") : X_("0"));
-
-       node->add_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
-
-       node->add_property (X_("Width"), width_spinner.get_value());
-       node->add_property (X_("Height"), height_spinner.get_value());
-
-       node->add_property (X_("Preset"), preset_combo.get_active_text());
-       node->add_property (X_("VCodec"), video_codec_combo.get_active_text());
-       node->add_property (X_("ACodec"), audio_codec_combo.get_active_text());
-       node->add_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
-       node->add_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
-       node->add_property (X_("AspectRatio"), aspect_combo.get_active_text());
-       node->add_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
-       node->add_property (X_("FPS"), fps_combo.get_active_text());
+       node->set_property (X_("ChangeGeometry"), scale_checkbox.get_active());
+       node->set_property (X_("KeepAspect"), scale_aspect.get_active());
+       node->set_property (X_("ChangeAspect"), aspect_checkbox.get_active());
+       node->set_property (X_("NormalizeAudio"), normalize_checkbox.get_active());
+       node->set_property (X_("TwoPassEncode"), twopass_checkbox.get_active());
+       node->set_property (X_("CodecOptimzations"), optimizations_checkbox.get_active());
+       node->set_property (X_("Deinterlace"), deinterlace_checkbox.get_active());
+       node->set_property (X_("BFrames"), bframes_checkbox.get_active());
+       node->set_property (X_("ChangeFPS"), fps_checkbox.get_active());
+       node->set_property (X_("Metadata"), meta_checkbox.get_active());
+
+       node->set_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
+
+       node->set_property (X_("Width"), width_spinner.get_value());
+       node->set_property (X_("Height"), height_spinner.get_value());
+
+       node->set_property (X_("Preset"), preset_combo.get_active_text());
+       node->set_property (X_("VCodec"), video_codec_combo.get_active_text());
+       node->set_property (X_("ACodec"), audio_codec_combo.get_active_text());
+       node->set_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
+       node->set_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
+       node->set_property (X_("AspectRatio"), aspect_combo.get_active_text());
+       node->set_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
+       node->set_property (X_("FPS"), fps_combo.get_active_text());
 
        return *node;
 }
@@ -528,22 +545,6 @@ ExportVideoDialog::on_show ()
        Dialog::on_show ();
 }
 
-bool
-ExportVideoDialog::on_focus_in_event (GdkEventFocus *ev)
-{
-       Dialog::on_focus_in_event (ev);
-       Gtkmm2ext::Keyboard::magic_widget_grab_focus ();
-       return true;
-}
-
-bool
-ExportVideoDialog::on_focus_out_event (GdkEventFocus *ev)
-{
-       Dialog::on_focus_out_event (ev);
-       Gtkmm2ext::Keyboard::magic_widget_drop_focus ();
-       return true;
-}
-
 void
 ExportVideoDialog::abort_clicked ()
 {
@@ -576,23 +577,36 @@ gint
 ExportVideoDialog::audio_progress_display ()
 {
        std::string status_text;
-       double progress = 0.0;
-               if (status->normalizing) {
+       double progress = -1.0;
+       switch (status->active_job) {
+               case ExportStatus::Normalizing:
                        pbar.set_text (_("Normalizing audio"));
-                       progress = ((float) status->current_normalize_cycle) / status->total_normalize_cycles;
-                       progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0/3.0);
-               } else {
+                       progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
+                       progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0 / 3.0);
+                       break;
+               case ExportStatus::Exporting:
                        pbar.set_text (_("Exporting audio"));
                        progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
                        progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
-               }
-               if (progress < _previous_progress) {
-                       // Work around gtk bug
-                       pbar.hide();
-                       pbar.show();
-               }
-               _previous_progress = progress;
+                       break;
+               default:
+                       pbar.set_text (_("Exporting audio"));
+                       break;
+       }
+
+       if (progress < _previous_progress) {
+               // Work around gtk bug
+               pbar.hide();
+               pbar.show();
+       }
+       _previous_progress = progress;
+
+       if (progress >= 0) {
                pbar.set_fraction (progress);
+       } else {
+               pbar.set_pulse_step(.1);
+               pbar.pulse();
+       }
        return TRUE;
 }
 
@@ -631,7 +645,7 @@ ExportVideoDialog::launch_export ()
        _session->add_extra_xml (get_state());
 
        std::string outfn = outfn_path_entry.get_text();
-       if (!confirm_video_outfn(outfn)) { return; }
+       if (!confirm_video_outfn(*this, outfn)) { return; }
 
        vbox->hide();
        cancel_button->hide();
@@ -763,7 +777,7 @@ ExportVideoDialog::launch_export ()
 
        audio_progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportVideoDialog::audio_progress_display), 100);
        _previous_progress = 0.0;
-       while (status->running) {
+       while (status->running ()) {
                if (_aborted) { status->abort(); }
                if (gtk_events_pending()) {
                        gtk_main_iteration ();