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