Optimize automation-event process splitting
[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 <gtkmm/filechooserdialog.h>
33 #include <gtkmm/stock.h>
34 #include <gtkmm/table.h>
35
36 #include "pbd/gstdio_compat.h"
37
38 #include "pbd/error.h"
39 #include "pbd/convert.h"
40 #include "gtkmm2ext/keyboard.h"
41 #include "gtkmm2ext/utils.h"
42 #include "ardour/session_directory.h"
43 #include "ardour/profile.h"
44 #include "ardour/template_utils.h"
45 #include "ardour/session.h"
46 #include "ardour_ui.h"
47 #include "gui_thread.h"
48
49 #include "ardour/export_handler.h"
50 #include "ardour/export_status.h"
51 #include "ardour/export_timespan.h"
52 #include "ardour/export_channel_configuration.h"
53 #include "ardour/export_format_specification.h"
54 #include "ardour/export_filename.h"
55 #include "ardour/route.h"
56 #include "ardour/session_metadata.h"
57 #include "ardour/broadcast_info.h"
58
59 #include "opts.h"
60 #include "export_video_dialog.h"
61 #include "utils_videotl.h"
62 #include "pbd/i18n.h"
63
64 using namespace Gtk;
65 using namespace std;
66 using namespace PBD;
67 using namespace ARDOUR;
68 using namespace VideoUtils;
69
70 ExportVideoDialog::ExportVideoDialog ()
71         : ArdourDialog (_("Export Video File "))
72         , _aborted(false)
73         , _twopass(false)
74         , _firstpass(false)
75         , _normalize(false)
76         , _previous_progress(0)
77         , _transcoder(0)
78         , _video_source_aspect_ratio(-1)
79         , _suspend_signals(false)
80         , outfn_path_label (_("File:"), Gtk::ALIGN_LEFT)
81         , outfn_browse_button (_("Browse"))
82         , invid_path_label (_("Video:"), Gtk::ALIGN_LEFT)
83         , invid_browse_button (_("Browse"))
84         , transcode_button (_("Export"))
85         , abort_button (_("Abort"))
86         , progress_box (0)
87         , scale_checkbox (_("Scale Video (W x H):"))
88         , scale_aspect (_("Retain Aspect"))
89         , width_adjustment (768, 128, 1920, 1, 16, 0)
90         , width_spinner (width_adjustment)
91         , height_adjustment (576, 128, 1920, 1, 16, 0)
92         , height_spinner (height_adjustment)
93         , aspect_checkbox (_("Set Aspect Ratio:"))
94         , normalize_checkbox (_("Normalize Audio"))
95         , twopass_checkbox (_("2 Pass Encoding"))
96         , optimizations_checkbox (_("Codec Optimizations:"))
97         , optimizations_label ("-")
98         , deinterlace_checkbox (_("Deinterlace"))
99         , bframes_checkbox (_("Use [2] B-frames (MPEG 2 or 4 only)"))
100         , fps_checkbox (_("Override FPS (Default is to retain FPS from the input video file):"))
101         , meta_checkbox (_("Include Session Metadata"))
102 #if 1 /* tentative debug mode */
103         , debug_checkbox (_("Debug Mode: Print ffmpeg command and output to stdout."))
104 #endif
105 {
106         set_name ("ExportVideoDialog");
107         set_modal (true);
108         set_skip_taskbar_hint (true);
109         set_resizable (false);
110
111         Gtk::Label* l;
112         vbox = manage (new VBox);
113         VBox* options_box = manage (new VBox);
114         HBox* path_hbox;
115
116         /* check if ffmpeg can be found */
117         _transcoder = new TranscodeFfmpeg(X_(""));
118         if (!_transcoder->ffexec_ok()) {
119                 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));
120                 l->set_line_wrap();
121                 vbox->pack_start (*l, false, false, 8);
122                 get_vbox()->pack_start (*vbox, false, false);
123                 add_button (Stock::OK, RESPONSE_CANCEL);
124                 show_all_children ();
125                 delete _transcoder; _transcoder = 0;
126                 return;
127         }
128         delete _transcoder; _transcoder = 0;
129
130         l = manage (new Label (_("<b>Output:</b> (file extension defines format)"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
131         l->set_use_markup ();
132         vbox->pack_start (*l, false, false, 4);
133
134         path_hbox = manage (new HBox);
135         path_hbox->pack_start (outfn_path_label, false, false, 3);
136         path_hbox->pack_start (outfn_path_entry, true, true, 3);
137         path_hbox->pack_start (outfn_browse_button, false, false, 3);
138         vbox->pack_start (*path_hbox, false, false, 2);
139
140         l = manage (new Label (_("<b>Input Video:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
141         l->set_use_markup ();
142         vbox->pack_start (*l, false, false, 4);
143
144         path_hbox = manage (new HBox);
145         path_hbox->pack_start (invid_path_label, false, false, 3);
146         path_hbox->pack_start (invid_path_entry, true, true, 3);
147         path_hbox->pack_start (invid_browse_button, false, false, 3);
148         vbox->pack_start (*path_hbox, false, false, 2);
149
150         path_hbox = manage (new HBox);
151         l = manage (new Label (_("Audio:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
152         path_hbox->pack_start (*l, false, false, 3);
153         l = manage (new Label (_("Master Bus"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
154         path_hbox->pack_start (*l, false, false, 2);
155         vbox->pack_start (*path_hbox, false, false, 2);
156
157         outfn_path_entry.set_width_chars(38);
158
159         l = manage (new Label (_("<b>Settings:</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
160         l->set_use_markup ();
161         options_box->pack_start (*l, false, true, 4);
162
163         Table* t = manage (new Table (4, 12));
164         t->set_spacings (4);
165         int ty = 0;
166         options_box->pack_start (*t, true, true, 4);
167         l = manage (new Label (_("Range:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
168         t->attach (*l, 0, 1, ty, ty+1);
169         t->attach (insnd_combo, 1, 4, ty, ty+1); ty++;
170         l = manage (new Label (_("Preset:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
171         t->attach (*l, 0, 1, ty, ty+1);
172         t->attach (preset_combo, 1, 4, ty, ty+1); ty++;
173         l = manage (new Label (_("Video Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
174         t->attach (*l, 0, 1, ty, ty+1);
175         t->attach (video_codec_combo, 1, 2, ty, ty+1);
176         l = manage (new Label (_("Video KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
177         t->attach (*l, 2, 3, ty, ty+1);
178         t->attach (video_bitrate_combo, 3, 4, ty, ty+1); ty++;
179         l = manage (new Label (_("Audio Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
180         t->attach (*l, 0, 1, ty, ty+1);
181         t->attach (audio_codec_combo, 1, 2, ty, ty+1);
182         l = manage (new Label (_("Audio KBit/s:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
183         t->attach (*l, 2, 3, ty, ty+1);
184         t->attach (audio_bitrate_combo, 3, 4, ty, ty+1); ty++;
185         l = manage (new Label (_("Audio Samplerate:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
186         t->attach (*l, 0, 1, ty, ty+1);
187         t->attach (audio_samplerate_combo, 1, 2, ty, ty+1);
188         t->attach (normalize_checkbox, 2, 4, ty, ty+1); ty++;
189         t->attach (scale_checkbox, 0, 1, ty, ty+1);
190         t->attach (scale_aspect, 1, 2, ty, ty+1);
191         t->attach (width_spinner, 2, 3, ty, ty+1);
192         t->attach (height_spinner, 3, 4, ty, ty+1); ty++;
193         t->attach (fps_checkbox, 0, 3, ty, ty+1);
194         t->attach (fps_combo, 3, 4, ty, ty+1); ty++;
195         t->attach (twopass_checkbox, 0, 2, ty, ty+1);
196         t->attach (aspect_checkbox, 2, 3, ty, ty+1);
197         t->attach (aspect_combo, 3, 4, ty, ty+1); ty++;
198         t->attach (bframes_checkbox, 0, 2, ty, ty+1);
199         t->attach (deinterlace_checkbox, 2, 4, ty, ty+1); ty++;
200         t->attach (meta_checkbox, 2, 4, ty, ty+1); ty++;
201         t->attach (optimizations_checkbox, 0, 1, ty, ty+1);
202         t->attach (optimizations_label, 1, 4, ty, ty+1); ty++;
203 #if 1 /* tentative debug mode */
204         t->attach (debug_checkbox, 0, 4, ty, ty+1); ty++;
205 #endif
206
207         preset_combo.append_text("none");
208         preset_combo.append_text("dvd-mp2");
209         preset_combo.append_text("dvd-NTSC");
210         preset_combo.append_text("dvd-PAL");
211         preset_combo.append_text("flv");
212         preset_combo.append_text("mpeg4");
213         preset_combo.append_text("mp4/h264/aac");
214         preset_combo.append_text("ogg");
215         preset_combo.append_text("webm");
216         preset_combo.append_text("you-tube");
217
218         audio_codec_combo.append_text(_("(default for format)"));
219         audio_codec_combo.append_text("ac3");
220         audio_codec_combo.append_text("aac");
221         audio_codec_combo.append_text("libmp3lame");
222         audio_codec_combo.append_text("libvorbis");
223         audio_codec_combo.append_text("mp2");
224         audio_codec_combo.append_text("pcm_s16le");
225
226         video_codec_combo.append_text(_("(default for format)"));
227         video_codec_combo.append_text("flv");
228         video_codec_combo.append_text("libtheora");
229         video_codec_combo.append_text("mjpeg");
230         video_codec_combo.append_text("mpeg2video");
231         video_codec_combo.append_text("mpeg4");
232         video_codec_combo.append_text("h264");
233         video_codec_combo.append_text("vpx (webm)");
234         video_codec_combo.append_text("copy");
235
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.append_text("22050");
244         audio_samplerate_combo.append_text("44100");
245         audio_samplerate_combo.append_text("48000");
246
247         video_bitrate_combo.append_text(_("(default)"));
248         video_bitrate_combo.append_text(_("(retain)"));
249         video_bitrate_combo.append_text("200k");
250         video_bitrate_combo.append_text("800k");
251         video_bitrate_combo.append_text("2000k");
252         video_bitrate_combo.append_text("5000k");
253         video_bitrate_combo.append_text("8000k");
254
255         fps_combo.append_text("23.976");
256         fps_combo.append_text("24");
257         fps_combo.append_text("24.976");
258         fps_combo.append_text("25");
259         fps_combo.append_text("29.97");
260         fps_combo.append_text("30");
261         fps_combo.append_text("59.94");
262         fps_combo.append_text("60");
263
264         aspect_combo.append_text("4:3");
265         aspect_combo.append_text("16:9");
266
267         vbox->pack_start (*options_box, false, true, 4);
268         get_vbox()->set_spacing (4);
269         get_vbox()->pack_start (*vbox, false, false);
270
271         progress_box = manage (new VBox);
272         progress_box->pack_start (pbar, false, false);
273         progress_box->pack_start (abort_button, false, false);
274         get_vbox()->pack_start (*progress_box, false, false);
275
276         scale_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::scale_checkbox_toggled));
277         aspect_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::aspect_checkbox_toggled));
278         fps_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &ExportVideoDialog::fps_checkbox_toggled));
279         preset_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::preset_combo_changed));
280         video_codec_combo.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::video_codec_combo_changed));
281         outfn_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_outfn_dialog));
282         invid_browse_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::open_invid_dialog));
283         transcode_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::launch_export));
284         abort_button.signal_clicked().connect (sigc::mem_fun (*this, &ExportVideoDialog::abort_clicked));
285
286         invid_path_entry.signal_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::set_original_file_information));
287         width_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::width_value_changed));
288         height_spinner.signal_value_changed().connect (sigc::mem_fun (*this, &ExportVideoDialog::height_value_changed));
289
290         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
291         get_action_area()->pack_start (transcode_button, false, false);
292         show_all_children ();
293         progress_box->hide();
294 }
295
296 ExportVideoDialog::~ExportVideoDialog ()
297 {
298         if (_transcoder) { delete _transcoder; _transcoder = 0;}
299 }
300
301 void
302 ExportVideoDialog::set_original_file_information()
303 {
304         assert(_transcoder == 0);
305         std::string infile = invid_path_entry.get_text();
306
307         if (scale_checkbox.get_active()) {
308                 // user may have set custom values already, don't touch.
309                 return;
310         }
311         if (infile == "" || !Glib::file_test(infile, Glib::FILE_TEST_EXISTS)) {
312                 return;
313         }
314
315         _transcoder = new TranscodeFfmpeg(infile);
316         if (_transcoder->probe_ok()) {
317                 _video_source_aspect_ratio = -1;
318                 width_spinner.set_value(_transcoder->get_width());
319                 height_spinner.set_value(_transcoder->get_height());
320                 _video_source_aspect_ratio = _transcoder->get_aspect();
321         }
322
323         delete _transcoder; _transcoder = 0;
324 }
325 void
326 ExportVideoDialog::apply_state (TimeSelection &tme, bool range)
327 {
328         _suspend_dirty = true; // TODO really just queue 'dirty' and mark session dirty on "Export"
329
330         export_range = tme;
331         _video_source_aspect_ratio = -1;
332
333         outfn_path_entry.set_text (_session->session_directory().export_path() + G_DIR_SEPARATOR +"export.avi");
334
335         // TODO remember setting for export-range.. somehow, (let explicit range override)
336         sampleoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
337
338         insnd_combo.remove_all ();
339
340         insnd_combo.append_text (_("from session start marker to session end marker"));
341
342         if (av_offset < 0 ) {
343                 insnd_combo.append_text (_("from 00:00:00:00 to the video end"));
344         } else {
345                 insnd_combo.append_text (_("from video start to video end"));
346         }
347         if (!export_range.empty()) {
348                 insnd_combo.append_text (_("Selected range"));  // TODO show export_range.start() -> export_range.end_sample()
349         }
350         if (range) {
351                 insnd_combo.set_active(2);
352         } else {
353                 insnd_combo.set_active(0);
354         }
355
356         preset_combo.set_active(0);
357         audio_codec_combo.set_active(0);
358         video_codec_combo.set_active(0);
359         audio_bitrate_combo.set_active(0);
360         audio_samplerate_combo.set_active(2);
361         video_bitrate_combo.set_active(0);
362         aspect_combo.set_active(1);
363
364         scale_checkbox.set_active(false);
365         scale_aspect.set_active(true);
366         aspect_checkbox.set_active(false);
367         normalize_checkbox.set_active(false);
368         twopass_checkbox.set_active(false);
369         optimizations_checkbox.set_active(false);
370         deinterlace_checkbox.set_active(false);
371         bframes_checkbox.set_active(false);
372         fps_checkbox.set_active(false);
373         meta_checkbox.set_active(false);
374
375         float tcfps = _session->timecode_frames_per_second();
376
377         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
378         bool filenameset = false;
379         if (node) {
380                 std::string filename;
381                 if (node->get_property(X_("OriginalVideoFile"), filename)) {
382                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
383                                 invid_path_entry.set_text (filename);
384                                 filenameset = true;
385                         }
386                 }
387
388                 bool local_file;
389
390                 if (!filenameset && node->get_property (X_("Filename"), filename) &&
391                     node->get_property (X_("LocalFile"), local_file) && local_file) {
392                         if (filename.at(0) != G_DIR_SEPARATOR)
393                         {
394                                 filename = Glib::build_filename (_session->session_directory().video_path(), filename);
395                         }
396                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS))
397                         {
398                                 invid_path_entry.set_text (filename);
399                                 filenameset = true;
400                         }
401                 }
402         }
403         if (!filenameset) {
404                 invid_path_entry.set_text (X_(""));
405         }
406
407         node = _session->extra_xml (X_("Videoexport"));
408         if (node) {
409                 bool yn;
410                 if (node->get_property (X_("ChangeGeometry"), yn)) {
411                         scale_checkbox.set_active (yn);
412                 }
413                 if (node->get_property (X_("KeepAspect"), yn)) {
414                         scale_aspect.set_active (yn);
415                 }
416                 if (node->get_property (X_("ChangeAspect"), yn)) {
417                         aspect_checkbox.set_active (yn);
418                 }
419                 if (node->get_property (X_("NormalizeAudio"), yn)) {
420                         normalize_checkbox.set_active (yn);
421                 }
422                 if (node->get_property (X_("TwoPassEncode"), yn)) {
423                         twopass_checkbox.set_active (yn);
424                 }
425                 if (node->get_property (X_("CodecOptimzations"), yn)) {
426                         optimizations_checkbox.set_active (yn);
427                 }
428                 if (node->get_property (X_("Deinterlace"), yn)) {
429                         deinterlace_checkbox.set_active (yn);
430                 }
431                 if (node->get_property (X_("BSamples"), yn)) {
432                         bframes_checkbox.set_active (yn);
433                 }
434                 if (node->get_property (X_("ChangeFPS"), yn)) {
435                         fps_checkbox.set_active (yn);
436                 }
437                 if (node->get_property (X_("Metadata"), yn)) {
438                         meta_checkbox.set_active (yn);
439                 }
440
441                 std::string str;
442                 if (node->get_property (X_("Format"), str) && !str.empty()) {
443                         change_file_extension ("." + str);
444                 }
445
446                 _suspend_signals = true;
447                 double val;
448                 if (node->get_property (X_("Width"), val)) {
449                         width_spinner.set_value (val);
450                 }
451                 if (node->get_property (X_("Height"), val)) {
452                         height_spinner.set_value (val);
453                 }
454                 _suspend_signals = false;
455
456                 if (fps_checkbox.get_active () && node->get_property (X_("FPS"), val)) {
457                         tcfps = val;
458                 }
459
460                 if (node->get_property (X_("Preset"), str)) {
461                         preset_combo.set_active_text (str);
462                 }
463                 if (node->get_property (X_("VCodec"), str)) {
464                         video_codec_combo.set_active_text (str);
465                 }
466                 if (node->get_property (X_("ACodec"), str)) {
467                         audio_codec_combo.set_active_text (str);
468                 }
469                 if (node->get_property (X_("VBitrate"), str)) {
470                         video_bitrate_combo.set_active_text (str);
471                 }
472                 if (node->get_property (X_("ABitrate"), str)) {
473                         audio_bitrate_combo.set_active_text (str);
474                 }
475                 if (node->get_property (X_("AspectRatio"), str)) {
476                         aspect_combo.set_active_text (str);
477                 }
478                 if (node->get_property (X_("SampleRate"), str)) {
479                         audio_samplerate_combo.set_active_text (str);
480                 }
481         }
482
483         if      (fabs(tcfps - 23.976) < 0.01) { fps_combo.set_active(0); }
484         else if (fabs(tcfps - 24.0  ) < 0.01) { fps_combo.set_active(1); }
485         else if (fabs(tcfps - 24.976) < 0.01) { fps_combo.set_active(2); }
486         else if (fabs(tcfps - 25.0  ) < 0.01) { fps_combo.set_active(3); }
487         else if (fabs(tcfps - 29.97 ) < 0.01) { fps_combo.set_active(4); }
488         else if (fabs(tcfps - 30.0  ) < 0.01) { fps_combo.set_active(5); }
489         else if (fabs(tcfps - 59.94 ) < 0.01) { fps_combo.set_active(6); }
490         else if (fabs(tcfps - 60.0  ) < 0.01) { fps_combo.set_active(7); }
491         else { fps_combo.set_active(5); }
492
493         set_original_file_information();
494
495         /* update sensitivity */
496         scale_checkbox_toggled();
497         aspect_checkbox_toggled();
498         fps_checkbox_toggled();
499         video_codec_combo_changed();
500
501         _suspend_dirty = false;
502
503         show_all_children ();
504         if (progress_box) {
505                 progress_box->hide();
506         }
507 }
508
509 XMLNode&
510 ExportVideoDialog::get_state ()
511 {
512         XMLNode* node = new XMLNode (X_("Videoexport"));
513         node->set_property (X_("ChangeGeometry"), scale_checkbox.get_active());
514         node->set_property (X_("KeepAspect"), scale_aspect.get_active());
515         node->set_property (X_("ChangeAspect"), aspect_checkbox.get_active());
516         node->set_property (X_("NormalizeAudio"), normalize_checkbox.get_active());
517         node->set_property (X_("TwoPassEncode"), twopass_checkbox.get_active());
518         node->set_property (X_("CodecOptimzations"), optimizations_checkbox.get_active());
519         node->set_property (X_("Deinterlace"), deinterlace_checkbox.get_active());
520         node->set_property (X_("BSamples"), bframes_checkbox.get_active());
521         node->set_property (X_("ChangeFPS"), fps_checkbox.get_active());
522         node->set_property (X_("Metadata"), meta_checkbox.get_active());
523
524         node->set_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
525
526         node->set_property (X_("Width"), width_spinner.get_value());
527         node->set_property (X_("Height"), height_spinner.get_value());
528
529         node->set_property (X_("Preset"), preset_combo.get_active_text());
530         node->set_property (X_("VCodec"), video_codec_combo.get_active_text());
531         node->set_property (X_("ACodec"), audio_codec_combo.get_active_text());
532         node->set_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
533         node->set_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
534         node->set_property (X_("AspectRatio"), aspect_combo.get_active_text());
535         node->set_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
536         node->set_property (X_("FPS"), fps_combo.get_active_text());
537
538         return *node;
539 }
540
541 void
542 ExportVideoDialog::set_state (const XMLNode &)
543 {
544 }
545
546 void
547 ExportVideoDialog::on_show ()
548 {
549         Dialog::on_show ();
550 }
551
552 void
553 ExportVideoDialog::abort_clicked ()
554 {
555         _aborted = true;
556         if (_transcoder) {
557                 _transcoder->cancel();
558         }
559 }
560
561 void
562 ExportVideoDialog::update_progress (samplecnt_t c, samplecnt_t a)
563 {
564         if (a == 0 || c > a) {
565                 pbar.set_pulse_step(.1);
566                 pbar.pulse();
567         } else {
568                 double progress = (double)c / (double) a;
569                 progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
570                 if (_normalize && _twopass) progress += (_firstpass ? .5 : .75);
571                 else if (_normalize) progress += 2.0/3.0;
572                 else if (_twopass) progress += (_firstpass ? 1.0/3.0 : 2.0/3.0);
573                 else progress += .5;
574
575                 pbar.set_fraction (progress);
576         }
577 }
578
579
580 gint
581 ExportVideoDialog::audio_progress_display ()
582 {
583         std::string status_text;
584         double progress = -1.0;
585         switch (status->active_job) {
586                 case ExportStatus::Normalizing:
587                         pbar.set_text (_("Normalizing audio"));
588                         progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
589                         progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0 / 3.0);
590                         break;
591                 case ExportStatus::Exporting:
592                         pbar.set_text (_("Exporting audio"));
593                         progress = ((float) status->processed_samples_current_timespan) / status->total_samples_current_timespan;
594                         progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
595                         break;
596                 default:
597                         pbar.set_text (_("Exporting audio"));
598                         break;
599         }
600
601         if (progress < _previous_progress) {
602                 // Work around gtk bug
603                 pbar.hide();
604                 pbar.show();
605         }
606         _previous_progress = progress;
607
608         if (progress >= 0) {
609                 pbar.set_fraction (progress);
610         } else {
611                 pbar.set_pulse_step(.1);
612                 pbar.pulse();
613         }
614         return TRUE;
615 }
616
617 void
618 ExportVideoDialog::finished ()
619 {
620         if (_aborted) {
621                 ::g_unlink(outfn_path_entry.get_text().c_str());
622                 ::g_unlink (_insnd.c_str());
623                 delete _transcoder; _transcoder = 0;
624                 Gtk::Dialog::response(RESPONSE_CANCEL);
625         } else if (_twopass && _firstpass) {
626                 _firstpass = false;
627                 if (_transcoder) { delete _transcoder; _transcoder = 0;}
628                 encode_pass(2);
629         } else {
630                 if (twopass_checkbox.get_active()) {
631                         std::string outfn = outfn_path_entry.get_text();
632                         std::string p2log = Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
633                         ::g_unlink (p2log.c_str());
634                 }
635                 ::g_unlink (_insnd.c_str());
636                 delete _transcoder; _transcoder = 0;
637                 Gtk::Dialog::response(RESPONSE_ACCEPT);
638         }
639 }
640
641 void
642 ExportVideoDialog::launch_export ()
643 {
644         /* remember current settings.
645          * needed because apply_state() acts on both:
646          * "Videotimeline" and "Video Export" extra XML
647          * as well as current _session settings
648          */
649         _session->add_extra_xml (get_state());
650
651         std::string outfn = outfn_path_entry.get_text();
652         if (!confirm_video_outfn(*this, outfn)) { return; }
653
654         vbox->hide();
655         cancel_button->hide();
656         transcode_button.hide();
657         pbar.set_size_request(300,-1);
658         pbar.set_text(_("Exporting Audio..."));
659         progress_box->show();
660         _aborted = false;
661         _twopass = twopass_checkbox.get_active();
662         _firstpass = true;
663         _normalize = normalize_checkbox.get_active();
664
665         /* export audio track */
666         ExportTimespanPtr tsp = _session->get_export_handler()->add_timespan();
667         boost::shared_ptr<ExportChannelConfiguration> ccp = _session->get_export_handler()->add_channel_config();
668         boost::shared_ptr<ARDOUR::ExportFilename> fnp = _session->get_export_handler()->add_filename();
669         boost::shared_ptr<AudioGrapher::BroadcastInfo> b;
670         XMLTree tree;
671         std::string vtl_samplerate = audio_samplerate_combo.get_active_text();
672         std::string vtl_normalize = _normalize ? "true" : "false";
673         tree.read_buffer(std::string(
674 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
675 "<ExportFormatSpecification name=\"VTL-WAV-16\" id=\"3094591e-ccb9-4385-a93f-c9955ffeb1f0\">"
676 "  <Encoding id=\"F_WAV\" type=\"T_Sndfile\" extension=\"wav\" name=\"WAV\" has-sample-format=\"true\" channel-limit=\"256\"/>"
677 "  <SampleRate rate=\""+ vtl_samplerate +"\"/>"
678 "  <SRCQuality quality=\"SRC_SincBest\"/>"
679 "  <EncodingOptions>"
680 "    <Option name=\"sample-format\" value=\"SF_16\"/>"
681 "    <Option name=\"dithering\" value=\"D_None\"/>"
682 "    <Option name=\"tag-metadata\" value=\"true\"/>"
683 "    <Option name=\"tag-support\" value=\"false\"/>"
684 "    <Option name=\"broadcast-info\" value=\"false\"/>"
685 "  </EncodingOptions>"
686 "  <Processing>"
687 "    <Normalize enabled=\""+ vtl_normalize +"\" target=\"0\"/>"
688 "    <Silence>"
689 "      <Start>"
690 "        <Trim enabled=\"false\"/>"
691 "        <Add enabled=\"false\">"
692 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
693 "        </Add>"
694 "      </Start>"
695 "      <End>"
696 "        <Trim enabled=\"false\"/>"
697 "        <Add enabled=\"false\">"
698 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
699 "        </Add>"
700 "      </End>"
701 "    </Silence>"
702 "  </Processing>"
703 "</ExportFormatSpecification>"
704 ));
705         boost::shared_ptr<ExportFormatSpecification> fmp = _session->get_export_handler()->add_format(*tree.root());
706
707         /* set up range */
708         samplepos_t start, end;
709         start = end = 0;
710         if (insnd_combo.get_active_row_number() == 1) {
711                 _transcoder = new TranscodeFfmpeg(invid_path_entry.get_text());
712                 if (_transcoder->probe_ok() && _transcoder->get_fps() > 0) {
713                         end = _transcoder->get_duration() * _session->nominal_sample_rate() / _transcoder->get_fps();
714                 } else {
715                         warning << _("Export Video: Cannot query duration of video-file, using duration from timeline instead.") << endmsg;
716                         end = ARDOUR_UI::instance()->video_timeline->get_duration();
717                 }
718                 if (_transcoder) {delete _transcoder; _transcoder = 0;}
719
720                 sampleoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
721 #if 0 /* DEBUG */
722                 printf("audio-range -- AV offset: %lld\n", av_offset);
723 #endif
724                 if (av_offset > 0) {
725                         start = av_offset;
726                 }
727                 end += av_offset;
728         }
729         else if (insnd_combo.get_active_row_number() == 2) {
730                 start = ARDOUR_UI::instance()->video_timeline->quantify_samples_to_apv(export_range.start());
731                 end   = ARDOUR_UI::instance()->video_timeline->quantify_samples_to_apv(export_range.end_sample());
732         }
733         if (end <= 0) {
734                 start = _session->current_start_sample();
735                 end   = _session->current_end_sample();
736         }
737 #if 0 /* DEBUG */
738         printf("audio export-range %lld -> %lld\n", start, end);
739 #endif
740
741         const sampleoffset_t vstart = ARDOUR_UI::instance()->video_timeline->get_offset();
742         const sampleoffset_t vend   = vstart + ARDOUR_UI::instance()->video_timeline->get_duration();
743
744         if ( (start >= end) || (end < vstart) || (start > vend)) {
745                 warning << _("Export Video: export-range does not include video.") << endmsg;
746                 delete _transcoder; _transcoder = 0;
747                 Gtk::Dialog::response(RESPONSE_CANCEL);
748                 return;
749         }
750
751         tsp->set_range (start, end);
752         tsp->set_name ("mysession");
753         tsp->set_range_id ("session");
754
755         /* add master outs as default */
756         IO* master_out = _session->master_out()->output().get();
757         if (!master_out) {
758                 warning << _("Export Video: No Master Out Ports to Connect for Audio Export") << endmsg;
759                 delete _transcoder; _transcoder = 0;
760                 Gtk::Dialog::response(RESPONSE_CANCEL);
761                 return;
762         }
763         for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
764                 PortExportChannel * channel = new PortExportChannel ();
765                 channel->add_port (master_out->audio (n));
766                 ExportChannelPtr chan_ptr (channel);
767                 ccp->register_channel (chan_ptr);
768         }
769
770         /* outfile */
771         fnp->set_timespan(tsp);
772         fnp->set_label("vtl");
773         fnp->include_label = true;
774         _insnd = fnp->get_path(fmp);
775
776         /* do sound export */
777         fmp->set_soundcloud_upload(false);
778         _session->get_export_handler()->reset ();
779         _session->get_export_handler()->add_export_config (tsp, ccp, fmp, fnp, b);
780         _session->get_export_handler()->do_export();
781         status = _session->get_export_status ();
782
783         audio_progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportVideoDialog::audio_progress_display), 100);
784         _previous_progress = 0.0;
785         while (status->running ()) {
786                 if (_aborted) { status->abort(); }
787                 if (gtk_events_pending()) {
788                         gtk_main_iteration ();
789                 } else {
790                         Glib::usleep (10000);
791                 }
792         }
793         audio_progress_connection.disconnect();
794         status->finish ();
795         if (status->aborted()) {
796                 ::g_unlink (_insnd.c_str());
797                 delete _transcoder; _transcoder = 0;
798                 Gtk::Dialog::response(RESPONSE_CANCEL);
799                 return;
800         }
801         pbar.set_text (_("Encoding Video..."));
802         encode_pass(1);
803 }
804
805 void
806 ExportVideoDialog::encode_pass (int pass)
807 {
808         std::string outfn = outfn_path_entry.get_text();
809         std::string invid = invid_path_entry.get_text();
810
811         _transcoder = new TranscodeFfmpeg(invid);
812         if (!_transcoder->ffexec_ok()) {
813                 /* ffmpeg binary was not found. TranscodeFfmpeg prints a warning */
814                 ::g_unlink (_insnd.c_str());
815                 delete _transcoder; _transcoder = 0;
816                 Gtk::Dialog::response(RESPONSE_CANCEL);
817                 return;
818         }
819         if (!_transcoder->probe_ok()) {
820                 /* video input file can not be read */
821                 warning << _("Export Video: Video input file cannot be read.") << endmsg;
822                 ::g_unlink (_insnd.c_str());
823                 delete _transcoder; _transcoder = 0;
824                 Gtk::Dialog::response(RESPONSE_CANCEL);
825                 return;
826         }
827
828         std::string preset = preset_combo.get_active_text();
829         TranscodeFfmpeg::FFSettings ffs ; /* = transcoder->default_encoder_settings(); */
830         ffs.clear();
831
832         if (fps_checkbox.get_active()) {
833                 ffs["-r"] = fps_combo.get_active_text();
834                 _transcoder->set_fps(atof(fps_combo.get_active_text()));
835         }
836
837         if (scale_checkbox.get_active()) {
838                 ffs["-s"] = string_compose("%1x%2", width_spinner.get_value(), height_spinner.get_value());
839         }
840
841         if (video_codec_combo.get_active_text() != _("(default for format)")) {
842                 ffs["-vcodec"] = video_codec_combo.get_active_text();
843         }
844         if (audio_codec_combo.get_active_text() != _("(default for format)")) {
845                 ffs["-acodec"] = audio_codec_combo.get_active_text();
846         }
847
848         if (video_bitrate_combo.get_active_text() == _("(default)") ) {
849                 ;
850         }
851         else if (video_bitrate_combo.get_active_text() == _("(retain)") ) {
852                 ffs["-qscale"]  = "0";
853         } else {
854                 ffs["-b:v"]  = video_bitrate_combo.get_active_text();
855         }
856
857         if (audio_bitrate_combo.get_active_text() != _("(default)") ) {
858                 ffs["-b:a"] = audio_bitrate_combo.get_active_text();
859         }
860
861         if (audio_codec_combo.get_active_text() == "aac" ) {
862                 ffs["-strict"] = "-2";
863         }
864
865         if (video_codec_combo.get_active_text() == "h264" ) {
866                 ffs["-vcodec"] = "libx264";
867         }
868         else if (video_codec_combo.get_active_text() == "vpx (webm)" ) {
869                 ffs["-vcodec"] = "libvpx";
870                 ffs["-g"] = "120";
871                 ffs["-qmin"] = "11";
872                 ffs["-qmax"] = "51";
873         }
874
875         if (optimizations_checkbox.get_active()) {
876           if (video_codec_combo.get_active_text() == "mpeg2video") {
877                         ffs["-mbd"] = "rd";
878                         ffs["-trellis"] = "2";
879                         ffs["-cmp"] = "2";
880                         ffs["-subcmp"] = "2";
881                 }
882                 else if (video_codec_combo.get_active_text() == "mpeg4") {
883                         ffs["-mbd"] = "rd";
884                         ffs["-flags"] = "+mv4+aic";
885                         ffs["-trellis"] = "2";
886                         ffs["-cmp"] = "2";
887                         ffs["-subcmp"] = "2";
888                         ffs["-g"] = "300";
889                 }
890                 else if (video_codec_combo.get_active_text() == "flv") {
891                         ffs["-mbd"] = "2";
892                         ffs["-cmp"] = "2";
893                         ffs["-subcmp"] = "2";
894                         ffs["-trellis"] = "2";
895                         ffs["-flags"] = "+aic+mv0+mv4";
896                         ffs["-g"] = "160";
897                 }
898         }
899
900         if (bframes_checkbox.get_active() && (
901                    video_codec_combo.get_active_text() == "mpeg2video"
902                 || video_codec_combo.get_active_text() == "mpeg4"
903                 )) {
904                 ffs["-bf"] = "2";
905         }
906
907         if (preset == "dvd-PAL") {
908                 ffs.clear(); /* ignore all prev settings */
909                 ffs["-target"] = "pal-dvd";
910                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
911         }
912         else if (preset == "dvd-NTSC") {
913                 ffs.clear(); /* ignore all prev settings */
914                 ffs["-target"] = "ntsc-dvd";
915                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
916         }
917
918         if (aspect_checkbox.get_active()) {
919                 ffs["-aspect"] = aspect_combo.get_active_text();
920         }
921         if (deinterlace_checkbox.get_active()) {
922                 ffs["-deinterlace"] = "-y"; // we use '-y' as dummy parameter for non key/value options
923         }
924
925         bool map = true;
926         if (pass == 1 && _twopass) {
927                 pbar.set_text (_("Encoding Video.. Pass 1/2"));
928                 map = false;
929                 ffs["-pass"] = "1";
930                 ffs["-an"] = "-y";
931                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
932                 ffs["-f"] = get_file_extension(invid).empty()?"mov":get_file_extension(invid);
933 #ifdef PLATFORM_WINDOWS
934                 outfn = "NUL";
935 #else
936                 outfn = "/dev/null";
937 #endif
938         } else if (pass == 2) {
939                 pbar.set_text (_("Encoding Video.. Pass 2/2"));
940                 ffs["-pass"] = "2";
941                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
942         }
943
944         sampleoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
945         double duration_s  = 0;
946
947         if (insnd_combo.get_active_row_number() == 0) {
948                 /* session start to session end */
949                 samplecnt_t duration_f = _session->current_end_sample() - _session->current_start_sample();
950                 duration_s = (double)duration_f / (double)_session->nominal_sample_rate();
951         } else if (insnd_combo.get_active_row_number() == 2) {
952                 /* selected range */
953                 duration_s = export_range.length() / (double)_session->nominal_sample_rate();
954         } else {
955                 /* video start to end */
956                 samplecnt_t duration_f = ARDOUR_UI::instance()->video_timeline->get_duration();
957                 if (av_offset < 0 ) {
958                         duration_f += av_offset;
959                 }
960                 duration_s = (double)duration_f / (double)_session->nominal_sample_rate();
961         }
962
963         std::ostringstream osstream; osstream << duration_s;
964         ffs["-t"] = osstream.str();
965         _transcoder->set_duration(duration_s * _transcoder->get_fps());
966
967         if (insnd_combo.get_active_row_number() == 0 || insnd_combo.get_active_row_number() == 2) {
968                 samplepos_t start, snend;
969                 const sampleoffset_t vid_duration = ARDOUR_UI::instance()->video_timeline->get_duration();
970                 if (insnd_combo.get_active_row_number() == 0) {
971                         start = _session->current_start_sample();
972                         snend = _session->current_end_sample();
973                 } else {
974                         start = export_range.start();
975                         snend = export_range.end_sample();
976                 }
977
978 #if 0 /* DEBUG */
979                 printf("AV offset: %lld Vid-len: %lld Vid-end: %lld || start:%lld || end:%lld\n",
980                                 av_offset, vid_duration, av_offset+vid_duration, start, snend); // XXX
981 #endif
982
983                 if (av_offset > start && av_offset + vid_duration < snend) {
984                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_sample_rate(),
985                                 (snend - (av_offset + vid_duration)) / (double)_session->nominal_sample_rate());
986                 } else if (av_offset > start) {
987                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_sample_rate(), 0);
988                 } else if (av_offset + vid_duration < snend) {
989                         _transcoder->set_leadinout(0, (snend - (av_offset + vid_duration)) / (double)_session->nominal_sample_rate());
990                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_sample_rate());
991                 }
992 #if 0
993                 else if (start > av_offset) {
994                         std::ostringstream osstream; osstream << ((start - av_offset) / (double)_session->nominal_sample_rate());
995                         ffs["-ss"] = osstream.str();
996                 }
997 #endif
998                 else {
999                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_sample_rate());
1000                 }
1001
1002         } else if (av_offset < 0) {
1003                 /* from 00:00:00:00 to video-end */
1004                 _transcoder->set_avoffset(av_offset / (double)_session->nominal_sample_rate());
1005         }
1006
1007         /* NOTE: type (MetaDataMap) == type (FFSettings) == map<string, string> */
1008         ARDOUR::SessionMetadata::MetaDataMap meta = _transcoder->default_meta_data();
1009         if (meta_checkbox.get_active()) {
1010                 ARDOUR::SessionMetadata * session_data = ARDOUR::SessionMetadata::Metadata();
1011                 session_data->av_export_tag (meta);
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_sample_rate() == 48000 || _session->nominal_sample_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_sample_rate() == 48000 || _session->nominal_sample_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_sample_rate() == 48000 || _session->nominal_sample_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_sample_rate() == 48000 || _session->nominal_sample_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_sample_rate() == 48000 || _session->nominal_sample_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 }