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