rework video-export dialog, remember settings
[ardour.git] / gtk2_ardour / export_video_dialog.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3     Author: Robin Gareus <robin@gareus.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include <cstdio>
21 #include <string>
22 #include <sstream>
23 #include <iomanip>
24
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29
30 #include <sigc++/bind.h>
31
32 #include <glib/gstdio.h>
33
34 #include "pbd/error.h"
35 #include "pbd/convert.h"
36 #include "gtkmm2ext/keyboard.h"
37 #include "gtkmm2ext/utils.h"
38 #include "ardour/session_directory.h"
39 #include "ardour/profile.h"
40 #include "ardour/template_utils.h"
41 #include "ardour/session.h"
42 #include "ardour_ui.h"
43 #include "gui_thread.h"
44
45 #include "ardour/export_handler.h"
46 #include "ardour/export_status.h"
47 #include "ardour/export_timespan.h"
48 #include "ardour/export_channel_configuration.h"
49 #include "ardour/export_format_specification.h"
50 #include "ardour/export_filename.h"
51 #include "ardour/route.h"
52 #include "ardour/session_metadata.h"
53 #include "ardour/broadcast_info.h"
54
55 #include "opts.h"
56 #include "export_video_dialog.h"
57 #include "utils_videotl.h"
58 #include "i18n.h"
59
60 using namespace Gtk;
61 using namespace std;
62 using namespace PBD;
63 using namespace ARDOUR;
64 using namespace VideoUtils;
65
66 ExportVideoDialog::ExportVideoDialog ()
67         : ArdourDialog (_("Export Video File "))
68         , _aborted(false)
69         , _twopass(false)
70         , _firstpass(false)
71         , _normalize(false)
72         , _previous_progress(0)
73         , _transcoder(0)
74         , _video_source_aspect_ratio(-1)
75         , _suspend_signals(false)
76         , outfn_path_label (_("File:"), Gtk::ALIGN_LEFT)
77         , outfn_browse_button (_("Browse"))
78         , invid_path_label (_("Video:"), Gtk::ALIGN_LEFT)
79         , invid_browse_button (_("Browse"))
80         , transcode_button (_("Export"))
81         , abort_button (_("Abort"))
82         , scale_checkbox (_("Scale Video (W x H):"))
83         , scale_aspect (_("Retain Aspect"))
84         , width_adjustment (768, 128, 1920, 1, 16, 0)
85         , width_spinner (width_adjustment)
86         , height_adjustment (576, 128, 1920, 1, 16, 0)
87         , height_spinner (height_adjustment)
88         , aspect_checkbox (_("Set Aspect Ratio:"))
89         , normalize_checkbox (_("Normalize Audio"))
90         , twopass_checkbox (_("2 Pass Encoding"))
91         , optimizations_checkbox (_("Codec Optimizations:"))
92         , optimizations_label ("-")
93         , deinterlace_checkbox (_("Deinterlace"))
94         , bframes_checkbox (_("Use [2] B-frames (MPEG 2 or 4 only)"))
95         , fps_checkbox (_("Override FPS (Default is to retain FPS from the input video file):"))
96         , meta_checkbox (_("Include Session Metadata"))
97 #if 1 /* tentative debug mode */
98         , debug_checkbox (_("Debug Mode: Print ffmpeg command and output to stdout."))
99 #endif
100 {
101         set_name ("ExportVideoDialog");
102         set_modal (true);
103         set_skip_taskbar_hint (true);
104         set_resizable (false);
105
106         Gtk::Label* l;
107         vbox = manage (new VBox);
108         VBox* options_box = manage (new VBox);
109         HBox* path_hbox;
110
111         /* check if ffmpeg can be found */
112         _transcoder = new TranscodeFfmpeg("");
113         if (!_transcoder->ffexec_ok()) {
114                 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));
115                 l->set_line_wrap();
116                 vbox->pack_start (*l, false, false, 8);
117                 get_vbox()->pack_start (*vbox, false, false);
118                 add_button (Stock::OK, RESPONSE_CANCEL);
119                 show_all_children ();
120                 return;
121         }
122         delete _transcoder; _transcoder = 0;
123
124         l = manage (new Label (_("<b>Output:</b> (file extension defines format)"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
125         l->set_use_markup ();
126         vbox->pack_start (*l, false, false, 4);
127
128         path_hbox = manage (new HBox);
129         path_hbox->pack_start (outfn_path_label, false, false, 3);
130         path_hbox->pack_start (outfn_path_entry, true, true, 3);
131         path_hbox->pack_start (outfn_browse_button, false, false, 3);
132         vbox->pack_start (*path_hbox, false, false, 2);
133
134         l = manage (new Label (_("<b>Input Video:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
135         l->set_use_markup ();
136         vbox->pack_start (*l, false, false, 4);
137
138         path_hbox = manage (new HBox);
139         path_hbox->pack_start (invid_path_label, false, false, 3);
140         path_hbox->pack_start (invid_path_entry, true, true, 3);
141         path_hbox->pack_start (invid_browse_button, false, false, 3);
142         vbox->pack_start (*path_hbox, false, false, 2);
143
144         path_hbox = manage (new HBox);
145         l = manage (new Label (_("Audio:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
146         path_hbox->pack_start (*l, false, false, 3);
147         l = manage (new Label (_("Master Bus"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
148         path_hbox->pack_start (*l, false, false, 2);
149         vbox->pack_start (*path_hbox, false, false, 2);
150
151         insnd_combo.set_name ("PaddedButton");
152         insnd_combo.append_text (string_compose (_("from the %1 session's start to the session's end"), PROGRAM_NAME));
153         outfn_path_entry.set_width_chars(38);
154
155         l = manage (new Label (_("<b>Settings:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
156         l->set_use_markup ();
157         options_box->pack_start (*l, false, true, 4);
158
159         Table* t = manage (new Table (4, 12));
160         t->set_spacings (4);
161         int ty = 0;
162         options_box->pack_start (*t, true, true, 4);
163         l = manage (new Label (_("Range:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
164         t->attach (*l, 0, 1, ty, ty+1);
165         t->attach (insnd_combo, 1, 4, ty, ty+1); ty++;
166         l = manage (new Label (_("Preset:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
167         t->attach (*l, 0, 1, ty, ty+1);
168         t->attach (preset_combo, 1, 4, ty, ty+1); ty++;
169         l = manage (new Label (_("Video Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
170         t->attach (*l, 0, 1, ty, ty+1);
171         t->attach (video_codec_combo, 1, 2, ty, ty+1);
172         l = manage (new Label (_("Video KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
173         t->attach (*l, 2, 3, ty, ty+1);
174         t->attach (video_bitrate_combo, 3, 4, ty, ty+1); ty++;
175         l = manage (new Label (_("Audio Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
176         t->attach (*l, 0, 1, ty, ty+1);
177         t->attach (audio_codec_combo, 1, 2, ty, ty+1);
178         l = manage (new Label (_("Audio KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
179         t->attach (*l, 2, 3, ty, ty+1);
180         t->attach (audio_bitrate_combo, 3, 4, ty, ty+1); ty++;
181         l = manage (new Label (_("Audio Samplerate:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
182         t->attach (*l, 0, 1, ty, ty+1);
183         t->attach (audio_samplerate_combo, 1, 2, ty, ty+1);
184         t->attach (normalize_checkbox, 2, 4, ty, ty+1); ty++;
185         t->attach (scale_checkbox, 0, 1, ty, ty+1);
186         t->attach (scale_aspect, 1, 2, ty, ty+1);
187         t->attach (width_spinner, 2, 3, ty, ty+1);
188         t->attach (height_spinner, 3, 4, ty, ty+1); ty++;
189         t->attach (fps_checkbox, 0, 3, ty, ty+1);
190         t->attach (fps_combo, 3, 4, ty, ty+1); ty++;
191         t->attach (twopass_checkbox, 0, 2, ty, ty+1);
192         t->attach (aspect_checkbox, 2, 3, ty, ty+1);
193         t->attach (aspect_combo, 3, 4, ty, ty+1); ty++;
194         t->attach (bframes_checkbox, 0, 2, ty, ty+1);
195         t->attach (deinterlace_checkbox, 2, 4, ty, ty+1); ty++;
196         t->attach (meta_checkbox, 2, 4, ty, ty+1); ty++;
197         t->attach (optimizations_checkbox, 0, 1, ty, ty+1);
198         t->attach (optimizations_label, 1, 4, ty, ty+1); ty++;
199 #if 1 /* tentative debug mode */
200         t->attach (debug_checkbox, 0, 4, ty, ty+1); ty++;
201 #endif
202
203         preset_combo.set_name ("PaddedButton");
204         preset_combo.append_text("none");
205         preset_combo.append_text("dvd-mp2");
206         preset_combo.append_text("dvd-NTSC");
207         preset_combo.append_text("dvd-PAL");
208         preset_combo.append_text("flv");
209         preset_combo.append_text("mpeg4");
210         preset_combo.append_text("mp4/h264/aac");
211         preset_combo.append_text("ogg");
212         preset_combo.append_text("webm");
213         preset_combo.append_text("you-tube");
214
215         audio_codec_combo.set_name ("PaddedButton");
216         audio_codec_combo.append_text(_("(default for format)"));
217         audio_codec_combo.append_text("ac3");
218         audio_codec_combo.append_text("aac");
219         audio_codec_combo.append_text("libmp3lame");
220         audio_codec_combo.append_text("libvorbis");
221         audio_codec_combo.append_text("mp2");
222         audio_codec_combo.append_text("pcm_s16le");
223
224         video_codec_combo.set_name ("PaddedButton");
225         video_codec_combo.append_text(_("(default for format)"));
226         video_codec_combo.append_text("flv");
227         video_codec_combo.append_text("libtheora");
228         video_codec_combo.append_text("mjpeg");
229         video_codec_combo.append_text("mpeg2video");
230         video_codec_combo.append_text("mpeg4");
231         video_codec_combo.append_text("h264");
232         video_codec_combo.append_text("vpx (webm)");
233         video_codec_combo.append_text("copy");
234
235         audio_bitrate_combo.set_name ("PaddedButton");
236         audio_bitrate_combo.append_text(_("(default)"));
237         audio_bitrate_combo.append_text("64k");
238         audio_bitrate_combo.append_text("128k");
239         audio_bitrate_combo.append_text("192k");
240         audio_bitrate_combo.append_text("256k");
241         audio_bitrate_combo.append_text("320k");
242
243         audio_samplerate_combo.set_name ("PaddedButton");
244         audio_samplerate_combo.append_text("22050");
245         audio_samplerate_combo.append_text("44100");
246         audio_samplerate_combo.append_text("48000");
247
248         video_bitrate_combo.set_name ("PaddedButton");
249         video_bitrate_combo.append_text(_("(default)"));
250         video_bitrate_combo.append_text(_("(retain)"));
251         video_bitrate_combo.append_text("200k");
252         video_bitrate_combo.append_text("800k");
253         video_bitrate_combo.append_text("2000k");
254         video_bitrate_combo.append_text("5000k");
255         video_bitrate_combo.append_text("8000k");
256
257         fps_combo.set_name ("PaddedButton");
258         fps_combo.append_text("23.976");
259         fps_combo.append_text("24");
260         fps_combo.append_text("24.976");
261         fps_combo.append_text("25");
262         fps_combo.append_text("29.97");
263         fps_combo.append_text("30");
264         fps_combo.append_text("59.94");
265         fps_combo.append_text("60");
266
267         aspect_combo.set_name ("PaddedButton");
268         aspect_combo.append_text("4:3");
269         aspect_combo.append_text("16:9");
270
271         vbox->pack_start (*options_box, false, true, 4);
272         get_vbox()->set_spacing (4);
273         get_vbox()->pack_start (*vbox, false, false);
274
275         progress_box = manage (new VBox);
276         progress_box->pack_start (pbar, false, false);
277         progress_box->pack_start (abort_button, false, false);
278         get_vbox()->pack_start (*progress_box, false, false);
279
280         scale_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::scale_checkbox_toggled));
281         aspect_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::aspect_checkbox_toggled));
282         fps_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::fps_checkbox_toggled));
283         preset_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::preset_combo_changed));
284         video_codec_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::video_codec_combo_changed));
285         outfn_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_outfn_dialog));
286         invid_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_invid_dialog));
287         transcode_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::launch_export));
288         abort_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::abort_clicked));
289
290         invid_path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::set_original_file_information));
291         width_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::width_value_changed));
292         height_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::height_value_changed));
293
294         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
295         get_action_area()->pack_start (transcode_button, false, false);
296         show_all_children ();
297         progress_box->hide();
298 }
299
300 ExportVideoDialog::~ExportVideoDialog ()
301 {
302         if (_transcoder) { delete _transcoder; _transcoder = 0;}
303 }
304
305 void
306 ExportVideoDialog::set_original_file_information()
307 {
308         assert(_transcoder == 0);
309         std::string infile = invid_path_entry.get_text();
310
311         if (scale_checkbox.get_active()) {
312                 // user may have set custom values already, don't touch.
313                 return;
314         }
315         if (infile == "" || !Glib::file_test(infile, Glib::FILE_TEST_EXISTS)) {
316                 return;
317         }
318
319         _transcoder = new TranscodeFfmpeg(infile);
320         if (_transcoder->probe_ok()) {
321                 _video_source_aspect_ratio = -1;
322                 width_spinner.set_value(_transcoder->get_width());
323                 height_spinner.set_value(_transcoder->get_height());
324                 _video_source_aspect_ratio = _transcoder->get_aspect();
325         }
326
327         delete _transcoder; _transcoder = 0;
328 }
329 void
330 ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
331 {
332         _suspend_dirty = true; // TODO really just queue 'dirty' and mark session dirty on "Export"
333
334         export_range = tme;
335         _video_source_aspect_ratio = -1;
336
337         outfn_path_entry.set_text (_session->session_directory().export_path() + G_DIR_SEPARATOR +"export.avi");
338
339         // TODO remember setting for export-range.. somehow, (let explicit range override)
340         frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
341         if (av_offset < 0 ) {
342                 insnd_combo.append_text (_("from 00:00:00:00 to the video's end"));
343         } else {
344                 insnd_combo.append_text (_("from the video's start to the video's end"));
345         }
346         if (!export_range.empty()) {
347                 insnd_combo.append_text (_("Selected range"));  // TODO show export_range.start() -> export_range.end_frame()
348         }
349         if (range) {
350                 insnd_combo.set_active(2);
351         } else {
352                 insnd_combo.set_active(0);
353         }
354
355         preset_combo.set_active(0);
356         audio_codec_combo.set_active(0);
357         video_codec_combo.set_active(0);
358         audio_bitrate_combo.set_active(0);
359         audio_samplerate_combo.set_active(2);
360         video_bitrate_combo.set_active(0);
361         aspect_combo.set_active(1);
362
363         scale_checkbox.set_active(false);
364         scale_aspect.set_active(true);
365         aspect_checkbox.set_active(false);
366         normalize_checkbox.set_active(false);
367         twopass_checkbox.set_active(false);
368         optimizations_checkbox.set_active(false);
369         deinterlace_checkbox.set_active(false);
370         bframes_checkbox.set_active(false);
371         fps_checkbox.set_active(false);
372         meta_checkbox.set_active(false);
373
374         float tcfps = _session->timecode_frames_per_second();
375
376         LocaleGuard lg (X_("C"));
377
378         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
379         if (node) {
380                 bool filenameset = false;
381                 if (node->property(X_("OriginalVideoFile"))) {
382                         std::string filename = node->property(X_("OriginalVideoFile"))->value();
383                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
384                                 invid_path_entry.set_text (filename);
385                                 filenameset = true;
386                         }
387                 }
388                 if (!filenameset
389                                 && node->property(X_("Filename"))
390                                 && node->property(X_("LocalFile"))
391                                 && node->property(X_("LocalFile"))->value() == X_("1")
392                    )
393                 {
394                         std::string filename = node->property(X_("Filename"))->value();
395                         if (filename.at(0) != G_DIR_SEPARATOR)
396                         {
397                                 filename = Glib::build_filename (_session->session_directory().video_path(), filename);
398                         }
399                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS))
400                         {
401                                 invid_path_entry.set_text (filename);
402                                 filenameset = true;
403                         }
404                 }
405                 if (!filenameset) {
406                         invid_path_entry.set_text (X_(""));
407                 }
408         }
409
410         node = _session->extra_xml (X_("Videoexport"));
411         if (node) {
412                 const XMLProperty* prop;
413                 prop = node->property (X_("ChangeGeometry"));
414                 if (prop) { scale_checkbox.set_active(atoi(prop->value())?true:false); }
415                 prop = node->property (X_("KeepAspect"));
416                 if (prop) { scale_aspect.set_active(atoi(prop->value())?true:false); }
417                 prop = node->property (X_("ChangeAspect"));
418                 if (prop) { aspect_checkbox.set_active(atoi(prop->value())?true:false); }
419                 prop = node->property (X_("NormalizeAudio"));
420                 if (prop) { normalize_checkbox.set_active(atoi(prop->value())?true:false); }
421                 prop = node->property (X_("TwoPassEncode"));
422                 if (prop) { twopass_checkbox.set_active(atoi(prop->value())?true:false); }
423                 prop = node->property (X_("CodecOptimzations"));
424                 if (prop) { optimizations_checkbox.set_active(atoi(prop->value())?true:false); }
425                 prop = node->property (X_("Deinterlace"));
426                 if (prop) { deinterlace_checkbox.set_active(atoi(prop->value())?true:false); }
427                 prop = node->property (X_("BFrames"));
428                 if (prop) { bframes_checkbox.set_active(atoi(prop->value())?true:false); }
429                 prop = node->property (X_("ChangeFPS"));
430                 if (prop) { fps_checkbox.set_active(atoi(prop->value())?true:false); }
431                 prop = node->property (X_("Metadata"));
432                 if (prop) { meta_checkbox.set_active(atoi(prop->value())?true:false); }
433
434                 prop = node->property (X_("Format"));
435                 if (prop && !prop->value().empty()) { change_file_extension( "." + prop->value()); }
436
437                 _suspend_signals = true;
438                 prop = node->property (X_("Width"));
439                 if (prop) { width_spinner.set_value(atoi(prop->value())); }
440                 prop = node->property (X_("Height"));
441                 if (prop) { height_spinner.set_value(atoi(prop->value())); }
442                 _suspend_signals = false;
443
444                 prop = node->property (X_("FPS"));
445                 if (prop && fps_checkbox.get_active()) { tcfps = atof(prop->value()); }
446
447                 prop = node->property (X_("Preset"));
448                 if (prop) { preset_combo.set_active_text(prop->value()); }
449                 prop = node->property (X_("VCodec"));
450                 if (prop) { video_codec_combo.set_active_text(prop->value()); }
451                 prop = node->property (X_("ACodec"));
452                 if (prop) { audio_codec_combo.set_active_text(prop->value()); }
453                 prop = node->property (X_("VBitrate"));
454                 if (prop) { video_bitrate_combo.set_active_text(prop->value()); }
455                 prop = node->property (X_("ABitrate"));
456                 if (prop) { audio_bitrate_combo.set_active_text(prop->value()); }
457                 prop = node->property (X_("AspectRatio"));
458                 if (prop) { aspect_combo.set_active_text(prop->value()); }
459                 prop = node->property (X_("SampleRate"));
460                 if (prop) { audio_samplerate_combo.set_active_text(prop->value()); }
461         }
462
463         if      (fabs(tcfps - 23.976) < 0.01) { fps_combo.set_active(0); }
464         else if (fabs(tcfps - 24.0  ) < 0.01) { fps_combo.set_active(1); }
465         else if (fabs(tcfps - 24.976) < 0.01) { fps_combo.set_active(2); }
466         else if (fabs(tcfps - 25.0  ) < 0.01) { fps_combo.set_active(3); }
467         else if (fabs(tcfps - 29.97 ) < 0.01) { fps_combo.set_active(4); }
468         else if (fabs(tcfps - 30.0  ) < 0.01) { fps_combo.set_active(5); }
469         else if (fabs(tcfps - 59.94 ) < 0.01) { fps_combo.set_active(6); }
470         else if (fabs(tcfps - 60.0  ) < 0.01) { fps_combo.set_active(7); }
471         else { fps_combo.set_active(5); }
472
473         set_original_file_information();
474
475         /* update sensitivity */
476         scale_checkbox_toggled();
477         aspect_checkbox_toggled();
478         fps_checkbox_toggled();
479         video_codec_combo_changed();
480
481         _suspend_dirty = false;
482
483         show_all_children ();
484         progress_box->hide();
485 }
486
487 XMLNode&
488 ExportVideoDialog::get_state ()
489 {
490         LocaleGuard lg (X_("C"));
491         XMLNode* node = new XMLNode (X_("Videoexport"));
492         node->add_property (X_("ChangeGeometry"), scale_checkbox.get_active() ? X_("1") : X_("0"));
493         node->add_property (X_("KeepAspect"), scale_aspect.get_active() ? X_("1") : X_("0"));
494         node->add_property (X_("ChangeAspect"), aspect_checkbox.get_active() ? X_("1") : X_("0"));
495         node->add_property (X_("NormalizeAudio"), normalize_checkbox.get_active() ? X_("1") : X_("0"));
496         node->add_property (X_("TwoPassEncode"), twopass_checkbox.get_active() ? X_("1") : X_("0"));
497         node->add_property (X_("CodecOptimzations"), optimizations_checkbox.get_active() ? X_("1") : X_("0"));
498         node->add_property (X_("Deinterlace"), deinterlace_checkbox.get_active() ? X_("1") : X_("0"));
499         node->add_property (X_("BFrames"), bframes_checkbox.get_active() ? X_("1") : X_("0"));
500         node->add_property (X_("ChangeFPS"), fps_checkbox.get_active() ? X_("1") : X_("0"));
501         node->add_property (X_("Metadata"), meta_checkbox.get_active() ? X_("1") : X_("0"));
502
503         node->add_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
504
505         node->add_property (X_("Width"), width_spinner.get_value());
506         node->add_property (X_("Height"), height_spinner.get_value());
507
508         node->add_property (X_("Preset"), preset_combo.get_active_text());
509         node->add_property (X_("VCodec"), video_codec_combo.get_active_text());
510         node->add_property (X_("ACodec"), audio_codec_combo.get_active_text());
511         node->add_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
512         node->add_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
513         node->add_property (X_("AspectRatio"), aspect_combo.get_active_text());
514         node->add_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
515         node->add_property (X_("FPS"), fps_combo.get_active_text());
516
517         return *node;
518 }
519
520 void
521 ExportVideoDialog::set_state (const XMLNode &)
522 {
523 }
524
525 void
526 ExportVideoDialog::on_show ()
527 {
528         Dialog::on_show ();
529 }
530
531 bool
532 ExportVideoDialog::on_focus_in_event (GdkEventFocus *ev)
533 {
534         Dialog::on_focus_in_event (ev);
535         Gtkmm2ext::Keyboard::magic_widget_grab_focus ();
536         return true;
537 }
538
539 bool
540 ExportVideoDialog::on_focus_out_event (GdkEventFocus *ev)
541 {
542         Dialog::on_focus_out_event (ev);
543         Gtkmm2ext::Keyboard::magic_widget_drop_focus ();
544         return true;
545 }
546
547 void
548 ExportVideoDialog::abort_clicked ()
549 {
550         _aborted = true;
551         if (_transcoder) {
552                 _transcoder->cancel();
553         }
554 }
555
556 void
557 ExportVideoDialog::update_progress (framecnt_t c, framecnt_t a)
558 {
559         if (a == 0 || c > a) {
560                 pbar.set_pulse_step(.1);
561                 pbar.pulse();
562         } else {
563                 double progress = (double)c / (double) a;
564                 progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
565                 if (_normalize && _twopass) progress += (_firstpass ? .5 : .75);
566                 else if (_normalize) progress += 2.0/3.0;
567                 else if (_twopass) progress += (_firstpass ? 1.0/3.0 : 2.0/3.0);
568                 else progress += .5;
569
570                 pbar.set_fraction (progress);
571         }
572 }
573
574
575 gint
576 ExportVideoDialog::audio_progress_display ()
577 {
578         std::string status_text;
579         double progress = 0.0;
580                 if (status->normalizing) {
581                         pbar.set_text (_("Normalizing audio"));
582                         progress = ((float) status->current_normalize_cycle) / status->total_normalize_cycles;
583                         progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0/3.0);
584                 } else {
585                         pbar.set_text (_("Exporting audio"));
586                         progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
587                         progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
588                 }
589                 if (progress < _previous_progress) {
590                         // Work around gtk bug
591                         pbar.hide();
592                         pbar.show();
593                 }
594                 _previous_progress = progress;
595                 pbar.set_fraction (progress);
596         return TRUE;
597 }
598
599 void
600 ExportVideoDialog::finished ()
601 {
602         if (_aborted) {
603                 ::g_unlink(outfn_path_entry.get_text().c_str());
604                 ::g_unlink (_insnd.c_str());
605                 delete _transcoder; _transcoder = 0;
606                 Gtk::Dialog::response(RESPONSE_CANCEL);
607         } else if (_twopass && _firstpass) {
608                 _firstpass = false;
609                 if (_transcoder) { delete _transcoder; _transcoder = 0;}
610                 encode_pass(2);
611         } else {
612                 if (twopass_checkbox.get_active()) {
613                         std::string outfn = outfn_path_entry.get_text();
614                         std::string p2log = Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
615                         ::g_unlink (p2log.c_str());
616                 }
617                 ::g_unlink (_insnd.c_str());
618                 delete _transcoder; _transcoder = 0;
619                 Gtk::Dialog::response(RESPONSE_ACCEPT);
620         }
621 }
622
623 void
624 ExportVideoDialog::launch_export ()
625 {
626         /* remember current settings.
627          * needed because apply_state() acts on both:
628          * "Videotimeline" and "Video Export" extra XML
629          * as well as current _session settings
630          */
631         _session->add_extra_xml (get_state());
632
633         std::string outfn = outfn_path_entry.get_text();
634         if (!confirm_video_outfn(outfn)) { return; }
635
636         vbox->hide();
637         cancel_button->hide();
638         transcode_button.hide();
639         pbar.set_size_request(300,-1);
640         pbar.set_text(_("Exporting Audio..."));
641         progress_box->show();
642         _aborted = false;
643         _twopass = twopass_checkbox.get_active();
644         _firstpass = true;
645         _normalize = normalize_checkbox.get_active();
646
647         /* export audio track */
648         ExportTimespanPtr tsp = _session->get_export_handler()->add_timespan();
649         boost::shared_ptr<ExportChannelConfiguration> ccp = _session->get_export_handler()->add_channel_config();
650         boost::shared_ptr<ARDOUR::ExportFilename> fnp = _session->get_export_handler()->add_filename();
651         boost::shared_ptr<AudioGrapher::BroadcastInfo> b;
652         XMLTree tree;
653         std::string vtl_samplerate = audio_samplerate_combo.get_active_text();
654         std::string vtl_normalize = _normalize ? "true" : "false";
655         tree.read_buffer(std::string(
656 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
657 "<ExportFormatSpecification name=\"VTL-WAV-16\" id=\"3094591e-ccb9-4385-a93f-c9955ffeb1f0\">"
658 "  <Encoding id=\"F_WAV\" type=\"T_Sndfile\" extension=\"wav\" name=\"WAV\" has-sample-format=\"true\" channel-limit=\"256\"/>"
659 "  <SampleRate rate=\""+ vtl_samplerate +"\"/>"
660 "  <SRCQuality quality=\"SRC_SincBest\"/>"
661 "  <EncodingOptions>"
662 "    <Option name=\"sample-format\" value=\"SF_16\"/>"
663 "    <Option name=\"dithering\" value=\"D_None\"/>"
664 "    <Option name=\"tag-metadata\" value=\"true\"/>"
665 "    <Option name=\"tag-support\" value=\"false\"/>"
666 "    <Option name=\"broadcast-info\" value=\"false\"/>"
667 "  </EncodingOptions>"
668 "  <Processing>"
669 "    <Normalize enabled=\""+ vtl_normalize +"\" target=\"0\"/>"
670 "    <Silence>"
671 "      <Start>"
672 "        <Trim enabled=\"false\"/>"
673 "        <Add enabled=\"false\">"
674 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
675 "        </Add>"
676 "      </Start>"
677 "      <End>"
678 "        <Trim enabled=\"false\"/>"
679 "        <Add enabled=\"false\">"
680 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
681 "        </Add>"
682 "      </End>"
683 "    </Silence>"
684 "  </Processing>"
685 "</ExportFormatSpecification>"
686 ));
687         boost::shared_ptr<ExportFormatSpecification> fmp = _session->get_export_handler()->add_format(*tree.root());
688
689         /* set up range */
690         framepos_t start, end;
691         start = end = 0;
692         if (insnd_combo.get_active_row_number() == 1) {
693                 _transcoder = new TranscodeFfmpeg(invid_path_entry.get_text());
694                 if (_transcoder->probe_ok() && _transcoder->get_fps() > 0) {
695                         end = _transcoder->get_duration() * _session->nominal_frame_rate() / _transcoder->get_fps();
696                 } else {
697                         warning << _("Export Video: Cannot query duration of video-file, using duration from timeline instead.") << endmsg;
698                         end = ARDOUR_UI::instance()->video_timeline->get_duration();
699                 }
700                 if (_transcoder) {delete _transcoder; _transcoder = 0;}
701
702                 frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
703 #if 0 /* DEBUG */
704                 printf("audio-range -- AV offset: %lld\n", av_offset);
705 #endif
706                 if (av_offset > 0) {
707                         start = av_offset;
708                 }
709                 end += av_offset;
710         }
711         else if (insnd_combo.get_active_row_number() == 2) {
712                 start = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.start());
713                 end   = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.end_frame());
714         }
715         if (end <= 0) {
716                 start = _session->current_start_frame();
717                 end   = _session->current_end_frame();
718         }
719 #if 0 /* DEBUG */
720         printf("audio export-range %lld -> %lld\n", start, end);
721 #endif
722
723         const frameoffset_t vstart = ARDOUR_UI::instance()->video_timeline->get_offset();
724         const frameoffset_t vend   = vstart + ARDOUR_UI::instance()->video_timeline->get_duration();
725
726         if ( (start >= end) || (end < vstart) || (start > vend)) {
727                 warning << _("Export Video: export-range does not include video.") << endmsg;
728                 delete _transcoder; _transcoder = 0;
729                 Gtk::Dialog::response(RESPONSE_CANCEL);
730                 return;
731         }
732
733         tsp->set_range (start, end);
734         tsp->set_name ("mysession");
735         tsp->set_range_id ("session");
736
737         /* add master outs as default */
738         IO* master_out = _session->master_out()->output().get();
739         if (!master_out) {
740                 warning << _("Export Video: No Master Out Ports to Connect for Audio Export") << endmsg;
741                 delete _transcoder; _transcoder = 0;
742                 Gtk::Dialog::response(RESPONSE_CANCEL);
743                 return;
744         }
745         for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
746                 PortExportChannel * channel = new PortExportChannel ();
747                 channel->add_port (master_out->audio (n));
748                 ExportChannelPtr chan_ptr (channel);
749                 ccp->register_channel (chan_ptr);
750         }
751
752         /* outfile */
753         fnp->set_timespan(tsp);
754         fnp->set_label("vtl");
755         fnp->include_label = true;
756         _insnd = fnp->get_path(fmp);
757
758         /* do sound export */
759         fmp->set_soundcloud_upload(false);
760         _session->get_export_handler()->add_export_config (tsp, ccp, fmp, fnp, b);
761         _session->get_export_handler()->do_export();
762         status = _session->get_export_status ();
763
764         audio_progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportVideoDialog::audio_progress_display), 100);
765         _previous_progress = 0.0;
766         while (status->running) {
767                 if (_aborted) { status->abort(); }
768                 if (gtk_events_pending()) {
769                         gtk_main_iteration ();
770                 } else {
771                         Glib::usleep (10000);
772                 }
773         }
774         audio_progress_connection.disconnect();
775         status->finish ();
776         if (status->aborted()) {
777                 ::g_unlink (_insnd.c_str());
778                 delete _transcoder; _transcoder = 0;
779                 Gtk::Dialog::response(RESPONSE_CANCEL);
780                 return;
781         }
782         pbar.set_text (_("Encoding Video..."));
783         encode_pass(1);
784 }
785
786 void
787 ExportVideoDialog::encode_pass (int pass)
788 {
789         std::string outfn = outfn_path_entry.get_text();
790         std::string invid = invid_path_entry.get_text();
791
792         _transcoder = new TranscodeFfmpeg(invid);
793         if (!_transcoder->ffexec_ok()) {
794                 /* ffmpeg binary was not found. TranscodeFfmpeg prints a warning */
795                 ::g_unlink (_insnd.c_str());
796                 delete _transcoder; _transcoder = 0;
797                 Gtk::Dialog::response(RESPONSE_CANCEL);
798                 return;
799         }
800         if (!_transcoder->probe_ok()) {
801                 /* video input file can not be read */
802                 warning << _("Export Video: Video input file cannot be read.") << endmsg;
803                 ::g_unlink (_insnd.c_str());
804                 delete _transcoder; _transcoder = 0;
805                 Gtk::Dialog::response(RESPONSE_CANCEL);
806                 return;
807         }
808
809         std::string preset = preset_combo.get_active_text();
810         TranscodeFfmpeg::FFSettings ffs ; /* = transcoder->default_encoder_settings(); */
811         ffs.clear();
812
813         if (fps_checkbox.get_active()) {
814                 ffs["-r"] = fps_combo.get_active_text();
815                 _transcoder->set_fps(atof(fps_combo.get_active_text()));
816         }
817
818         if (scale_checkbox.get_active()) {
819                 ffs["-s"] = string_compose("%1x%2", width_spinner.get_value(), height_spinner.get_value());
820         }
821
822         if (video_codec_combo.get_active_text() != _("(default for format)")) {
823                 ffs["-vcodec"] = video_codec_combo.get_active_text();
824         }
825         if (audio_codec_combo.get_active_text() != _("(default for format)")) {
826                 ffs["-acodec"] = audio_codec_combo.get_active_text();
827         }
828
829         if (video_bitrate_combo.get_active_text() == _("(default)") ) {
830                 ;
831         }
832         else if (video_bitrate_combo.get_active_text() == _("(retain)") ) {
833                 ffs["-qscale"]  = "0";
834         } else {
835                 ffs["-b:v"]  = video_bitrate_combo.get_active_text();
836         }
837
838         if (audio_bitrate_combo.get_active_text() != _("(default)") ) {
839                 ffs["-b:a"] = audio_bitrate_combo.get_active_text();
840         }
841
842         if (audio_codec_combo.get_active_text() == "aac" ) {
843                 ffs["-strict"] = "-2";
844         }
845
846         if (video_codec_combo.get_active_text() == "h264" ) {
847                 ffs["-vcodec"] = "libx264";
848         }
849         else if (video_codec_combo.get_active_text() == "vpx (webm)" ) {
850                 ffs["-vcodec"] = "libvpx";
851                 ffs["-g"] = "120";
852                 ffs["-qmin"] = "11";
853                 ffs["-qmax"] = "51";
854         }
855
856         if (optimizations_checkbox.get_active()) {
857           if (video_codec_combo.get_active_text() == "mpeg2video") {
858                         ffs["-mbd"] = "rd";
859                         ffs["-trellis"] = "2";
860                         ffs["-cmp"] = "2";
861                         ffs["-subcmp"] = "2";
862                 }
863                 else if (video_codec_combo.get_active_text() == "mpeg4") {
864                         ffs["-mbd"] = "rd";
865                         ffs["-flags"] = "+mv4+aic";
866                         ffs["-trellis"] = "2";
867                         ffs["-cmp"] = "2";
868                         ffs["-subcmp"] = "2";
869                         ffs["-g"] = "300";
870                 }
871                 else if (video_codec_combo.get_active_text() == "flv") {
872                         ffs["-mbd"] = "2";
873                         ffs["-cmp"] = "2";
874                         ffs["-subcmp"] = "2";
875                         ffs["-trellis"] = "2";
876                         ffs["-flags"] = "+aic+mv0+mv4";
877                         ffs["-g"] = "160";
878                 }
879         }
880
881         if (bframes_checkbox.get_active() && (
882                    video_codec_combo.get_active_text() == "mpeg2video"
883                 || video_codec_combo.get_active_text() == "mpeg4"
884                 )) {
885                 ffs["-bf"] = "2";
886         }
887
888         if (preset == "dvd-PAL") {
889                 ffs.clear(); /* ignore all prev settings */
890                 ffs["-target"] = "pal-dvd";
891                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
892         }
893         else if (preset == "dvd-NTSC") {
894                 ffs.clear(); /* ignore all prev settings */
895                 ffs["-target"] = "ntsc-dvd";
896                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
897         }
898
899         if (aspect_checkbox.get_active()) {
900                 ffs["-aspect"] = aspect_combo.get_active_text();
901         }
902         if (deinterlace_checkbox.get_active()) {
903                 ffs["-deinterlace"] = "-y"; // we use '-y' as dummy parameter for non key/value options
904         }
905
906         bool map = true;
907         if (pass == 1 && _twopass) {
908                 pbar.set_text (_("Encoding Video.. Pass 1/2"));
909                 map = false;
910                 ffs["-pass"] = "1";
911                 ffs["-an"] = "-y";
912                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
913                 ffs["-f"] = get_file_extension(invid).empty()?"mov":get_file_extension(invid);
914 #ifdef PLATFORM_WINDOWS
915                 outfn = "NUL";
916 #else
917                 outfn = "/dev/null";
918 #endif
919         } else if (pass == 2) {
920                 pbar.set_text (_("Encoding Video.. Pass 2/2"));
921                 ffs["-pass"] = "2";
922                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
923         }
924
925         frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
926         double duration_s  = 0;
927
928         if (insnd_combo.get_active_row_number() == 0) {
929                 /* session start to session end */
930                 framecnt_t duration_f = _session->current_end_frame() - _session->current_start_frame();
931                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
932         } else if (insnd_combo.get_active_row_number() == 2) {
933                 /* selected range */
934                 duration_s = export_range.length() / (double)_session->nominal_frame_rate();
935         } else {
936                 /* video start to end */
937                 framecnt_t duration_f = ARDOUR_UI::instance()->video_timeline->get_duration();
938                 if (av_offset < 0 ) {
939                         duration_f += av_offset;
940                 }
941                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
942         }
943
944         std::ostringstream osstream; osstream << duration_s;
945         ffs["-t"] = osstream.str();
946         _transcoder->set_duration(duration_s * _transcoder->get_fps());
947
948         if (insnd_combo.get_active_row_number() == 0 || insnd_combo.get_active_row_number() == 2) {
949                 framepos_t start, snend;
950                 const frameoffset_t vid_duration = ARDOUR_UI::instance()->video_timeline->get_duration();
951                 if (insnd_combo.get_active_row_number() == 0) {
952                         start = _session->current_start_frame();
953                         snend = _session->current_end_frame();
954                 } else {
955                         start = export_range.start();
956                         snend = export_range.end_frame();
957                 }
958
959 #if 0 /* DEBUG */
960                 printf("AV offset: %lld Vid-len: %lld Vid-end: %lld || start:%lld || end:%lld\n",
961                                 av_offset, vid_duration, av_offset+vid_duration, start, snend); // XXX
962 #endif
963
964                 if (av_offset > start && av_offset + vid_duration < snend) {
965                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(),
966                                 (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
967                 } else if (av_offset > start) {
968                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(), 0);
969                 } else if (av_offset + vid_duration < snend) {
970                         _transcoder->set_leadinout(0, (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
971                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
972                 }
973 #if 0
974                 else if (start > av_offset) {
975                         std::ostringstream osstream; osstream << ((start - av_offset) / (double)_session->nominal_frame_rate());
976                         ffs["-ss"] = osstream.str();
977                 }
978 #endif
979                 else {
980                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
981                 }
982
983         } else if (av_offset < 0) {
984                 /* from 00:00:00:00 to video-end */
985                 _transcoder->set_avoffset(av_offset / (double)_session->nominal_frame_rate());
986         }
987
988         TranscodeFfmpeg::FFSettings meta = _transcoder->default_meta_data();
989         if (meta_checkbox.get_active()) {
990                 ARDOUR::SessionMetadata * session_data = ARDOUR::SessionMetadata::Metadata();
991                 if (session_data->year() > 0 ) {
992                         std::ostringstream osstream; osstream << session_data->year();
993                         meta["year"] = osstream.str();
994                 }
995                 if (session_data->track_number() > 0 ) {
996                         std::ostringstream osstream; osstream << session_data->track_number();
997                         meta["track"] = osstream.str();
998                 }
999                 if (session_data->disc_number() > 0 ) {
1000                         std::ostringstream osstream; osstream << session_data->disc_number();
1001                         meta["disc"] = osstream.str();
1002                 }
1003                 if (!session_data->title().empty())     {meta["title"] = session_data->title();}
1004                 if (!session_data->artist().empty())    {meta["author"] = session_data->artist();}
1005                 if (!session_data->album_artist().empty()) {meta["album_artist"] = session_data->album_artist();}
1006                 if (!session_data->album().empty())     {meta["album"] = session_data->album();}
1007                 if (!session_data->genre().empty())     {meta["genre"] = session_data->genre();}
1008                 if (!session_data->composer().empty())  {meta["composer"] = session_data->composer();}
1009                 if (!session_data->comment().empty())   {meta["comment"] = session_data->comment();}
1010                 if (!session_data->copyright().empty()) {meta["copyright"] = session_data->copyright();}
1011                 if (!session_data->subtitle().empty())  {meta["description"] = session_data->subtitle();}
1012         }
1013
1014 #if 1 /* tentative debug mode */
1015         if (debug_checkbox.get_active()) {
1016                 _transcoder->set_debug(true);
1017         }
1018 #endif
1019
1020         _transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::update_progress , this, _1, _2), gui_context());
1021         _transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::finished, this), gui_context());
1022         if (!_transcoder->encode(outfn, _insnd, invid, ffs, meta, map)) {
1023                 ARDOUR_UI::instance()->popup_error(_("Transcoding failed."));
1024                 delete _transcoder; _transcoder = 0;
1025                 Gtk::Dialog::response(RESPONSE_CANCEL);
1026                 return;
1027         }
1028 }
1029
1030 void
1031 ExportVideoDialog::change_file_extension (std::string ext)
1032 {
1033         if (ext == "") return;
1034         outfn_path_entry.set_text (
1035                 strip_file_extension(outfn_path_entry.get_text()) + ext
1036         );
1037 }
1038
1039 void
1040 ExportVideoDialog::width_value_changed ()
1041 {
1042         if (_suspend_signals) {
1043                 return;
1044         }
1045         if (_session && !_suspend_dirty) _session->set_dirty ();
1046         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1047                 return;
1048         }
1049         if (_video_source_aspect_ratio <= 0) {
1050                 return;
1051         }
1052         _suspend_signals = true;
1053         height_spinner.set_value(rintf(width_spinner.get_value() / _video_source_aspect_ratio));
1054         _suspend_signals = false;
1055 }
1056
1057 void
1058 ExportVideoDialog::height_value_changed ()
1059 {
1060         if (_suspend_signals) {
1061                 return;
1062         }
1063         if (_session && !_suspend_dirty) _session->set_dirty ();
1064         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1065                 return;
1066         }
1067         if (_video_source_aspect_ratio <= 0) {
1068                 return;
1069         }
1070         _suspend_signals = true;
1071         width_spinner.set_value(rintf(height_spinner.get_value() * _video_source_aspect_ratio));
1072         _suspend_signals = false;
1073 }
1074
1075 void
1076 ExportVideoDialog::scale_checkbox_toggled ()
1077 {
1078         scale_aspect.set_sensitive(scale_checkbox.get_active());
1079         width_spinner.set_sensitive(scale_checkbox.get_active());
1080         height_spinner.set_sensitive(scale_checkbox.get_active());
1081         if (_session && !_suspend_dirty) _session->set_dirty ();
1082 }
1083
1084 void
1085 ExportVideoDialog::fps_checkbox_toggled ()
1086 {
1087         fps_combo.set_sensitive(fps_checkbox.get_active());
1088         if (_session && !_suspend_dirty) _session->set_dirty ();
1089 }
1090
1091 void
1092 ExportVideoDialog::aspect_checkbox_toggled ()
1093 {
1094         aspect_combo.set_sensitive(aspect_checkbox.get_active());
1095         if (_session && !_suspend_dirty) _session->set_dirty ();
1096 }
1097
1098 void
1099 ExportVideoDialog::video_codec_combo_changed ()
1100 {
1101         if ((  video_codec_combo.get_active_text() == "mpeg4"
1102              ||video_codec_combo.get_active_text() == "mpeg2video"
1103                         ) && !(
1104                preset_combo.get_active_text() == "dvd-PAL"
1105              ||preset_combo.get_active_text() == "dvd-NTSC"
1106            )) {
1107                 bframes_checkbox.set_sensitive(true);
1108                 optimizations_checkbox.set_sensitive(true);
1109                 if (video_codec_combo.get_active_text() == "mpeg2video") {
1110                         optimizations_label.set_text("-mbd rd -trellis 2 -cmp 2 -subcmp 2"); // mpeg2
1111                 } else if (video_codec_combo.get_active_text() == "mpeg4") {
1112                         optimizations_label.set_text("-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300"); // mpeg4
1113                 } else {
1114                         optimizations_label.set_text("-mbd 2 -cmp 2 -subcmp 2 -trellis 2 -flags +aic+mv0+mv4 -g 160"); // flv
1115                 }
1116         } else {
1117                 bframes_checkbox.set_sensitive(false);
1118                 bframes_checkbox.set_active(false);
1119                 optimizations_checkbox.set_sensitive(false);
1120                 optimizations_checkbox.set_active(false);
1121                 optimizations_label.set_text("-");
1122         }
1123         if (_session && !_suspend_dirty) _session->set_dirty ();
1124 }
1125
1126 void
1127 ExportVideoDialog::preset_combo_changed ()
1128 {
1129         std::string p = preset_combo.get_active_text();
1130         scale_checkbox.set_sensitive(true);
1131
1132         if (p == "flv") {
1133                 change_file_extension(".flv");
1134                 audio_codec_combo.set_active(2);
1135                 video_codec_combo.set_active(1);
1136                 audio_bitrate_combo.set_active(2);
1137                 video_bitrate_combo.set_active(3);
1138                 audio_samplerate_combo.set_active(1);
1139         }
1140         else if (p == "you-tube") {
1141                 change_file_extension(".avi");
1142                 audio_codec_combo.set_active(3);
1143                 video_codec_combo.set_active(6);
1144                 audio_bitrate_combo.set_active(2);
1145                 video_bitrate_combo.set_active(4);
1146                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1147                         audio_samplerate_combo.set_active(2);
1148                 } else {
1149                         audio_samplerate_combo.set_active(1);
1150                 }
1151         }
1152         else if (p == "ogg") {
1153                 change_file_extension(".ogv");
1154                 audio_codec_combo.set_active(4);
1155                 video_codec_combo.set_active(2);
1156                 audio_bitrate_combo.set_active(3);
1157                 video_bitrate_combo.set_active(4);
1158                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1159                         audio_samplerate_combo.set_active(2);
1160                 } else {
1161                         audio_samplerate_combo.set_active(1);
1162                 }
1163         }
1164         else if (p == "webm") {
1165                 change_file_extension(".webm");
1166                 audio_codec_combo.set_active(4);
1167                 video_codec_combo.set_active(7);
1168                 audio_bitrate_combo.set_active(3);
1169                 video_bitrate_combo.set_active(4);
1170                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1171                         audio_samplerate_combo.set_active(2);
1172                 } else {
1173                         audio_samplerate_combo.set_active(1);
1174                 }
1175         }
1176         else if (p == "dvd-mp2") {
1177                 change_file_extension(".mpg");
1178                 audio_codec_combo.set_active(5);
1179                 video_codec_combo.set_active(4);
1180                 audio_bitrate_combo.set_active(4);
1181                 video_bitrate_combo.set_active(5);
1182                 audio_samplerate_combo.set_active(2);
1183         }
1184         else if (p == "dvd-NTSC" || p == "dvd-PAL") {
1185                 change_file_extension(".mpg");
1186                 audio_codec_combo.set_active(6);
1187                 video_codec_combo.set_active(4);
1188                 audio_bitrate_combo.set_active(4);
1189                 video_bitrate_combo.set_active(5);
1190                 audio_samplerate_combo.set_active(2);
1191
1192                 scale_checkbox.set_active(false);
1193                 scale_checkbox.set_sensitive(false);
1194         }
1195         else if (p == "mpeg4") {
1196                 change_file_extension(".mp4");
1197                 audio_codec_combo.set_active(1);
1198                 video_codec_combo.set_active(5);
1199                 audio_bitrate_combo.set_active(4);
1200                 video_bitrate_combo.set_active(5);
1201                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1202                         audio_samplerate_combo.set_active(2);
1203                 } else {
1204                         audio_samplerate_combo.set_active(1);
1205                 }
1206         }
1207         else if (p == "mp4/h264/aac") {
1208                 change_file_extension(".mp4");
1209                 audio_codec_combo.set_active(2);
1210                 video_codec_combo.set_active(6);
1211                 audio_bitrate_combo.set_active(0);
1212                 video_bitrate_combo.set_active(0);
1213                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1214                         audio_samplerate_combo.set_active(2);
1215                 } else {
1216                         audio_samplerate_combo.set_active(1);
1217                 }
1218         }
1219
1220         if (p == "none") {
1221                 audio_codec_combo.set_sensitive(true);
1222                 video_codec_combo.set_sensitive(true);
1223                 audio_bitrate_combo.set_sensitive(true);
1224                 video_bitrate_combo.set_sensitive(true);
1225                 audio_samplerate_combo.set_sensitive(true);
1226         } else {
1227                 audio_codec_combo.set_sensitive(false);
1228                 video_codec_combo.set_sensitive(false);
1229                 audio_bitrate_combo.set_sensitive(false);
1230                 video_bitrate_combo.set_sensitive(false);
1231                 audio_samplerate_combo.set_sensitive(false);
1232         }
1233
1234         Gtk::Table *t = (Gtk::Table*) preset_combo.get_parent();
1235         Gtk::Table_Helpers::TableList c = t->children();
1236         Gtk::Table_Helpers::TableList::iterator it;
1237         if (p == "dvd-PAL" || p == "dvd-NTSC") {
1238                 for (it = c.begin(); it != c.end(); ++it) {
1239                         int row = it->get_top_attach();
1240                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1241                                 it->get_widget()->hide();
1242                         }
1243                 }
1244         } else {
1245                 for (it = c.begin(); it != c.end(); ++it) {
1246                         int row = it->get_top_attach();
1247                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1248                                 it->get_widget()->show();
1249                         }
1250                 }
1251         }
1252
1253         video_codec_combo_changed();
1254 }
1255
1256 void
1257 ExportVideoDialog::open_outfn_dialog ()
1258 {
1259         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1260         dialog.set_filename (outfn_path_entry.get_text());
1261
1262         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1263         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1264
1265         int result = dialog.run();
1266
1267         if (result == Gtk::RESPONSE_OK) {
1268                 std::string filename = dialog.get_filename();
1269
1270                 if (filename.length()) {
1271                         outfn_path_entry.set_text (filename);
1272                 }
1273         }
1274 }
1275
1276 void
1277 ExportVideoDialog::open_invid_dialog ()
1278 {
1279         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1280         dialog.set_filename (invid_path_entry.get_text());
1281
1282         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1283         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1284
1285         int result = dialog.run();
1286
1287         if (result == Gtk::RESPONSE_OK) {
1288                 std::string filename = dialog.get_filename();
1289
1290                 if (filename.length()) {
1291                         invid_path_entry.set_text (filename);
1292                 }
1293         }
1294 }