2 Copyright (C) 2010,2013 Paul Davis
3 Author: Robin Gareus <robin@gareus.org>
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.
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.
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.
26 #include <sys/types.h>
30 #include <sigc++/bind.h>
32 #include <pbd/gstdio_compat.h>
34 #include "pbd/error.h"
35 #include "pbd/convert.h"
36 #include "gtkmm2ext/utils.h"
37 #include "ardour/session_directory.h"
38 #include "ardour/profile.h"
39 #include "ardour/template_utils.h"
40 #include "ardour/session.h"
41 #include "ardour_ui.h"
42 #include "gui_thread.h"
45 #include "transcode_video_dialog.h"
46 #include "utils_videotl.h"
52 using namespace ARDOUR;
53 using namespace VideoUtils;
55 TranscodeVideoDialog::TranscodeVideoDialog (Session* s, std::string infile)
56 : ArdourDialog (_("Transcode/Import Video File "))
58 , path_label (_("Output File:"), Gtk::ALIGN_LEFT)
59 , browse_button (_("Browse"))
60 , transcode_button (_("OK"))
61 , abort_button (_("Abort"))
63 , aspect_checkbox (_("Height = "))
64 , height_adjustment (128, 0, 1920, 1, 16, 0)
65 , height_spinner (height_adjustment)
66 , bitrate_checkbox (_("Manual Override"))
67 , bitrate_adjustment (2000, 500, 10000, 10, 100, 0)
68 , bitrate_spinner (bitrate_adjustment)
69 #if 1 /* tentative debug mode */
70 , debug_checkbox (_("Debug Mode: Print ffmpeg command and output to stdout."))
75 transcoder = new TranscodeFfmpeg(infile);
77 pending_audio_extract = false;
80 set_name ("TranscodeVideoDialog");
82 set_skip_taskbar_hint (true);
83 set_resizable (false);
86 vbox = manage (new VBox);
87 VBox* options_box = manage (new VBox);
88 HBox* path_hbox = manage (new HBox);
92 TranscodeFfmpeg::FFAudioStreams as; as.clear();
94 path_hbox->pack_start (path_label, false, false, 3);
95 path_hbox->pack_start (path_entry, true, true, 3);
96 path_hbox->pack_start (browse_button, false, false, 3);
97 browse_button.set_name ("PaddedButton");
99 path_entry.set_width_chars(38);
100 height_spinner.set_sensitive(false);
101 bitrate_spinner.set_sensitive(false);
103 std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
104 std::string dstfn = video_dest_file(dstdir, infile);
105 path_entry.set_text (dstfn);
107 l = manage (new Label (_("<b>File Information</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
108 l->set_use_markup ();
109 options_box->pack_start (*l, false, true, 4);
112 if (!transcoder->ffexec_ok()) {
113 l = manage (new Label (_("No ffprobe or ffmpeg executables could be found on this system. Video Import is not possible until you install those tools. See the Log window for more information."), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
115 options_box->pack_start (*l, false, true, 4);
116 aspect_checkbox.set_sensitive(false);
117 bitrate_checkbox.set_sensitive(false);
119 else if (!transcoder->probe_ok()) {
120 l = manage (new Label (string_compose(_("File-info can not be read. Most likely '%1' is not a valid video-file or an unsupported video codec or format."), infn), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
121 options_box->pack_start (*l, false, true, 4);
122 aspect_checkbox.set_sensitive(false);
123 bitrate_checkbox.set_sensitive(false);
125 w = transcoder->get_width();
126 h = transcoder->get_height();
127 as = transcoder->get_audio();
128 m_aspect = transcoder->get_aspect();
130 if (w > 0 && h > 0 && transcoder->get_fps() > 0 && transcoder->get_duration() > 0) {
134 Table* t = manage (new Table (4, 2));
136 options_box->pack_start (*t, true, true, 4);
137 l = manage (new Label (_("FPS:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
138 t->attach (*l, 0, 1, 0, 1);
139 l = manage (new Label (_("Duration:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
140 t->attach (*l, 2, 3, 0, 1);
141 l = manage (new Label (_("Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
142 t->attach (*l, 0, 1, 1, 2);
143 l = manage (new Label (_("Geometry:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
144 t->attach (*l, 2, 3, 1, 2);
146 std::ostringstream osstream;
147 osstream << transcoder->get_fps();
148 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
149 t->attach (*l, 1, 2, 0, 1);
152 osstream << w << "x" << h;
153 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
154 t->attach (*l, 3, 4, 1, 2);
157 if (transcoder->get_duration() == 0 || transcoder->get_fps() == 0) {
160 unsigned long sec = transcoder->get_duration() / transcoder->get_fps();
161 osstream << setfill('0') << setw(2);
162 osstream << (sec / 3600) << ":";
163 osstream << setfill('0') << setw(2);
164 osstream << ((sec /60 )%60) << ":";
165 osstream << setfill('0') << setw(2);
166 osstream << (sec%60) << ":";
167 osstream << setfill('0') << setw(2);
168 osstream << (transcoder->get_duration() % (int) floor(transcoder->get_fps()));
170 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
171 t->attach (*l, 3, 4, 0, 1);
174 osstream << transcoder->get_codec();
175 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
176 t->attach (*l, 1, 2, 1, 2);
179 l = manage (new Label (_("<b>Import Settings</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
180 l->set_use_markup ();
181 options_box->pack_start (*l, false, true, 4);
183 video_combo.set_name ("PaddedButton");
186 video_combo.append_text(_("Reference From Current Location (Previously Transcoded Files Only)"));
187 video_combo.append_text(_("Import/Transcode Video to Session"));
188 video_combo.set_active(1);
190 video_combo.append_text(_("Do Not Import Video (Audio Import Only)"));
191 audio_combo.set_sensitive(true);
193 audio_combo.set_sensitive(false);
195 video_combo.set_sensitive(true);
196 transcode_button.set_sensitive(true);
197 path_entry.set_sensitive (true);
198 browse_button.set_sensitive (true);
199 } else if (as.size() > 0) {
200 video_combo.append_text(_("Do Not Import Video (Audio Import Only)"));
201 video_combo.set_active(0);
202 path_entry.set_text ("");
204 video_combo.set_sensitive(false);
205 audio_combo.set_sensitive(true);
206 transcode_button.set_sensitive(true);
207 path_entry.set_sensitive (false);
208 browse_button.set_sensitive (false);
210 video_combo.append_text(_("Do Not Import Video"));
211 video_combo.set_active(0);
212 path_entry.set_text ("");
213 video_combo.set_sensitive(false);
214 audio_combo.set_sensitive(false);
215 transcode_button.set_sensitive(false);
216 path_entry.set_sensitive (false);
217 browse_button.set_sensitive (false);
220 options_box->pack_start (video_combo, false, false, 4);
222 Table* t = manage (new Table (4, 3));
224 options_box->pack_start (*t, true, true, 4);
226 l = manage (new Label (_("Scale Video: Width = "), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
227 t->attach (*l, 0, 1, 0, 1);
228 scale_combo.set_name ("PaddedButton");
229 t->attach (scale_combo, 1, 2, 0, 1);
230 t->attach (aspect_checkbox, 2, 3, 0, 1);
231 t->attach (height_spinner, 3, 4, 0, 1);
233 scale_combo.append_text(_("Original Width"));
234 if (w > 1920) { scale_combo.append_text("1920 (hd1080)"); }
235 if (w > 1408) { scale_combo.append_text("1408 (16cif)"); }
236 if (w > 1280) { scale_combo.append_text("1280 (sxga, hd720)"); }
237 if (w > 1024) { scale_combo.append_text("1024 (xga)"); }
238 if (w > 852) { scale_combo.append_text(" 852 (hd480)"); }
239 if (w > 768) { scale_combo.append_text(" 768 (PAL)"); }
240 if (w > 720) { scale_combo.append_text(" 720 (PAL)"); }
241 if (w > 640) { scale_combo.append_text(" 640 (vga, ega)"); }
242 if (w > 352) { scale_combo.append_text(" 352 (cif)"); }
243 if (w > 320) { scale_combo.append_text(" 320 (cga, qvga)"); }
244 if (w > 176) { scale_combo.append_text(" 176 (qcif)"); }
245 scale_combo.set_active(0);
246 height_spinner.set_value(h);
248 l = manage (new Label (_("Bitrate (KBit/s):"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
249 t->attach (*l, 0, 1, 1, 2);
250 t->attach (bitrate_checkbox, 2, 3, 1, 2);
251 t->attach (bitrate_spinner, 3, 4, 1, 2);
253 l = manage (new Label (_("Extract Audio:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
254 t->attach (*l, 0, 1, 2, 3);
255 audio_combo.set_name ("PaddedButton");
256 t->attach (audio_combo, 1, 4, 2, 3);
257 if (as.size() == 0) {
258 audio_combo.append_text(_("No Audio Track Present"));
259 audio_combo.set_sensitive(false);
261 audio_combo.append_text(_("Do Not Extract Audio"));
262 for (TranscodeFfmpeg::FFAudioStreams::iterator it = as.begin(); it < as.end(); ++it) {
263 audio_combo.append_text((*it).name);
266 audio_combo.set_active(0);
268 #if 1 /* tentative debug mode */
269 options_box->pack_start (debug_checkbox, false, true, 4);
272 vbox->pack_start (*path_hbox, false, false);
273 vbox->pack_start (*options_box, false, true);
275 get_vbox()->set_spacing (4);
276 get_vbox()->pack_start (*vbox, false, false);
278 progress_box = manage (new VBox);
279 progress_box->pack_start (progress_label, false, false);
280 progress_box->pack_start (pbar, false, false);
281 progress_box->pack_start (abort_button, false, false);
282 get_vbox()->pack_start (*progress_box, false, false);
284 browse_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::open_browse_dialog));
285 transcode_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::launch_transcode));
286 abort_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::abort_clicked));
288 video_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::video_combo_changed));
289 audio_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::audio_combo_changed));
290 scale_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::scale_combo_changed));
291 aspect_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::aspect_checkbox_toggled));
292 height_spinner.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::update_bitrate));
293 bitrate_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::bitrate_checkbox_toggled));
297 cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
298 get_action_area()->pack_start (transcode_button, false, false);
299 show_all_children ();
300 progress_box->hide();
303 TranscodeVideoDialog::~TranscodeVideoDialog ()
309 TranscodeVideoDialog::on_show ()
315 TranscodeVideoDialog::abort_clicked ()
318 transcoder->cancel();
322 TranscodeVideoDialog::update_progress (framecnt_t c, framecnt_t a)
324 if (a == 0 || c > a) {
325 pbar.set_pulse_step(.5);
329 pbar.set_fraction ((double)c / (double) a);
333 TranscodeVideoDialog::finished ()
336 ::g_unlink(path_entry.get_text().c_str());
337 if (!audiofile.empty()) {
338 ::g_unlink(audiofile.c_str());
340 Gtk::Dialog::response(RESPONSE_CANCEL);
342 if (pending_audio_extract) {
345 Gtk::Dialog::response(RESPONSE_ACCEPT);
351 TranscodeVideoDialog::launch_audioonly ()
353 if (audio_combo.get_active_row_number() == 0) {
357 dialog_progress_mode();
358 #if 1 /* tentative debug mode */
359 if (debug_checkbox.get_active()) {
360 transcoder->set_debug(true);
363 transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::update_progress , this, _1, _2), gui_context());
364 transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::finished, this), gui_context());
369 TranscodeVideoDialog::launch_extract ()
371 audiofile= path_entry.get_text() + ".wav"; /* TODO: mktemp */
373 pending_audio_extract = false;
375 audio_stream = audio_combo.get_active_row_number() -1;
376 progress_label.set_text (_("Extracting Audio.."));
378 if (!transcoder->extract_audio(audiofile, _session->nominal_frame_rate(), audio_stream)) {
379 ARDOUR_UI::instance()->popup_error(_("Audio Extraction Failed."));
381 Gtk::Dialog::response(RESPONSE_CANCEL);
387 TranscodeVideoDialog::dialog_progress_mode ()
390 cancel_button->hide();
391 transcode_button.hide();
392 pbar.set_size_request(300,-1);
393 progress_box->show();
397 TranscodeVideoDialog::launch_transcode ()
399 if (video_combo.get_active_row_number() != 1) {
403 std::string outfn = path_entry.get_text();
404 if (!confirm_video_outfn(outfn, video_get_docroot(Config))) return;
405 progress_label.set_text (_("Transcoding Video.."));
406 dialog_progress_mode();
407 #if 1 /* tentative debug mode */
408 if (debug_checkbox.get_active()) {
409 transcoder->set_debug(true);
414 if (audio_combo.get_active_row_number() != 0) {
415 pending_audio_extract = true;
416 StartNextStage.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::launch_extract , this), gui_context());
419 int scale_width, scale_height, bitrate;
420 if (scale_combo.get_active_row_number() == 0 ) {
423 scale_width = atoi(scale_combo.get_active_text());
425 if (!aspect_checkbox.get_active()) {
428 scale_height = (int) floor(height_spinner.get_value());
430 if (bitrate_checkbox.get_active() ){
431 bitrate = (int) floor(bitrate_spinner.get_value());
436 transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::update_progress , this, _1, _2), gui_context());
437 transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::finished, this), gui_context());
438 if (!transcoder->transcode(outfn, scale_width, scale_height, bitrate)) {
439 ARDOUR_UI::instance()->popup_error(_("Transcoding Failed."));
440 Gtk::Dialog::response(RESPONSE_CANCEL);
446 TranscodeVideoDialog::video_combo_changed ()
448 const int i = video_combo.get_active_row_number();
450 scale_combo.set_sensitive(false);
451 aspect_checkbox.set_sensitive(false);
452 height_spinner.set_sensitive(false);
453 bitrate_checkbox.set_sensitive(false);
454 bitrate_spinner.set_sensitive(false);
456 scale_combo.set_sensitive(true);
457 aspect_checkbox.set_sensitive(true);
458 height_spinner.set_sensitive(true);
459 bitrate_checkbox.set_sensitive(true);
460 bitrate_spinner.set_sensitive(true);
462 if (i == 2 && audio_combo.get_active_row_number() == 0) {
463 audio_combo.set_active(1);
468 TranscodeVideoDialog::audio_combo_changed ()
470 if (video_combo.get_active_row_number() == 2
471 && audio_combo.get_active_row_number() == 0)
473 audio_combo.set_active(1);
478 TranscodeVideoDialog::scale_combo_changed ()
480 if (!aspect_checkbox.get_active()) {
482 if (scale_combo.get_active_row_number() == 0 ) {
483 h = transcoder->get_height();
485 h = floor(atof(scale_combo.get_active_text()) / m_aspect);
487 height_spinner.set_value(h);
493 TranscodeVideoDialog::aspect_checkbox_toggled ()
495 height_spinner.set_sensitive(aspect_checkbox.get_active());
496 scale_combo_changed();
500 TranscodeVideoDialog::bitrate_checkbox_toggled ()
502 bitrate_spinner.set_sensitive(bitrate_checkbox.get_active());
503 if (!bitrate_checkbox.get_active()) {
509 TranscodeVideoDialog::update_bitrate ()
511 double br = .7; /* avg quality - bits per pixel */
512 if (bitrate_checkbox.get_active() || !transcoder->probe_ok()) { return; }
513 br *= transcoder->get_fps();
514 br *= height_spinner.get_value();
516 if (scale_combo.get_active_row_number() == 0 ) {
517 br *= transcoder->get_width();
519 br *= atof(scale_combo.get_active_text());
522 bitrate_spinner.set_value(floor(br/10000.0)*10);
527 TranscodeVideoDialog::open_browse_dialog ()
529 Gtk::FileChooserDialog dialog(_("Save Transcoded Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
530 dialog.set_filename (path_entry.get_text());
532 dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
533 dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
535 int result = dialog.run();
537 if (result == Gtk::RESPONSE_OK) {
538 std::string filename = dialog.get_filename();
540 if (filename.length()) {
541 path_entry.set_text (filename);
546 enum VtlTranscodeOption
547 TranscodeVideoDialog::import_option() {
548 int i = video_combo.get_active_row_number();
549 return static_cast<VtlTranscodeOption>(i);