merge with master
[ardour.git] / gtk2_ardour / transcode_video_dialog.cc
1 /*
2     Copyright (C) 2010,2013 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 <glib/gstdio.h>
33
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"
43
44 #include "utils.h"
45 #include "opts.h"
46 #include "transcode_video_dialog.h"
47 #include "utils_videotl.h"
48 #include "i18n.h"
49
50 using namespace Gtk;
51 using namespace std;
52 using namespace PBD;
53 using namespace ARDOUR;
54 using namespace VideoUtils;
55
56 TranscodeVideoDialog::TranscodeVideoDialog (Session* s, std::string infile)
57         : ArdourDialog (_("Transcode/Import Video File "))
58         , infn (infile)
59         , path_label (_("Output File:"), Gtk::ALIGN_LEFT)
60         , browse_button (_("Browse"))
61         , transcode_button (_("OK"))
62         , abort_button (_("Abort"))
63         , progress_label ()
64         , aspect_checkbox (_("Height = "))
65         , height_adjustment (128, 0, 1920, 1, 16, 0)
66         , height_spinner (height_adjustment)
67         , bitrate_checkbox (_("Manual Override"))
68         , bitrate_adjustment (2000, 500, 10000, 10, 100, 0)
69         , bitrate_spinner (bitrate_adjustment)
70 #if 1 /* tentative debug mode */
71         , debug_checkbox (_("Debug Mode: Print ffmpeg command and output to stdout."))
72 #endif
73 {
74         set_session (s);
75
76         transcoder = new TranscodeFfmpeg(infile);
77         audiofile = "";
78         pending_audio_extract = false;
79         aborted = false;
80
81         set_name ("TranscodeVideoDialog");
82         set_modal (true);
83         set_skip_taskbar_hint (true);
84         set_resizable (false);
85
86         Gtk::Label* l;
87         vbox = manage (new VBox);
88         VBox* options_box = manage (new VBox);
89         HBox* path_hbox = manage (new HBox);
90
91         int w = 0, h = 0;
92         m_aspect = 4.0/3.0;
93         TranscodeFfmpeg::FFAudioStreams as; as.clear();
94
95         path_hbox->pack_start (path_label, false, false, 3);
96         path_hbox->pack_start (path_entry, true, true, 3);
97         path_hbox->pack_start (browse_button, false, false, 3);
98         browse_button.set_name ("PaddedButton");
99
100         path_entry.set_width_chars(38);
101         height_spinner.set_sensitive(false);
102         bitrate_spinner.set_sensitive(false);
103
104         std::string dstdir = video_dest_dir(_session->session_directory().video_path(), video_get_docroot(Config));
105         std::string dstfn  = video_dest_file(dstdir, infile);
106         path_entry.set_text (dstfn);
107
108         l = manage (new Label (_("<b>File Information</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
109         l->set_use_markup ();
110         options_box->pack_start (*l, false, true, 4);
111
112         bool ffok = false;
113         if (!transcoder->ffexec_ok()) {
114                 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                 l->set_line_wrap();
116                 options_box->pack_start (*l, false, true, 4);
117                 aspect_checkbox.set_sensitive(false);
118                 bitrate_checkbox.set_sensitive(false);
119         }
120         else if (!transcoder->probe_ok()) {
121                 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));
122                 options_box->pack_start (*l, false, true, 4);
123                 aspect_checkbox.set_sensitive(false);
124                 bitrate_checkbox.set_sensitive(false);
125         } else {
126                 ffok = true;
127                 w = transcoder->get_width();
128                 h = transcoder->get_height();
129                 as = transcoder->get_audio();
130                 m_aspect = transcoder->get_aspect();
131
132                 Table* t = manage (new Table (4, 2));
133                 t->set_spacings (4);
134                 options_box->pack_start (*t, true, true, 4);
135                 l = manage (new Label (_("FPS:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
136                 t->attach (*l, 0, 1, 0, 1);
137                 l = manage (new Label (_("Duration:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
138                 t->attach (*l, 2, 3, 0, 1);
139                 l = manage (new Label (_("Codec:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
140                 t->attach (*l, 0, 1, 1, 2);
141                 l = manage (new Label (_("Geometry:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
142                 t->attach (*l, 2, 3, 1, 2);
143
144                 std::ostringstream osstream;
145                 osstream << transcoder->get_fps();
146                 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
147                 t->attach (*l, 1, 2, 0, 1);
148
149                 osstream.str("");
150                 osstream << w << "x" << h;
151                 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
152                 t->attach (*l, 3, 4, 1, 2);
153
154                 osstream.str("");
155                 if (transcoder->get_duration() == 0 || transcoder->get_fps() == 0) {
156                         osstream << _("??");
157                 } else {
158                         unsigned long sec = transcoder->get_duration() / transcoder->get_fps();
159                         osstream << setfill('0') << setw(2);
160                         osstream << (sec / 3600) << ":";
161                         osstream << setfill('0') << setw(2);
162                         osstream << ((sec /60 )%60) << ":";
163                         osstream << setfill('0') << setw(2);
164                         osstream << (sec%60)  << ":";
165                         osstream << setfill('0') << setw(2);
166                         osstream << (transcoder->get_duration() % (int) floor(transcoder->get_fps()));
167                 }
168                 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
169                 t->attach (*l, 3, 4, 0, 1);
170
171                 osstream.str("");
172                 osstream << transcoder->get_codec();
173                 l = manage (new Label (osstream.str(), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
174                 t->attach (*l, 1, 2, 1, 2);
175         }
176
177         l = manage (new Label (_("<b>Import Settings</b>"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
178         l->set_use_markup ();
179         options_box->pack_start (*l, false, true, 4);
180
181         video_combo.set_name ("PaddedButton");
182         video_combo.append_text(_("Do Not Import Video"));
183         video_combo.append_text(_("Reference From Current Location"));
184         if (ffok)  {
185                 video_combo.append_text(_("Import/Transcode Video to Session"));
186                 video_combo.set_active(2);
187         } else {
188                 video_combo.set_active(1);
189                 video_combo.set_sensitive(false);
190                 audio_combo.set_sensitive(false);
191         }
192
193         options_box->pack_start (video_combo, false, false, 4);
194
195         Table* t = manage (new Table (4, 3));
196         t->set_spacings (4);
197         options_box->pack_start (*t, true, true, 4);
198
199         l = manage (new Label (_("Scale Video: Width = "), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
200         t->attach (*l, 0, 1, 0, 1);
201         scale_combo.set_name ("PaddedButton");
202         t->attach (scale_combo, 1, 2, 0, 1);
203         t->attach (aspect_checkbox, 2, 3, 0, 1);
204         t->attach (height_spinner, 3, 4, 0, 1);
205
206         scale_combo.append_text(_("Original Width"));
207         if (w > 1920) { scale_combo.append_text("1920 (hd1080)"); }
208         if (w > 1408) { scale_combo.append_text("1408 (16cif)"); }
209         if (w > 1280) { scale_combo.append_text("1280 (sxga, hd720)"); }
210         if (w > 1024) { scale_combo.append_text("1024 (xga)"); }
211         if (w > 852)  { scale_combo.append_text(" 852 (hd480)"); }
212         if (w > 768)  { scale_combo.append_text(" 768 (PAL)"); }
213         if (w > 720)  { scale_combo.append_text(" 720 (PAL)"); }
214         if (w > 640)  { scale_combo.append_text(" 640 (vga, ega)"); }
215         if (w > 352)  { scale_combo.append_text(" 352 (cif)"); }
216         if (w > 320)  { scale_combo.append_text(" 320 (cga, qvga)"); }
217         if (w > 176)  { scale_combo.append_text(" 176 (qcif)"); }
218         scale_combo.set_active(0);
219         height_spinner.set_value(h);
220
221         l = manage (new Label (_("Bitrate (KBit/s):"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
222         t->attach (*l, 0, 1, 1, 2);
223         t->attach (bitrate_checkbox, 2, 3, 1, 2);
224         t->attach (bitrate_spinner, 3, 4, 1, 2);
225
226         l = manage (new Label (_("Extract Audio:"), Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER, false));
227         t->attach (*l, 0, 1, 2, 3);
228         audio_combo.set_name ("PaddedButton");
229         t->attach (audio_combo, 1, 4, 2, 3);
230         audio_combo.append_text("No audio");
231         if (as.size() > 0) {
232                 for (TranscodeFfmpeg::FFAudioStreams::iterator it = as.begin(); it < as.end(); ++it) {
233                         audio_combo.append_text((*it).name);
234                 }
235         }
236         audio_combo.set_active(0);
237
238 #if 1 /* tentative debug mode */
239         options_box->pack_start (debug_checkbox, false, true, 4);
240 #endif
241
242         vbox->pack_start (*path_hbox, false, false);
243         vbox->pack_start (*options_box, false, true);
244
245         get_vbox()->set_spacing (4);
246         get_vbox()->pack_start (*vbox, false, false);
247
248         progress_box = manage (new VBox);
249         progress_box->pack_start (progress_label, false, false);
250         progress_box->pack_start (pbar, false, false);
251         progress_box->pack_start (abort_button, false, false);
252         get_vbox()->pack_start (*progress_box, false, false);
253
254         browse_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::open_browse_dialog));
255         transcode_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::launch_transcode));
256         abort_button.signal_clicked().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::abort_clicked));
257
258         video_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::video_combo_changed));
259         audio_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::audio_combo_changed));
260         scale_combo.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::scale_combo_changed));
261         aspect_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::aspect_checkbox_toggled));
262         height_spinner.signal_changed().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::update_bitrate));
263         bitrate_checkbox.signal_toggled().connect (sigc::mem_fun (*this, &TranscodeVideoDialog::bitrate_checkbox_toggled));
264
265         update_bitrate();
266
267         cancel_button = add_button (Stock::CANCEL, RESPONSE_CANCEL);
268         get_action_area()->pack_start (transcode_button, false, false);
269         show_all_children ();
270         progress_box->hide();
271 }
272
273 TranscodeVideoDialog::~TranscodeVideoDialog ()
274 {
275         delete transcoder;
276 }
277
278 void
279 TranscodeVideoDialog::on_show ()
280 {
281         Dialog::on_show ();
282 }
283
284 void
285 TranscodeVideoDialog::abort_clicked ()
286 {
287         aborted = true;
288         transcoder->cancel();
289 }
290
291 void
292 TranscodeVideoDialog::update_progress (framecnt_t c, framecnt_t a)
293 {
294         if (a == 0 || c > a) {
295                 pbar.set_pulse_step(.5);
296                 pbar.pulse();
297                 return;
298         }
299         pbar.set_fraction ((double)c / (double) a);
300 }
301
302 void
303 TranscodeVideoDialog::finished ()
304 {
305         if (aborted) {
306                 ::g_unlink(path_entry.get_text().c_str());
307                 if (!audiofile.empty()) {
308                         ::g_unlink(audiofile.c_str());
309                 }
310                 Gtk::Dialog::response(RESPONSE_CANCEL);
311         } else {
312                 if (pending_audio_extract) {
313                         StartNextStage();
314                 } else {
315                   Gtk::Dialog::response(RESPONSE_ACCEPT);
316                 }
317         }
318 }
319
320 void
321 TranscodeVideoDialog::launch_audioonly ()
322 {
323         if (audio_combo.get_active_row_number() == 0) {
324                 finished();
325                 return;
326         }
327         dialog_progress_mode();
328 #if 1 /* tentative debug mode */
329         if (debug_checkbox.get_active()) {
330                 transcoder->set_debug(true);
331         }
332 #endif
333         transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::update_progress , this, _1, _2), gui_context());
334         transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::finished, this), gui_context());
335         launch_extract();
336 }
337
338 void
339 TranscodeVideoDialog::launch_extract ()
340 {
341         audiofile= path_entry.get_text() + ".wav"; /* TODO: mktemp */
342         int audio_stream;
343         pending_audio_extract = false;
344         aborted = false;
345         audio_stream = audio_combo.get_active_row_number() -1;
346         progress_label.set_text (_("Extracting Audio.."));
347
348         if (!transcoder->extract_audio(audiofile, _session->nominal_frame_rate(), audio_stream)) {
349                 ARDOUR_UI::instance()->popup_error(_("Audio Extraction Failed."));
350                 audiofile="";
351                 Gtk::Dialog::response(RESPONSE_CANCEL);
352                 return;
353         }
354 }
355
356 void
357 TranscodeVideoDialog::dialog_progress_mode ()
358 {
359         vbox->hide();
360         cancel_button->hide();
361         transcode_button.hide();
362         pbar.set_size_request(300,-1);
363         progress_box->show();
364 }
365
366 void
367 TranscodeVideoDialog::launch_transcode ()
368 {
369         if (video_combo.get_active_row_number() != 2) {
370                 launch_audioonly();
371                 return;
372         }
373         std::string outfn = path_entry.get_text();
374         if (!confirm_video_outfn(outfn, video_get_docroot(Config))) return;
375         progress_label.set_text (_("Transcoding Video.."));
376         dialog_progress_mode();
377 #if 1 /* tentative debug mode */
378         if (debug_checkbox.get_active()) {
379                 transcoder->set_debug(true);
380         }
381 #endif
382
383         aborted = false;
384         if (audio_combo.get_active_row_number() != 0) {
385                 pending_audio_extract = true;
386                 StartNextStage.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::launch_extract , this), gui_context());
387         }
388
389         int scale_width, scale_height, bitrate;
390         if (scale_combo.get_active_row_number() == 0 ) {
391                 scale_width =0;
392         } else {
393           scale_width = atoi(scale_combo.get_active_text());
394         }
395         if (!aspect_checkbox.get_active()) {
396                 scale_height = 0;
397         } else {
398                 scale_height = (int) floor(height_spinner.get_value());
399         }
400         if (bitrate_checkbox.get_active() ){
401                 bitrate = (int) floor(bitrate_spinner.get_value());
402         } else {
403                 bitrate = 0;
404         }
405
406         transcoder->Progress.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::update_progress , this, _1, _2), gui_context());
407         transcoder->Finished.connect(*this, invalidator (*this), boost::bind (&TranscodeVideoDialog::finished, this), gui_context());
408         if (!transcoder->transcode(outfn, scale_width, scale_height, bitrate)) {
409                 ARDOUR_UI::instance()->popup_error(_("Transcoding Failed."));
410                 Gtk::Dialog::response(RESPONSE_CANCEL);
411                 return;
412         }
413 }
414
415 void
416 TranscodeVideoDialog::video_combo_changed ()
417 {
418         int i = video_combo.get_active_row_number();
419         if (i != 2) {
420                 scale_combo.set_sensitive(false);
421                 aspect_checkbox.set_sensitive(false);
422                 height_spinner.set_sensitive(false);
423                 bitrate_checkbox.set_sensitive(false);
424                 bitrate_spinner.set_sensitive(false);
425         } else {
426                 scale_combo.set_sensitive(true);
427                 aspect_checkbox.set_sensitive(true);
428                 height_spinner.set_sensitive(true);
429                 bitrate_checkbox.set_sensitive(true);
430                 bitrate_spinner.set_sensitive(true);
431         }
432 }
433
434 void
435 TranscodeVideoDialog::audio_combo_changed ()
436 {
437         ;
438 }
439
440 void
441 TranscodeVideoDialog::scale_combo_changed ()
442 {
443         if (!aspect_checkbox.get_active()) {
444                 int h;
445                 if (scale_combo.get_active_row_number() == 0 ) {
446                         h = transcoder->get_height();
447                 } else {
448                         h = floor(atof(scale_combo.get_active_text()) / m_aspect);
449                 }
450                 height_spinner.set_value(h);
451         }
452         update_bitrate();
453 }
454
455 void
456 TranscodeVideoDialog::aspect_checkbox_toggled ()
457 {
458         height_spinner.set_sensitive(aspect_checkbox.get_active());
459         scale_combo_changed();
460 }
461
462 void
463 TranscodeVideoDialog::bitrate_checkbox_toggled ()
464 {
465         bitrate_spinner.set_sensitive(bitrate_checkbox.get_active());
466         if (!bitrate_checkbox.get_active()) {
467                 update_bitrate();
468         }
469 }
470
471 void
472 TranscodeVideoDialog::update_bitrate ()
473 {
474         double br = .7; /* avg quality - bits per pixel */
475         if (bitrate_checkbox.get_active() || !transcoder->probe_ok()) { return; }
476         br *= transcoder->get_fps();
477         br *= height_spinner.get_value();
478
479         if (scale_combo.get_active_row_number() == 0 ) {
480                 br *= transcoder->get_width();
481         } else {
482                 br *= atof(scale_combo.get_active_text());
483         }
484         if (br != 0) {
485                 bitrate_spinner.set_value(floor(br/10000.0)*10);
486         }
487 }
488
489 void
490 TranscodeVideoDialog::open_browse_dialog ()
491 {
492         Gtk::FileChooserDialog dialog(_("Save Transcoded Video File"), Gtk::FILE_CHOOSER_ACTION_SAVE);
493         dialog.set_filename (path_entry.get_text());
494
495         dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
496         dialog.add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
497
498         int result = dialog.run();
499
500         if (result == Gtk::RESPONSE_OK) {
501                 std::string filename = dialog.get_filename();
502
503                 if (filename.length()) {
504                         path_entry.set_text (filename);
505                 }
506         }
507 }
508
509 enum VtlTranscodeOption
510 TranscodeVideoDialog::import_option() {
511         int i = video_combo.get_active_row_number();
512         return static_cast<VtlTranscodeOption>(i);
513 }