Remove LocaleGuards from ExportVideoDialog class
[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         XMLNode* node = _session->extra_xml (X_("Videotimeline"));
370         bool filenameset = false;
371         if (node) {
372                 std::string filename;
373                 if (node->get_property(X_("OriginalVideoFile"), filename)) {
374                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS)) {
375                                 invid_path_entry.set_text (filename);
376                                 filenameset = true;
377                         }
378                 }
379
380                 bool local_file;
381
382                 if (!filenameset && node->get_property (X_("Filename"), filename) &&
383                     node->get_property (X_("LocalFile"), local_file) && local_file) {
384                         if (filename.at(0) != G_DIR_SEPARATOR)
385                         {
386                                 filename = Glib::build_filename (_session->session_directory().video_path(), filename);
387                         }
388                         if (Glib::file_test(filename, Glib::FILE_TEST_EXISTS))
389                         {
390                                 invid_path_entry.set_text (filename);
391                                 filenameset = true;
392                         }
393                 }
394         }
395         if (!filenameset) {
396                 invid_path_entry.set_text (X_(""));
397         }
398
399         node = _session->extra_xml (X_("Videoexport"));
400         if (node) {
401                 bool yn;
402                 if (node->get_property (X_("ChangeGeometry"), yn)) {
403                         scale_checkbox.set_active (yn);
404                 }
405                 if (node->get_property (X_("KeepAspect"), yn)) {
406                         scale_aspect.set_active (yn);
407                 }
408                 if (node->get_property (X_("ChangeAspect"), yn)) {
409                         aspect_checkbox.set_active (yn);
410                 }
411                 if (node->get_property (X_("NormalizeAudio"), yn)) {
412                         normalize_checkbox.set_active (yn);
413                 }
414                 if (node->get_property (X_("TwoPassEncode"), yn)) {
415                         twopass_checkbox.set_active (yn);
416                 }
417                 if (node->get_property (X_("CodecOptimzations"), yn)) {
418                         optimizations_checkbox.set_active (yn);
419                 }
420                 if (node->get_property (X_("Deinterlace"), yn)) {
421                         deinterlace_checkbox.set_active (yn);
422                 }
423                 if (node->get_property (X_("BFrames"), yn)) {
424                         bframes_checkbox.set_active (yn);
425                 }
426                 if (node->get_property (X_("ChangeFPS"), yn)) {
427                         fps_checkbox.set_active (yn);
428                 }
429                 if (node->get_property (X_("Metadata"), yn)) {
430                         meta_checkbox.set_active (yn);
431                 }
432
433                 std::string str;
434                 if (node->get_property (X_("Format"), str) && !str.empty()) {
435                         change_file_extension ("." + str);
436                 }
437
438                 _suspend_signals = true;
439                 double val;
440                 if (node->get_property (X_("Width"), val)) {
441                         width_spinner.set_value (val);
442                 }
443                 if (node->get_property (X_("Height"), val)) {
444                         height_spinner.set_value (val);
445                 }
446                 _suspend_signals = false;
447
448                 if (fps_checkbox.get_active () && node->get_property (X_("FPS"), val)) {
449                         tcfps = val;
450                 }
451
452                 if (node->get_property (X_("Preset"), str)) {
453                         preset_combo.set_active_text (str);
454                 }
455                 if (node->get_property (X_("VCodec"), str)) {
456                         video_codec_combo.set_active_text (str);
457                 }
458                 if (node->get_property (X_("ACodec"), str)) {
459                         audio_codec_combo.set_active_text (str);
460                 }
461                 if (node->get_property (X_("VBitrate"), str)) {
462                         video_bitrate_combo.set_active_text (str);
463                 }
464                 if (node->get_property (X_("ABitrate"), str)) {
465                         audio_bitrate_combo.set_active_text (str);
466                 }
467                 if (node->get_property (X_("AspectRatio"), str)) {
468                         aspect_combo.set_active_text (str);
469                 }
470                 if (node->get_property (X_("SampleRate"), str)) {
471                         audio_samplerate_combo.set_active_text (str);
472                 }
473         }
474
475         if      (fabs(tcfps - 23.976) < 0.01) { fps_combo.set_active(0); }
476         else if (fabs(tcfps - 24.0  ) < 0.01) { fps_combo.set_active(1); }
477         else if (fabs(tcfps - 24.976) < 0.01) { fps_combo.set_active(2); }
478         else if (fabs(tcfps - 25.0  ) < 0.01) { fps_combo.set_active(3); }
479         else if (fabs(tcfps - 29.97 ) < 0.01) { fps_combo.set_active(4); }
480         else if (fabs(tcfps - 30.0  ) < 0.01) { fps_combo.set_active(5); }
481         else if (fabs(tcfps - 59.94 ) < 0.01) { fps_combo.set_active(6); }
482         else if (fabs(tcfps - 60.0  ) < 0.01) { fps_combo.set_active(7); }
483         else { fps_combo.set_active(5); }
484
485         set_original_file_information();
486
487         /* update sensitivity */
488         scale_checkbox_toggled();
489         aspect_checkbox_toggled();
490         fps_checkbox_toggled();
491         video_codec_combo_changed();
492
493         _suspend_dirty = false;
494
495         show_all_children ();
496         if (progress_box) {
497                 progress_box->hide();
498         }
499 }
500
501 XMLNode&
502 ExportVideoDialog::get_state ()
503 {
504         XMLNode* node = new XMLNode (X_("Videoexport"));
505         node->set_property (X_("ChangeGeometry"), scale_checkbox.get_active());
506         node->set_property (X_("KeepAspect"), scale_aspect.get_active());
507         node->set_property (X_("ChangeAspect"), aspect_checkbox.get_active());
508         node->set_property (X_("NormalizeAudio"), normalize_checkbox.get_active());
509         node->set_property (X_("TwoPassEncode"), twopass_checkbox.get_active());
510         node->set_property (X_("CodecOptimzations"), optimizations_checkbox.get_active());
511         node->set_property (X_("Deinterlace"), deinterlace_checkbox.get_active());
512         node->set_property (X_("BFrames"), bframes_checkbox.get_active());
513         node->set_property (X_("ChangeFPS"), fps_checkbox.get_active());
514         node->set_property (X_("Metadata"), meta_checkbox.get_active());
515
516         node->set_property (X_("Format"), get_file_extension(outfn_path_entry.get_text()));
517
518         node->set_property (X_("Width"), width_spinner.get_value());
519         node->set_property (X_("Height"), height_spinner.get_value());
520
521         node->set_property (X_("Preset"), preset_combo.get_active_text());
522         node->set_property (X_("VCodec"), video_codec_combo.get_active_text());
523         node->set_property (X_("ACodec"), audio_codec_combo.get_active_text());
524         node->set_property (X_("VBitrate"), video_bitrate_combo.get_active_text());
525         node->set_property (X_("ABitrate"), audio_bitrate_combo.get_active_text());
526         node->set_property (X_("AspectRatio"), aspect_combo.get_active_text());
527         node->set_property (X_("SampleRate"), audio_samplerate_combo.get_active_text());
528         node->set_property (X_("FPS"), fps_combo.get_active_text());
529
530         return *node;
531 }
532
533 void
534 ExportVideoDialog::set_state (const XMLNode &)
535 {
536 }
537
538 void
539 ExportVideoDialog::on_show ()
540 {
541         Dialog::on_show ();
542 }
543
544 void
545 ExportVideoDialog::abort_clicked ()
546 {
547         _aborted = true;
548         if (_transcoder) {
549                 _transcoder->cancel();
550         }
551 }
552
553 void
554 ExportVideoDialog::update_progress (framecnt_t c, framecnt_t a)
555 {
556         if (a == 0 || c > a) {
557                 pbar.set_pulse_step(.1);
558                 pbar.pulse();
559         } else {
560                 double progress = (double)c / (double) a;
561                 progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
562                 if (_normalize && _twopass) progress += (_firstpass ? .5 : .75);
563                 else if (_normalize) progress += 2.0/3.0;
564                 else if (_twopass) progress += (_firstpass ? 1.0/3.0 : 2.0/3.0);
565                 else progress += .5;
566
567                 pbar.set_fraction (progress);
568         }
569 }
570
571
572 gint
573 ExportVideoDialog::audio_progress_display ()
574 {
575         std::string status_text;
576         double progress = -1.0;
577         switch (status->active_job) {
578                 case ExportStatus::Normalizing:
579                         pbar.set_text (_("Normalizing audio"));
580                         progress = ((float) status->current_postprocessing_cycle) / status->total_postprocessing_cycles;
581                         progress = progress / (_twopass ? 4.0 : 3.0) + (_twopass ? .25 : 1.0 / 3.0);
582                         break;
583                 case ExportStatus::Exporting:
584                         pbar.set_text (_("Exporting audio"));
585                         progress = ((float) status->processed_frames_current_timespan) / status->total_frames_current_timespan;
586                         progress = progress / ((_twopass ? 2.0 : 1.0) + (_normalize ? 2.0 : 1.0));
587                         break;
588                 default:
589                         pbar.set_text (_("Exporting audio"));
590                         break;
591         }
592
593         if (progress < _previous_progress) {
594                 // Work around gtk bug
595                 pbar.hide();
596                 pbar.show();
597         }
598         _previous_progress = progress;
599
600         if (progress >= 0) {
601                 pbar.set_fraction (progress);
602         } else {
603                 pbar.set_pulse_step(.1);
604                 pbar.pulse();
605         }
606         return TRUE;
607 }
608
609 void
610 ExportVideoDialog::finished ()
611 {
612         if (_aborted) {
613                 ::g_unlink(outfn_path_entry.get_text().c_str());
614                 ::g_unlink (_insnd.c_str());
615                 delete _transcoder; _transcoder = 0;
616                 Gtk::Dialog::response(RESPONSE_CANCEL);
617         } else if (_twopass && _firstpass) {
618                 _firstpass = false;
619                 if (_transcoder) { delete _transcoder; _transcoder = 0;}
620                 encode_pass(2);
621         } else {
622                 if (twopass_checkbox.get_active()) {
623                         std::string outfn = outfn_path_entry.get_text();
624                         std::string p2log = Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
625                         ::g_unlink (p2log.c_str());
626                 }
627                 ::g_unlink (_insnd.c_str());
628                 delete _transcoder; _transcoder = 0;
629                 Gtk::Dialog::response(RESPONSE_ACCEPT);
630         }
631 }
632
633 void
634 ExportVideoDialog::launch_export ()
635 {
636         /* remember current settings.
637          * needed because apply_state() acts on both:
638          * "Videotimeline" and "Video Export" extra XML
639          * as well as current _session settings
640          */
641         _session->add_extra_xml (get_state());
642
643         std::string outfn = outfn_path_entry.get_text();
644         if (!confirm_video_outfn(*this, outfn)) { return; }
645
646         vbox->hide();
647         cancel_button->hide();
648         transcode_button.hide();
649         pbar.set_size_request(300,-1);
650         pbar.set_text(_("Exporting Audio..."));
651         progress_box->show();
652         _aborted = false;
653         _twopass = twopass_checkbox.get_active();
654         _firstpass = true;
655         _normalize = normalize_checkbox.get_active();
656
657         /* export audio track */
658         ExportTimespanPtr tsp = _session->get_export_handler()->add_timespan();
659         boost::shared_ptr<ExportChannelConfiguration> ccp = _session->get_export_handler()->add_channel_config();
660         boost::shared_ptr<ARDOUR::ExportFilename> fnp = _session->get_export_handler()->add_filename();
661         boost::shared_ptr<AudioGrapher::BroadcastInfo> b;
662         XMLTree tree;
663         std::string vtl_samplerate = audio_samplerate_combo.get_active_text();
664         std::string vtl_normalize = _normalize ? "true" : "false";
665         tree.read_buffer(std::string(
666 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
667 "<ExportFormatSpecification name=\"VTL-WAV-16\" id=\"3094591e-ccb9-4385-a93f-c9955ffeb1f0\">"
668 "  <Encoding id=\"F_WAV\" type=\"T_Sndfile\" extension=\"wav\" name=\"WAV\" has-sample-format=\"true\" channel-limit=\"256\"/>"
669 "  <SampleRate rate=\""+ vtl_samplerate +"\"/>"
670 "  <SRCQuality quality=\"SRC_SincBest\"/>"
671 "  <EncodingOptions>"
672 "    <Option name=\"sample-format\" value=\"SF_16\"/>"
673 "    <Option name=\"dithering\" value=\"D_None\"/>"
674 "    <Option name=\"tag-metadata\" value=\"true\"/>"
675 "    <Option name=\"tag-support\" value=\"false\"/>"
676 "    <Option name=\"broadcast-info\" value=\"false\"/>"
677 "  </EncodingOptions>"
678 "  <Processing>"
679 "    <Normalize enabled=\""+ vtl_normalize +"\" target=\"0\"/>"
680 "    <Silence>"
681 "      <Start>"
682 "        <Trim enabled=\"false\"/>"
683 "        <Add enabled=\"false\">"
684 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
685 "        </Add>"
686 "      </Start>"
687 "      <End>"
688 "        <Trim enabled=\"false\"/>"
689 "        <Add enabled=\"false\">"
690 "          <Duration format=\"Timecode\" hours=\"0\" minutes=\"0\" seconds=\"0\" frames=\"0\"/>"
691 "        </Add>"
692 "      </End>"
693 "    </Silence>"
694 "  </Processing>"
695 "</ExportFormatSpecification>"
696 ));
697         boost::shared_ptr<ExportFormatSpecification> fmp = _session->get_export_handler()->add_format(*tree.root());
698
699         /* set up range */
700         framepos_t start, end;
701         start = end = 0;
702         if (insnd_combo.get_active_row_number() == 1) {
703                 _transcoder = new TranscodeFfmpeg(invid_path_entry.get_text());
704                 if (_transcoder->probe_ok() && _transcoder->get_fps() > 0) {
705                         end = _transcoder->get_duration() * _session->nominal_frame_rate() / _transcoder->get_fps();
706                 } else {
707                         warning << _("Export Video: Cannot query duration of video-file, using duration from timeline instead.") << endmsg;
708                         end = ARDOUR_UI::instance()->video_timeline->get_duration();
709                 }
710                 if (_transcoder) {delete _transcoder; _transcoder = 0;}
711
712                 frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
713 #if 0 /* DEBUG */
714                 printf("audio-range -- AV offset: %lld\n", av_offset);
715 #endif
716                 if (av_offset > 0) {
717                         start = av_offset;
718                 }
719                 end += av_offset;
720         }
721         else if (insnd_combo.get_active_row_number() == 2) {
722                 start = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.start());
723                 end   = ARDOUR_UI::instance()->video_timeline->quantify_frames_to_apv(export_range.end_frame());
724         }
725         if (end <= 0) {
726                 start = _session->current_start_frame();
727                 end   = _session->current_end_frame();
728         }
729 #if 0 /* DEBUG */
730         printf("audio export-range %lld -> %lld\n", start, end);
731 #endif
732
733         const frameoffset_t vstart = ARDOUR_UI::instance()->video_timeline->get_offset();
734         const frameoffset_t vend   = vstart + ARDOUR_UI::instance()->video_timeline->get_duration();
735
736         if ( (start >= end) || (end < vstart) || (start > vend)) {
737                 warning << _("Export Video: export-range does not include video.") << endmsg;
738                 delete _transcoder; _transcoder = 0;
739                 Gtk::Dialog::response(RESPONSE_CANCEL);
740                 return;
741         }
742
743         tsp->set_range (start, end);
744         tsp->set_name ("mysession");
745         tsp->set_range_id ("session");
746
747         /* add master outs as default */
748         IO* master_out = _session->master_out()->output().get();
749         if (!master_out) {
750                 warning << _("Export Video: No Master Out Ports to Connect for Audio Export") << endmsg;
751                 delete _transcoder; _transcoder = 0;
752                 Gtk::Dialog::response(RESPONSE_CANCEL);
753                 return;
754         }
755         for (uint32_t n = 0; n < master_out->n_ports().n_audio(); ++n) {
756                 PortExportChannel * channel = new PortExportChannel ();
757                 channel->add_port (master_out->audio (n));
758                 ExportChannelPtr chan_ptr (channel);
759                 ccp->register_channel (chan_ptr);
760         }
761
762         /* outfile */
763         fnp->set_timespan(tsp);
764         fnp->set_label("vtl");
765         fnp->include_label = true;
766         _insnd = fnp->get_path(fmp);
767
768         /* do sound export */
769         fmp->set_soundcloud_upload(false);
770         _session->get_export_handler()->add_export_config (tsp, ccp, fmp, fnp, b);
771         _session->get_export_handler()->do_export();
772         status = _session->get_export_status ();
773
774         audio_progress_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &ExportVideoDialog::audio_progress_display), 100);
775         _previous_progress = 0.0;
776         while (status->running ()) {
777                 if (_aborted) { status->abort(); }
778                 if (gtk_events_pending()) {
779                         gtk_main_iteration ();
780                 } else {
781                         Glib::usleep (10000);
782                 }
783         }
784         audio_progress_connection.disconnect();
785         status->finish ();
786         if (status->aborted()) {
787                 ::g_unlink (_insnd.c_str());
788                 delete _transcoder; _transcoder = 0;
789                 Gtk::Dialog::response(RESPONSE_CANCEL);
790                 return;
791         }
792         pbar.set_text (_("Encoding Video..."));
793         encode_pass(1);
794 }
795
796 void
797 ExportVideoDialog::encode_pass (int pass)
798 {
799         std::string outfn = outfn_path_entry.get_text();
800         std::string invid = invid_path_entry.get_text();
801
802         _transcoder = new TranscodeFfmpeg(invid);
803         if (!_transcoder->ffexec_ok()) {
804                 /* ffmpeg binary was not found. TranscodeFfmpeg prints a warning */
805                 ::g_unlink (_insnd.c_str());
806                 delete _transcoder; _transcoder = 0;
807                 Gtk::Dialog::response(RESPONSE_CANCEL);
808                 return;
809         }
810         if (!_transcoder->probe_ok()) {
811                 /* video input file can not be read */
812                 warning << _("Export Video: Video input file cannot be read.") << endmsg;
813                 ::g_unlink (_insnd.c_str());
814                 delete _transcoder; _transcoder = 0;
815                 Gtk::Dialog::response(RESPONSE_CANCEL);
816                 return;
817         }
818
819         std::string preset = preset_combo.get_active_text();
820         TranscodeFfmpeg::FFSettings ffs ; /* = transcoder->default_encoder_settings(); */
821         ffs.clear();
822
823         if (fps_checkbox.get_active()) {
824                 ffs["-r"] = fps_combo.get_active_text();
825                 _transcoder->set_fps(atof(fps_combo.get_active_text()));
826         }
827
828         if (scale_checkbox.get_active()) {
829                 ffs["-s"] = string_compose("%1x%2", width_spinner.get_value(), height_spinner.get_value());
830         }
831
832         if (video_codec_combo.get_active_text() != _("(default for format)")) {
833                 ffs["-vcodec"] = video_codec_combo.get_active_text();
834         }
835         if (audio_codec_combo.get_active_text() != _("(default for format)")) {
836                 ffs["-acodec"] = audio_codec_combo.get_active_text();
837         }
838
839         if (video_bitrate_combo.get_active_text() == _("(default)") ) {
840                 ;
841         }
842         else if (video_bitrate_combo.get_active_text() == _("(retain)") ) {
843                 ffs["-qscale"]  = "0";
844         } else {
845                 ffs["-b:v"]  = video_bitrate_combo.get_active_text();
846         }
847
848         if (audio_bitrate_combo.get_active_text() != _("(default)") ) {
849                 ffs["-b:a"] = audio_bitrate_combo.get_active_text();
850         }
851
852         if (audio_codec_combo.get_active_text() == "aac" ) {
853                 ffs["-strict"] = "-2";
854         }
855
856         if (video_codec_combo.get_active_text() == "h264" ) {
857                 ffs["-vcodec"] = "libx264";
858         }
859         else if (video_codec_combo.get_active_text() == "vpx (webm)" ) {
860                 ffs["-vcodec"] = "libvpx";
861                 ffs["-g"] = "120";
862                 ffs["-qmin"] = "11";
863                 ffs["-qmax"] = "51";
864         }
865
866         if (optimizations_checkbox.get_active()) {
867           if (video_codec_combo.get_active_text() == "mpeg2video") {
868                         ffs["-mbd"] = "rd";
869                         ffs["-trellis"] = "2";
870                         ffs["-cmp"] = "2";
871                         ffs["-subcmp"] = "2";
872                 }
873                 else if (video_codec_combo.get_active_text() == "mpeg4") {
874                         ffs["-mbd"] = "rd";
875                         ffs["-flags"] = "+mv4+aic";
876                         ffs["-trellis"] = "2";
877                         ffs["-cmp"] = "2";
878                         ffs["-subcmp"] = "2";
879                         ffs["-g"] = "300";
880                 }
881                 else if (video_codec_combo.get_active_text() == "flv") {
882                         ffs["-mbd"] = "2";
883                         ffs["-cmp"] = "2";
884                         ffs["-subcmp"] = "2";
885                         ffs["-trellis"] = "2";
886                         ffs["-flags"] = "+aic+mv0+mv4";
887                         ffs["-g"] = "160";
888                 }
889         }
890
891         if (bframes_checkbox.get_active() && (
892                    video_codec_combo.get_active_text() == "mpeg2video"
893                 || video_codec_combo.get_active_text() == "mpeg4"
894                 )) {
895                 ffs["-bf"] = "2";
896         }
897
898         if (preset == "dvd-PAL") {
899                 ffs.clear(); /* ignore all prev settings */
900                 ffs["-target"] = "pal-dvd";
901                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
902         }
903         else if (preset == "dvd-NTSC") {
904                 ffs.clear(); /* ignore all prev settings */
905                 ffs["-target"] = "ntsc-dvd";
906                 ffs["-aspect"] = "4:3"; /* required for DVD - may be overridden below */
907         }
908
909         if (aspect_checkbox.get_active()) {
910                 ffs["-aspect"] = aspect_combo.get_active_text();
911         }
912         if (deinterlace_checkbox.get_active()) {
913                 ffs["-deinterlace"] = "-y"; // we use '-y' as dummy parameter for non key/value options
914         }
915
916         bool map = true;
917         if (pass == 1 && _twopass) {
918                 pbar.set_text (_("Encoding Video.. Pass 1/2"));
919                 map = false;
920                 ffs["-pass"] = "1";
921                 ffs["-an"] = "-y";
922                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
923                 ffs["-f"] = get_file_extension(invid).empty()?"mov":get_file_extension(invid);
924 #ifdef PLATFORM_WINDOWS
925                 outfn = "NUL";
926 #else
927                 outfn = "/dev/null";
928 #endif
929         } else if (pass == 2) {
930                 pbar.set_text (_("Encoding Video.. Pass 2/2"));
931                 ffs["-pass"] = "2";
932                 ffs["-passlogfile"] =  Glib::path_get_dirname (outfn) + G_DIR_SEPARATOR + "ffmpeg2pass";
933         }
934
935         frameoffset_t av_offset = ARDOUR_UI::instance()->video_timeline->get_offset();
936         double duration_s  = 0;
937
938         if (insnd_combo.get_active_row_number() == 0) {
939                 /* session start to session end */
940                 framecnt_t duration_f = _session->current_end_frame() - _session->current_start_frame();
941                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
942         } else if (insnd_combo.get_active_row_number() == 2) {
943                 /* selected range */
944                 duration_s = export_range.length() / (double)_session->nominal_frame_rate();
945         } else {
946                 /* video start to end */
947                 framecnt_t duration_f = ARDOUR_UI::instance()->video_timeline->get_duration();
948                 if (av_offset < 0 ) {
949                         duration_f += av_offset;
950                 }
951                 duration_s = (double)duration_f / (double)_session->nominal_frame_rate();
952         }
953
954         std::ostringstream osstream; osstream << duration_s;
955         ffs["-t"] = osstream.str();
956         _transcoder->set_duration(duration_s * _transcoder->get_fps());
957
958         if (insnd_combo.get_active_row_number() == 0 || insnd_combo.get_active_row_number() == 2) {
959                 framepos_t start, snend;
960                 const frameoffset_t vid_duration = ARDOUR_UI::instance()->video_timeline->get_duration();
961                 if (insnd_combo.get_active_row_number() == 0) {
962                         start = _session->current_start_frame();
963                         snend = _session->current_end_frame();
964                 } else {
965                         start = export_range.start();
966                         snend = export_range.end_frame();
967                 }
968
969 #if 0 /* DEBUG */
970                 printf("AV offset: %lld Vid-len: %lld Vid-end: %lld || start:%lld || end:%lld\n",
971                                 av_offset, vid_duration, av_offset+vid_duration, start, snend); // XXX
972 #endif
973
974                 if (av_offset > start && av_offset + vid_duration < snend) {
975                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(),
976                                 (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
977                 } else if (av_offset > start) {
978                         _transcoder->set_leadinout((av_offset - start) / (double)_session->nominal_frame_rate(), 0);
979                 } else if (av_offset + vid_duration < snend) {
980                         _transcoder->set_leadinout(0, (snend - (av_offset + vid_duration)) / (double)_session->nominal_frame_rate());
981                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
982                 }
983 #if 0
984                 else if (start > av_offset) {
985                         std::ostringstream osstream; osstream << ((start - av_offset) / (double)_session->nominal_frame_rate());
986                         ffs["-ss"] = osstream.str();
987                 }
988 #endif
989                 else {
990                         _transcoder->set_avoffset((av_offset - start) / (double)_session->nominal_frame_rate());
991                 }
992
993         } else if (av_offset < 0) {
994                 /* from 00:00:00:00 to video-end */
995                 _transcoder->set_avoffset(av_offset / (double)_session->nominal_frame_rate());
996         }
997
998         TranscodeFfmpeg::FFSettings meta = _transcoder->default_meta_data();
999         if (meta_checkbox.get_active()) {
1000                 ARDOUR::SessionMetadata * session_data = ARDOUR::SessionMetadata::Metadata();
1001                 if (session_data->year() > 0 ) {
1002                         std::ostringstream osstream; osstream << session_data->year();
1003                         meta["year"] = osstream.str();
1004                 }
1005                 if (session_data->track_number() > 0 ) {
1006                         std::ostringstream osstream; osstream << session_data->track_number();
1007                         meta["track"] = osstream.str();
1008                 }
1009                 if (session_data->disc_number() > 0 ) {
1010                         std::ostringstream osstream; osstream << session_data->disc_number();
1011                         meta["disc"] = osstream.str();
1012                 }
1013                 if (!session_data->title().empty())     {meta["title"] = session_data->title();}
1014                 if (!session_data->artist().empty())    {meta["author"] = session_data->artist();}
1015                 if (!session_data->album_artist().empty()) {meta["album_artist"] = session_data->album_artist();}
1016                 if (!session_data->album().empty())     {meta["album"] = session_data->album();}
1017                 if (!session_data->genre().empty())     {meta["genre"] = session_data->genre();}
1018                 if (!session_data->composer().empty())  {meta["composer"] = session_data->composer();}
1019                 if (!session_data->comment().empty())   {meta["comment"] = session_data->comment();}
1020                 if (!session_data->copyright().empty()) {meta["copyright"] = session_data->copyright();}
1021                 if (!session_data->subtitle().empty())  {meta["description"] = session_data->subtitle();}
1022         }
1023
1024 #if 1 /* tentative debug mode */
1025         if (debug_checkbox.get_active()) {
1026                 _transcoder->set_debug(true);
1027         }
1028 #endif
1029
1030         _transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::update_progress , this, _1, _2), gui_context());
1031         _transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&ExportVideoDialog::finished, this), gui_context());
1032         if (!_transcoder->encode(outfn, _insnd, invid, ffs, meta, map)) {
1033                 ARDOUR_UI::instance()->popup_error(_("Transcoding failed."));
1034                 delete _transcoder; _transcoder = 0;
1035                 Gtk::Dialog::response(RESPONSE_CANCEL);
1036                 return;
1037         }
1038 }
1039
1040 void
1041 ExportVideoDialog::change_file_extension (std::string ext)
1042 {
1043         if (ext == "") return;
1044         outfn_path_entry.set_text (
1045                 strip_file_extension(outfn_path_entry.get_text()) + ext
1046         );
1047 }
1048
1049 void
1050 ExportVideoDialog::width_value_changed ()
1051 {
1052         if (_suspend_signals) {
1053                 return;
1054         }
1055         if (_session && !_suspend_dirty) _session->set_dirty ();
1056         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1057                 return;
1058         }
1059         if (_video_source_aspect_ratio <= 0) {
1060                 return;
1061         }
1062         _suspend_signals = true;
1063         height_spinner.set_value(rintf(width_spinner.get_value() / _video_source_aspect_ratio));
1064         _suspend_signals = false;
1065 }
1066
1067 void
1068 ExportVideoDialog::height_value_changed ()
1069 {
1070         if (_suspend_signals) {
1071                 return;
1072         }
1073         if (_session && !_suspend_dirty) _session->set_dirty ();
1074         if (!scale_checkbox.get_active() || !scale_aspect.get_active()) {
1075                 return;
1076         }
1077         if (_video_source_aspect_ratio <= 0) {
1078                 return;
1079         }
1080         _suspend_signals = true;
1081         width_spinner.set_value(rintf(height_spinner.get_value() * _video_source_aspect_ratio));
1082         _suspend_signals = false;
1083 }
1084
1085 void
1086 ExportVideoDialog::scale_checkbox_toggled ()
1087 {
1088         scale_aspect.set_sensitive(scale_checkbox.get_active());
1089         width_spinner.set_sensitive(scale_checkbox.get_active());
1090         height_spinner.set_sensitive(scale_checkbox.get_active());
1091         if (_session && !_suspend_dirty) _session->set_dirty ();
1092 }
1093
1094 void
1095 ExportVideoDialog::fps_checkbox_toggled ()
1096 {
1097         fps_combo.set_sensitive(fps_checkbox.get_active());
1098         if (_session && !_suspend_dirty) _session->set_dirty ();
1099 }
1100
1101 void
1102 ExportVideoDialog::aspect_checkbox_toggled ()
1103 {
1104         aspect_combo.set_sensitive(aspect_checkbox.get_active());
1105         if (_session && !_suspend_dirty) _session->set_dirty ();
1106 }
1107
1108 void
1109 ExportVideoDialog::video_codec_combo_changed ()
1110 {
1111         if ((  video_codec_combo.get_active_text() == "mpeg4"
1112              ||video_codec_combo.get_active_text() == "mpeg2video"
1113                         ) && !(
1114                preset_combo.get_active_text() == "dvd-PAL"
1115              ||preset_combo.get_active_text() == "dvd-NTSC"
1116            )) {
1117                 bframes_checkbox.set_sensitive(true);
1118                 optimizations_checkbox.set_sensitive(true);
1119                 if (video_codec_combo.get_active_text() == "mpeg2video") {
1120                         optimizations_label.set_text("-mbd rd -trellis 2 -cmp 2 -subcmp 2"); // mpeg2
1121                 } else if (video_codec_combo.get_active_text() == "mpeg4") {
1122                         optimizations_label.set_text("-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -g 300"); // mpeg4
1123                 } else {
1124                         optimizations_label.set_text("-mbd 2 -cmp 2 -subcmp 2 -trellis 2 -flags +aic+mv0+mv4 -g 160"); // flv
1125                 }
1126         } else {
1127                 bframes_checkbox.set_sensitive(false);
1128                 bframes_checkbox.set_active(false);
1129                 optimizations_checkbox.set_sensitive(false);
1130                 optimizations_checkbox.set_active(false);
1131                 optimizations_label.set_text("-");
1132         }
1133         if (_session && !_suspend_dirty) _session->set_dirty ();
1134 }
1135
1136 void
1137 ExportVideoDialog::preset_combo_changed ()
1138 {
1139         std::string p = preset_combo.get_active_text();
1140         scale_checkbox.set_sensitive(true);
1141
1142         if (p == "flv") {
1143                 change_file_extension(".flv");
1144                 audio_codec_combo.set_active(2);
1145                 video_codec_combo.set_active(1);
1146                 audio_bitrate_combo.set_active(2);
1147                 video_bitrate_combo.set_active(3);
1148                 audio_samplerate_combo.set_active(1);
1149         }
1150         else if (p == "you-tube") {
1151                 change_file_extension(".avi");
1152                 audio_codec_combo.set_active(3);
1153                 video_codec_combo.set_active(6);
1154                 audio_bitrate_combo.set_active(2);
1155                 video_bitrate_combo.set_active(4);
1156                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1157                         audio_samplerate_combo.set_active(2);
1158                 } else {
1159                         audio_samplerate_combo.set_active(1);
1160                 }
1161         }
1162         else if (p == "ogg") {
1163                 change_file_extension(".ogv");
1164                 audio_codec_combo.set_active(4);
1165                 video_codec_combo.set_active(2);
1166                 audio_bitrate_combo.set_active(3);
1167                 video_bitrate_combo.set_active(4);
1168                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1169                         audio_samplerate_combo.set_active(2);
1170                 } else {
1171                         audio_samplerate_combo.set_active(1);
1172                 }
1173         }
1174         else if (p == "webm") {
1175                 change_file_extension(".webm");
1176                 audio_codec_combo.set_active(4);
1177                 video_codec_combo.set_active(7);
1178                 audio_bitrate_combo.set_active(3);
1179                 video_bitrate_combo.set_active(4);
1180                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1181                         audio_samplerate_combo.set_active(2);
1182                 } else {
1183                         audio_samplerate_combo.set_active(1);
1184                 }
1185         }
1186         else if (p == "dvd-mp2") {
1187                 change_file_extension(".mpg");
1188                 audio_codec_combo.set_active(5);
1189                 video_codec_combo.set_active(4);
1190                 audio_bitrate_combo.set_active(4);
1191                 video_bitrate_combo.set_active(5);
1192                 audio_samplerate_combo.set_active(2);
1193         }
1194         else if (p == "dvd-NTSC" || p == "dvd-PAL") {
1195                 change_file_extension(".mpg");
1196                 audio_codec_combo.set_active(6);
1197                 video_codec_combo.set_active(4);
1198                 audio_bitrate_combo.set_active(4);
1199                 video_bitrate_combo.set_active(5);
1200                 audio_samplerate_combo.set_active(2);
1201
1202                 scale_checkbox.set_active(false);
1203                 scale_checkbox.set_sensitive(false);
1204         }
1205         else if (p == "mpeg4") {
1206                 change_file_extension(".mp4");
1207                 audio_codec_combo.set_active(1);
1208                 video_codec_combo.set_active(5);
1209                 audio_bitrate_combo.set_active(4);
1210                 video_bitrate_combo.set_active(5);
1211                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1212                         audio_samplerate_combo.set_active(2);
1213                 } else {
1214                         audio_samplerate_combo.set_active(1);
1215                 }
1216         }
1217         else if (p == "mp4/h264/aac") {
1218                 change_file_extension(".mp4");
1219                 audio_codec_combo.set_active(2);
1220                 video_codec_combo.set_active(6);
1221                 audio_bitrate_combo.set_active(0);
1222                 video_bitrate_combo.set_active(0);
1223                 if (_session->nominal_frame_rate() == 48000 || _session->nominal_frame_rate() == 96000) {
1224                         audio_samplerate_combo.set_active(2);
1225                 } else {
1226                         audio_samplerate_combo.set_active(1);
1227                 }
1228         }
1229
1230         if (p == "none") {
1231                 audio_codec_combo.set_sensitive(true);
1232                 video_codec_combo.set_sensitive(true);
1233                 audio_bitrate_combo.set_sensitive(true);
1234                 video_bitrate_combo.set_sensitive(true);
1235                 audio_samplerate_combo.set_sensitive(true);
1236         } else {
1237                 audio_codec_combo.set_sensitive(false);
1238                 video_codec_combo.set_sensitive(false);
1239                 audio_bitrate_combo.set_sensitive(false);
1240                 video_bitrate_combo.set_sensitive(false);
1241                 audio_samplerate_combo.set_sensitive(false);
1242         }
1243
1244         Gtk::Table *t = (Gtk::Table*) preset_combo.get_parent();
1245         Gtk::Table_Helpers::TableList c = t->children();
1246         Gtk::Table_Helpers::TableList::iterator it;
1247         if (p == "dvd-PAL" || p == "dvd-NTSC") {
1248                 for (it = c.begin(); it != c.end(); ++it) {
1249                         int row = it->get_top_attach();
1250                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1251                                 it->get_widget()->hide();
1252                         }
1253                 }
1254         } else {
1255                 for (it = c.begin(); it != c.end(); ++it) {
1256                         int row = it->get_top_attach();
1257                         if (row == 2 || row == 3 || row== 5 || row== 6 || row == 9) {
1258                                 it->get_widget()->show();
1259                         }
1260                 }
1261         }
1262
1263         video_codec_combo_changed();
1264 }
1265
1266 void
1267 ExportVideoDialog::open_outfn_dialog ()
1268 {
1269         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1270         dialog.set_filename (outfn_path_entry.get_text());
1271
1272         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1273         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1274
1275         int result = dialog.run();
1276
1277         if (result == Gtk::RESPONSE_OK) {
1278                 std::string filename = dialog.get_filename();
1279
1280                 if (filename.length()) {
1281                         outfn_path_entry.set_text (filename);
1282                 }
1283         }
1284 }
1285
1286 void
1287 ExportVideoDialog::open_invid_dialog ()
1288 {
1289         Gtk::FileChooserDialog dialog(_("Save Exported Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
1290         dialog.set_filename (invid_path_entry.get_text());
1291
1292         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
1293         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
1294
1295         int result = dialog.run();
1296
1297         if (result == Gtk::RESPONSE_OK) {
1298                 std::string filename = dialog.get_filename();
1299
1300                 if (filename.length()) {
1301                         invid_path_entry.set_text (filename);
1302                 }
1303         }
1304 }