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