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.
23 #include <sys/types.h>
25 #include "pbd/error.h"
26 #include "pbd/convert.h"
27 #include "pbd/file_utils.h"
28 #include "gui_thread.h"
30 #include "ardour/filesystem_paths.h"
32 #include "transcode_ffmpeg.h"
33 #include "utils_videotl.h"
38 using namespace VideoUtils;
40 TranscodeFfmpeg::TranscodeFfmpeg (std::string f)
46 m_avoffset = m_lead_in = m_lead_out = 0;
47 m_width = m_height = 0;
50 #if 1 /* tentative debug mode */
54 if (!ARDOUR::ArdourVideoToolPaths::transcoder_exe(ffmpeg_exe, ffprobe_exe)) {
55 warning << string_compose(
57 "ffmpeg installation was not found on this system.\n"
58 "%1 requires ffmpeg and ffprobe from ffmpeg.org - version 1.1 or newer.\n"
59 "Video import and export is not possible until you install tools.\n"
61 "The tools are included with the %1 releases from ardour.org "
62 "and also available with the video-server at http://x42.github.com/harvid/\n"
64 "Important: the files need to be installed in $PATH and named ffmpeg_harvid and ffprobe_harvid.\n"
65 "If you already have a suitable ffmpeg installation on your system, we recommend creating "
66 "symbolic links from ffmpeg to ffmpeg_harvid and from ffprobe to ffprobe_harvid.\n"
68 "see also http://manual.ardour.org/video-timeline/setup/"
69 ), PROGRAM_NAME) << endmsg;
74 if (infile.empty() || !probe()) {
80 TranscodeFfmpeg::~TranscodeFfmpeg ()
86 TranscodeFfmpeg::probe ()
90 argp=(char**) calloc(7,sizeof(char*));
91 argp[0] = strdup(ffprobe_exe.c_str());
92 argp[1] = strdup("-print_format");
93 argp[2] = strdup("csv=nk=0");
94 argp[3] = strdup("-show_format");
95 argp[4] = strdup("-show_streams");
96 argp[5] = strdup(infile.c_str());
98 ffcmd = new ARDOUR::SystemExec(ffprobe_exe, argp);
99 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffprobeparse, this, _1 ,_2));
100 ffcmd->Terminated.connect (*this, invalidator (*this), boost::bind (&TranscodeFfmpeg::ffexit, this), gui_context());
101 if (ffcmd->start(1)) {
106 /* wait for ffprobe process to exit */
109 /* wait for interposer thread to copy all data.
110 * SystemExec::Terminated is emitted and ffcmd set to NULL */
111 int timeout = 300; // 1.5 sec
112 while (ffcmd && --timeout > 0) {
116 if (timeout == 0 || ffoutput.empty()) {
122 std::vector<std::vector<std::string> > lines;
123 ParseCSV(ffoutput, lines);
125 m_width = m_height = 0;
126 m_fps = m_aspect = 0;
132 #define PARSE_FRACTIONAL_FPS(VAR) \
134 std::string::size_type pos; \
136 pos = value.find_first_of('/'); \
137 if (pos != std::string::npos) { \
138 VAR = atof(value.substr(0, pos)) / atof(value.substr(pos+1)); \
142 std::string duration_from_format;
144 for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
145 if (i->at(0) == X_("format")) {
146 /* format,filename,#streams,format-name,format-long-name,start-time,duration,size,bitrate */
147 for (std::vector<std::string>::iterator kv = i->begin(); kv != i->end(); ++kv) {
148 const size_t kvsep = kv->find('=');
149 if(kvsep == std::string::npos) continue;
150 std::string key = kv->substr(0, kvsep);
151 std::string value = kv->substr(kvsep + 1);
152 if (key == X_("duration")) {
153 duration_from_format = value;
157 if (i->at(0) == X_("stream")) {
158 if (i->at(5) == X_("codec_type=video") && m_width == 0) {
160 for (std::vector<std::string>::iterator kv = i->begin(); kv != i->end(); ++kv) {
161 const size_t kvsep = kv->find('=');
162 if(kvsep == std::string::npos) continue;
163 std::string key = kv->substr(0, kvsep);
164 std::string value = kv->substr(kvsep + 1);
166 if (key == X_("index")) {
167 m_videoidx = atoi(value);
168 } else if (key == X_("width")) {
169 m_width = atoi(value);
170 } else if (key == X_("height")) {
171 m_height = atoi(value);
172 } else if (key == X_("codec_name")) {
173 if (!m_codec.empty()) m_codec += " ";
175 } else if (key == X_("codec_long_name")) {
176 if (!m_codec.empty()) m_codec += " ";
177 m_codec += "[" + value + "]";
178 } else if (key == X_("codec_tag_string")) {
179 if (!m_codec.empty()) m_codec += " ";
180 m_codec += "(" + value + ")";
181 } else if (key == X_("r_frame_rate")) {
182 PARSE_FRACTIONAL_FPS(m_fps)
183 } else if (key == X_("avg_frame_rate") && m_fps == 0) {
184 PARSE_FRACTIONAL_FPS(m_fps)
185 } else if (key == X_("time_base")) {
186 PARSE_FRACTIONAL_FPS(timebase)
187 } else if (key == X_("timecode") && m_duration == 0 && m_fps > 0) {
188 int h,m,s; char f[32];
189 if (sscanf(i->at(16).c_str(), "%d:%d:%d:%32s",&h,&m,&s,f) == 4) {
190 m_duration = (ARDOUR::samplecnt_t) floor(m_fps * (
194 + atoi(f) / pow((double)10, (int)strlen(f))
197 } else if (key == X_("duration_ts") && m_fps == 0 && timebase !=0 ) {
198 m_duration = atof(value) * m_fps * timebase;
199 } else if (key == X_("duration") && m_fps != 0 && m_duration == 0) {
200 m_duration = atof(value) * m_fps;
201 } else if (key == X_("sample_aspect_ratio")) {
202 std::string::size_type pos;
203 pos = value.find_first_of(':');
204 if (pos != std::string::npos && atof(value.substr(pos+1)) != 0) {
206 m_sar.replace(pos, 1, "/");
208 } else if (key == X_("display_aspect_ratio")) {
209 std::string::size_type pos;
210 pos = value.find_first_of(':');
211 if (pos != std::string::npos && atof(value.substr(pos+1)) != 0) {
212 m_aspect = atof(value.substr(0, pos)) / atof(value.substr(pos+1));
218 m_aspect = (double)m_width / (double)m_height;
221 } else if (i->at(5) == X_("codec_type=audio")) { /* new ffprobe */
223 for (std::vector<std::string>::iterator kv = i->begin(); kv != i->end(); ++kv) {
224 const size_t kvsep = kv->find('=');
225 if(kvsep == std::string::npos) continue;
226 std::string key = kv->substr(0, kvsep);
227 std::string value = kv->substr(kvsep + 1);
229 if (key == X_("channels")) {
230 as.channels = atoi(value);
231 } else if (key == X_("index")) {
232 as.stream_id = value;
233 } else if (key == X_("codec_long_name")) {
234 if (!as.name.empty()) as.name += " ";
236 } else if (key == X_("codec_name")) {
237 if (!as.name.empty()) as.name += " ";
239 } else if (key == X_("sample_fmt")) {
240 if (!as.name.empty()) as.name += " ";
241 as.name += "FMT:" + value;
242 } else if (key == X_("sample_rate")) {
243 if (!as.name.empty()) as.name += " ";
244 as.name += "SR:" + value;
248 m_audio.push_back(as);
254 if (m_duration == 0 && !duration_from_format.empty() && m_fps > 0) {
255 warning << "using video-duration from format (container)." << endmsg;
256 m_duration = atof(duration_from_format) * m_fps;
260 printf("FPS: %f\n", m_fps);
261 printf("Duration: %lu frames\n",(unsigned long)m_duration);
262 printf("W/H: %ix%i\n",m_width, m_height);
263 printf("aspect: %f\n",m_aspect);
264 printf("codec: %s\n",m_codec.c_str());
265 if (m_audio.size() > 0) {
266 for (AudioStreams::iterator it = m_audio.begin(); it < m_audio.end(); ++it) {
267 printf("audio: %s - %i channels\n",(*it).stream_id.c_str(), (*it).channels);
270 printf("audio: no audio streams in file.\n");
277 TranscodeFfmpeg::FFSettings
278 TranscodeFfmpeg::default_encoder_settings ()
280 TranscodeFfmpeg::FFSettings ffs;
282 ffs["-vcodec"] = "mpeg4";
283 ffs["-acodec"] = "ac3";
284 ffs["-b:v"] = "5000k";
285 ffs["-b:a"] = "160k";
289 TranscodeFfmpeg::FFSettings
290 TranscodeFfmpeg::default_meta_data ()
292 TranscodeFfmpeg::FFSettings ffm;
294 ffm["comment"] = "Created with " PROGRAM_NAME;
300 TranscodeFfmpeg::encode (std::string outfile, std::string inf_a, std::string inf_v, TranscodeFfmpeg::FFSettings ffs, TranscodeFfmpeg::FFSettings meta, bool map)
302 #define MAX_FFMPEG_ENCODER_ARGS (100)
306 argp=(char**) calloc(MAX_FFMPEG_ENCODER_ARGS,sizeof(char*));
307 argp[a++] = strdup(ffmpeg_exe.c_str());
308 if (m_avoffset < 0 || m_avoffset > 0) {
309 std::ostringstream osstream; osstream << m_avoffset;
310 argp[a++] = strdup("-itsoffset");
311 argp[a++] = strdup(osstream.str().c_str());
313 argp[a++] = strdup("-i");
314 argp[a++] = strdup(inf_v.c_str());
316 argp[a++] = strdup("-i");
317 argp[a++] = strdup(inf_a.c_str());
319 for(TranscodeFfmpeg::FFSettings::const_iterator it = ffs.begin(); it != ffs.end(); ++it) {
320 argp[a++] = strdup(it->first.c_str());
321 argp[a++] = strdup(it->second.c_str());
323 for(TranscodeFfmpeg::FFSettings::const_iterator it = meta.begin(); it != meta.end(); ++it) {
324 argp[a++] = strdup("-metadata");
325 argp[a++] = SystemExec::format_key_value_parameter (it->first.c_str(), it->second.c_str());
329 m_lead_in = rint (m_lead_in * m_fps) / m_fps;
330 m_lead_out = rint (m_lead_out * m_fps) / m_fps;
333 if (m_lead_in != 0 && m_lead_out != 0) {
334 std::ostringstream osstream;
335 argp[a++] = strdup("-vf");
336 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_in;
337 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
338 osstream << X_(" [pre]; ");
339 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_out;
340 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
341 osstream << X_(" [post]; ");
342 osstream << X_("[pre] [in] [post] concat=n=3");
343 argp[a++] = strdup(osstream.str().c_str());
344 } else if (m_lead_in != 0) {
345 std::ostringstream osstream;
346 argp[a++] = strdup("-vf");
347 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_in;
348 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
349 osstream << X_(" [pre]; ");
350 osstream << X_("[pre] [in] concat=n=2");
351 argp[a++] = strdup(osstream.str().c_str());
352 } else if (m_lead_out != 0) {
353 std::ostringstream osstream;
354 argp[a++] = strdup("-vf");
355 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_out;
356 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
357 osstream << X_(" [post]; ");
358 osstream << X_("[in] [post] concat=n=2");
359 argp[a++] = strdup(osstream.str().c_str());
363 std::ostringstream osstream;
364 argp[a++] = strdup("-map");
365 osstream << X_("0:") << m_videoidx;
366 argp[a++] = strdup(osstream.str().c_str());
367 argp[a++] = strdup("-map");
368 argp[a++] = strdup("1:0");
371 argp[a++] = strdup("-y");
372 argp[a++] = strdup(outfile.c_str());
374 assert(a<MAX_FFMPEG_ENCODER_ARGS);
375 /* Note: these are free()d in ~SystemExec */
377 if (debug_enable) { /* tentative debug mode */
378 printf("EXPORT ENCODE:\n");
379 for (int i=0; i< a; ++i) {
380 printf("%s ", argp[i]);
386 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
387 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_v, this, _1 ,_2));
388 ffcmd->Terminated.connect (*this, invalidator (*this), boost::bind (&TranscodeFfmpeg::ffexit, this), gui_context());
389 if (ffcmd->start(2)) {
397 TranscodeFfmpeg::extract_audio (std::string outfile, ARDOUR::samplecnt_t /*samplerate*/, unsigned int stream)
399 if (!probeok) return false;
400 if (stream >= m_audio.size()) return false;
405 argp=(char**) calloc(15,sizeof(char*));
406 argp[i++] = strdup(ffmpeg_exe.c_str());
407 argp[i++] = strdup("-i");
408 argp[i++] = strdup(infile.c_str());
409 #if 0 /* ffmpeg write original samplerate, use a3/SRC to resample */
410 argp[i++] = strdup("-ar");
411 argp[i] = (char*) calloc(7,sizeof(char)); snprintf(argp[i++], 7, "%"PRId64, samplerate);
413 argp[i++] = strdup("-ac");
414 argp[i] = (char*) calloc(3,sizeof(char)); snprintf(argp[i++], 3, "%i", m_audio.at(stream).channels);
415 argp[i++] = strdup("-map");
416 argp[i] = (char*) calloc(8,sizeof(char)); snprintf(argp[i++], 8, "0:%s", m_audio.at(stream).stream_id.c_str());
417 argp[i++] = strdup("-vn");
418 argp[i++] = strdup("-acodec");
419 argp[i++] = strdup("pcm_f32le");
420 argp[i++] = strdup("-y");
421 argp[i++] = strdup(outfile.c_str());
422 argp[i++] = (char *)0;
423 /* Note: argp is free()d in ~SystemExec */
425 if (debug_enable) { /* tentative debug mode */
426 printf("EXTRACT AUDIO:\n");
427 for (int i=0; i< 14; ++i) {
428 printf("%s ", argp[i]);
434 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
435 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_a, this, _1 ,_2));
436 ffcmd->Terminated.connect (*this, invalidator (*this), boost::bind (&TranscodeFfmpeg::ffexit, this), gui_context());
437 if (ffcmd->start(2)) {
446 TranscodeFfmpeg::transcode (std::string outfile, const int outw, const int outh, const int kbitps)
448 if (!probeok) return false;
451 int bitrate = kbitps;
455 if (width < 1 || width > m_width) { width = m_width; } /* don't allow upscaling */
456 if (height < 1 || height > m_height) { height = floor(width / m_aspect); }
459 const double bitperpixel = .7; /* avg quality */
460 bitrate = floor(m_fps * width * height * bitperpixel / 10000.0);
462 bitrate = bitrate / 10;
464 if (bitrate < 10) bitrate = 10;
465 if (bitrate > 1000) bitrate = 1000;
467 argp=(char**) calloc(16,sizeof(char*));
468 argp[0] = strdup(ffmpeg_exe.c_str());
469 argp[1] = strdup("-i");
470 argp[2] = strdup(infile.c_str());
471 argp[3] = strdup("-b:v");
472 argp[4] = (char*) calloc(7,sizeof(char)); snprintf(argp[4], 7, "%i0k", bitrate);
473 argp[5] = strdup("-s");
474 argp[6] = (char*) calloc(10,sizeof(char)); snprintf(argp[6], 10, "%ix%i", width, height);
475 argp[7] = strdup("-y");
476 argp[8] = strdup("-vcodec");
477 argp[9] = strdup("mjpeg");
478 argp[10] = strdup("-an");
479 argp[11] = strdup("-intra");
480 argp[12] = strdup("-g");
481 argp[13] = strdup("1");
482 argp[14] = strdup(outfile.c_str());
483 argp[15] = (char *)0;
484 /* Note: these are free()d in ~SystemExec */
486 if (debug_enable) { /* tentative debug mode */
487 printf("TRANSCODE VIDEO:\n");
488 for (int i=0; i< 15; ++i) {
489 printf("%s ", argp[i]);
494 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
495 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_v, this, _1 ,_2));
496 ffcmd->Terminated.connect (*this, invalidator (*this), boost::bind (&TranscodeFfmpeg::ffexit, this), gui_context());
497 if (ffcmd->start(2)) {
505 TranscodeFfmpeg::cancel ()
507 if (!ffcmd || !ffcmd->is_running()) { return;}
508 ffcmd->write_to_stdin("q");
509 #ifdef PLATFORM_WINDOWS
520 TranscodeFfmpeg::ffexit ()
524 Finished(); /* EMIT SIGNAL */
528 TranscodeFfmpeg::ffprobeparse (std::string d, size_t /* s */)
534 TranscodeFfmpeg::ffmpegparse_a (std::string d, size_t /* s */)
537 int h,m,s; char f[7];
538 ARDOUR::samplecnt_t p = -1;
540 if (!(t=strstr(d.c_str(), "time="))) { return; }
542 if (sscanf(t+5, "%d:%d:%d.%s",&h,&m,&s,f) == 4) {
543 p = (ARDOUR::samplecnt_t) floor( 100.0 * (
547 + atoi(f) / pow((double)10, (int)strlen(f))
549 p = p * m_fps / 100.0;
550 if (p > m_duration ) { p = m_duration; }
551 Progress(p, m_duration); /* EMIT SIGNAL */
553 Progress(0, 0); /* EMIT SIGNAL */
558 TranscodeFfmpeg::ffmpegparse_v (std::string d, size_t /* s */)
560 if (strstr(d.c_str(), "ERROR") || strstr(d.c_str(), "Error") || strstr(d.c_str(), "error")) {
561 warning << "ffmpeg-error: " << d << endmsg;
563 if (strncmp(d.c_str(), "frame=",6)) {
566 d.erase(d.find_last_not_of(" \t\r\n") + 1);
567 printf("ffmpeg: '%s'\n", d.c_str());
572 ARDOUR::samplecnt_t f = atol(d.substr(6));
574 Progress(0, 0); /* EMIT SIGNAL */
576 Progress(f, m_duration); /* EMIT SIGNAL */