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 "transcode_ffmpeg.h"
31 #include "utils_videotl.h"
32 #include "video_tool_paths.h"
37 using namespace VideoUtils;
39 TranscodeFfmpeg::TranscodeFfmpeg (std::string f)
45 m_avoffset = m_lead_in = m_lead_out = 0;
46 m_width = m_height = 0;
49 #if 1 /* tentative debug mode */
53 if (!ArdourVideoToolPaths::transcoder_exe(ffmpeg_exe, ffprobe_exe)) {
54 warning << string_compose(
56 "No ffprobe or ffmpeg executables could be found on this system.\n"
57 "Video import and export is not possible until you install those tools.\n"
58 "%1 requires ffmpeg and ffprobe from ffmpeg.org - version 1.1 or newer.\n"
60 "The tools are included with the %1 releases from ardour.org "
61 "and also available with the video-server at http://x42.github.com/harvid/\n"
63 "Important: the files need to be installed in $PATH and named ffmpeg_harvid and ffprobe_harvid.\n"
64 "If you already have a suitable ffmpeg installation on your system, we recommend creating "
65 "symbolic links from ffmpeg to ffmpeg_harvid and from ffprobe to ffprobe_harvid.\n"
67 "see also http://manual.ardour.org/video-timeline/setup/"
68 ), PROGRAM_NAME) << endmsg;
73 if (infile.empty() || !probe()) {
79 TranscodeFfmpeg::~TranscodeFfmpeg ()
85 TranscodeFfmpeg::probe ()
89 argp=(char**) calloc(7,sizeof(char*));
90 argp[0] = strdup(ffprobe_exe.c_str());
91 argp[1] = strdup("-print_format");
92 argp[2] = strdup("csv=nk=0");
93 argp[3] = strdup("-show_format");
94 argp[4] = strdup("-show_streams");
95 argp[5] = strdup(infile.c_str());
97 ffcmd = new ARDOUR::SystemExec(ffprobe_exe, argp);
98 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffprobeparse, this, _1 ,_2));
99 ffcmd->Terminated.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffexit, this));
100 if (ffcmd->start(1)) {
105 /* wait for ffprobe process to exit */
108 /* wait for interposer thread to copy all data.
109 * SystemExec::Terminated is emitted and ffcmd set to NULL */
110 int timeout = 300; // 1.5 sec
111 while (ffcmd && --timeout > 0) {
114 if (timeout == 0 || ffoutput.empty()) {
120 std::vector<std::vector<std::string> > lines;
121 ParseCSV(ffoutput, lines);
123 m_width = m_height = 0;
124 m_fps = m_aspect = 0;
130 #define PARSE_FRACTIONAL_FPS(VAR) \
132 std::string::size_type pos; \
134 pos = value.find_first_of('/'); \
135 if (pos != std::string::npos) { \
136 VAR = atof(value.substr(0, pos)) / atof(value.substr(pos+1)); \
140 for (std::vector<std::vector<std::string> >::iterator i = lines.begin(); i != lines.end(); ++i) {
141 if (i->at(0) == X_("format")) {
142 /* format,filename,#streams,format-name,format-long-name,start-time,duration,size,bitrate */
144 if (i->at(0) == X_("stream")) {
145 if (i->at(5) == X_("codec_type=video") && m_width == 0) {
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);
153 if (key == X_("index")) {
154 m_videoidx = atoi(value);
155 } else if (key == X_("width")) {
156 m_width = atoi(value);
157 } else if (key == X_("height")) {
158 m_height = atoi(value);
159 } else if (key == X_("codec_name")) {
160 if (!m_codec.empty()) m_codec += " ";
162 } else if (key == X_("codec_long_name")) {
163 if (!m_codec.empty()) m_codec += " ";
164 m_codec += "[" + value + "]";
165 } else if (key == X_("codec_tag_string")) {
166 if (!m_codec.empty()) m_codec += " ";
167 m_codec += "(" + value + ")";
168 } else if (key == X_("r_frame_rate")) {
169 PARSE_FRACTIONAL_FPS(m_fps)
170 } else if (key == X_("avg_frame_rate") && m_fps == 0) {
171 PARSE_FRACTIONAL_FPS(m_fps)
172 } else if (key == X_("time_base")) {
173 PARSE_FRACTIONAL_FPS(timebase)
174 } else if (key == X_("timecode") && m_duration == 0) {
175 int h,m,s; char f[32];
176 if (sscanf(i->at(16).c_str(), "%d:%d:%d:%32s",&h,&m,&s,f) == 4) {
177 m_duration = (ARDOUR::framecnt_t) floor(m_fps * (
181 + atoi(f) / pow((double)10, (int)strlen(f))
184 } else if (key == X_("duration_ts") && m_fps == 0 && timebase !=0 ) {
185 m_duration = atof(value) * m_fps * timebase;
186 } else if (key == X_("duration") && m_fps != 0 && m_duration == 0) {
187 m_duration = atof(value) * m_fps;
188 } else if (key == X_("sample_aspect_ratio")) {
189 std::string::size_type pos;
190 pos = value.find_first_of(':');
191 if (pos != std::string::npos && atof(value.substr(pos+1)) != 0) {
193 m_sar.replace(pos, 1, "/");
195 } else if (key == X_("display_aspect_ratio")) {
196 std::string::size_type pos;
197 pos = value.find_first_of(':');
198 if (pos != std::string::npos && atof(value.substr(pos+1)) != 0) {
199 m_aspect = atof(value.substr(0, pos)) / atof(value.substr(pos+1));
205 m_aspect = (double)m_width / (double)m_height;
208 } else if (i->at(5) == X_("codec_type=audio")) { /* new ffprobe */
210 for (std::vector<std::string>::iterator kv = i->begin(); kv != i->end(); ++kv) {
211 const size_t kvsep = kv->find('=');
212 if(kvsep == std::string::npos) continue;
213 std::string key = kv->substr(0, kvsep);
214 std::string value = kv->substr(kvsep + 1);
216 if (key == X_("channels")) {
217 as.channels = atoi(value);
218 } else if (key == X_("index")) {
219 as.stream_id = value;
220 } else if (key == X_("codec_long_name")) {
221 if (!as.name.empty()) as.name += " ";
223 } else if (key == X_("codec_name")) {
224 if (!as.name.empty()) as.name += " ";
226 } else if (key == X_("sample_fmt")) {
227 if (!as.name.empty()) as.name += " ";
228 as.name += "FMT:" + value;
229 } else if (key == X_("sample_rate")) {
230 if (!as.name.empty()) as.name += " ";
231 as.name += "SR:" + value;
235 m_audio.push_back(as);
242 printf("FPS: %f\n", m_fps);
243 printf("Duration: %lu frames\n",(unsigned long)m_duration);
244 printf("W/H: %ix%i\n",m_width, m_height);
245 printf("aspect: %f\n",m_aspect);
246 printf("codec: %s\n",m_codec.c_str());
247 if (m_audio.size() > 0) {
248 for (AudioStreams::iterator it = m_audio.begin(); it < m_audio.end(); ++it) {
249 printf("audio: %s - %i channels\n",(*it).stream_id.c_str(), (*it).channels);
252 printf("audio: no audio streams in file.\n");
259 TranscodeFfmpeg::FFSettings
260 TranscodeFfmpeg::default_encoder_settings ()
262 TranscodeFfmpeg::FFSettings ffs;
264 ffs["-vcodec"] = "mpeg4";
265 ffs["-acodec"] = "ac3";
266 ffs["-b:v"] = "5000k";
267 ffs["-b:a"] = "160k";
271 TranscodeFfmpeg::FFSettings
272 TranscodeFfmpeg::default_meta_data ()
274 TranscodeFfmpeg::FFSettings ffm;
276 ffm["comment"] = "Created with " PROGRAM_NAME;
281 TranscodeFfmpeg::format_metadata (std::string key, std::string value)
283 size_t start_pos = 0;
284 std::string v1 = value;
285 while((start_pos = v1.find_first_not_of(
286 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(),.\"'",
287 start_pos)) != std::string::npos)
289 v1.replace(start_pos, 1, "_");
294 while((start_pos = v1.find("\"", start_pos)) != std::string::npos) {
295 v1.replace(start_pos, 1, "\\\"");
299 size_t len = key.length() + v1.length() + 4;
300 char *mds = (char*) calloc(len, sizeof(char));
301 snprintf(mds, len, "%s=\"%s\"", key.c_str(), v1.c_str());
306 TranscodeFfmpeg::encode (std::string outfile, std::string inf_a, std::string inf_v, TranscodeFfmpeg::FFSettings ffs, TranscodeFfmpeg::FFSettings meta, bool map)
308 #define MAX_FFMPEG_ENCODER_ARGS (100)
312 argp=(char**) calloc(MAX_FFMPEG_ENCODER_ARGS,sizeof(char*));
313 argp[a++] = strdup(ffmpeg_exe.c_str());
314 if (m_avoffset < 0 || m_avoffset > 0) {
315 std::ostringstream osstream; osstream << m_avoffset;
316 argp[a++] = strdup("-itsoffset");
317 argp[a++] = strdup(osstream.str().c_str());
319 argp[a++] = strdup("-i");
320 argp[a++] = strdup(inf_v.c_str());
322 argp[a++] = strdup("-i");
323 argp[a++] = strdup(inf_a.c_str());
325 for(TranscodeFfmpeg::FFSettings::const_iterator it = ffs.begin(); it != ffs.end(); ++it) {
326 argp[a++] = strdup(it->first.c_str());
327 argp[a++] = strdup(it->second.c_str());
329 #ifndef PLATFORM_WINDOWS
330 // quote-escape does not work here. this needs further work (system_exec.cc)
331 for(TranscodeFfmpeg::FFSettings::const_iterator it = meta.begin(); it != meta.end(); ++it) {
332 argp[a++] = strdup("-metadata");
333 argp[a++] = format_metadata(it->first.c_str(), it->second.c_str());
338 m_lead_in = rint (m_lead_in * m_fps) / m_fps;
339 m_lead_out = rint (m_lead_out * m_fps) / m_fps;
342 if (m_lead_in != 0 && m_lead_out != 0) {
343 std::ostringstream osstream;
344 argp[a++] = strdup("-vf");
345 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_in;
346 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
347 osstream << X_(" [pre]; ");
348 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_out;
349 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
350 osstream << X_(" [post]; ");
351 osstream << X_("[pre] [in] [post] concat=n=3");
352 argp[a++] = strdup(osstream.str().c_str());
353 } else if (m_lead_in != 0) {
354 std::ostringstream osstream;
355 argp[a++] = strdup("-vf");
356 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_in;
357 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
358 osstream << X_(" [pre]; ");
359 osstream << X_("[pre] [in] concat=n=2");
360 argp[a++] = strdup(osstream.str().c_str());
361 } else if (m_lead_out != 0) {
362 std::ostringstream osstream;
363 argp[a++] = strdup("-vf");
364 osstream << X_("color=c=black:s=") << m_width << X_("x") << m_height << X_(":d=") << m_lead_out;
365 if (!m_sar.empty()) osstream << X_(":sar=") << m_sar;
366 osstream << X_(" [post]; ");
367 osstream << X_("[in] [post] concat=n=2");
368 argp[a++] = strdup(osstream.str().c_str());
372 std::ostringstream osstream;
373 argp[a++] = strdup("-map");
374 osstream << X_("0:") << m_videoidx;
375 argp[a++] = strdup(osstream.str().c_str());
376 argp[a++] = strdup("-map");
377 argp[a++] = strdup("1:0");
380 argp[a++] = strdup("-y");
381 argp[a++] = strdup(outfile.c_str());
383 assert(a<MAX_FFMPEG_ENCODER_ARGS);
384 /* Note: these are free()d in ~SystemExec */
386 if (debug_enable) { /* tentative debug mode */
387 printf("EXPORT ENCODE:\n");
388 for (int i=0; i< a; ++i) {
389 printf("%s ", argp[i]);
395 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
396 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_v, this, _1 ,_2));
397 ffcmd->Terminated.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffexit, this));
398 if (ffcmd->start(2)) {
406 TranscodeFfmpeg::extract_audio (std::string outfile, ARDOUR::framecnt_t /*samplerate*/, unsigned int stream)
408 if (!probeok) return false;
409 if (stream >= m_audio.size()) return false;
414 argp=(char**) calloc(15,sizeof(char*));
415 argp[i++] = strdup(ffmpeg_exe.c_str());
416 argp[i++] = strdup("-i");
417 argp[i++] = strdup(infile.c_str());
418 #if 0 /* ffmpeg write original samplerate, use a3/SRC to resample */
419 argp[i++] = strdup("-ar");
420 argp[i] = (char*) calloc(7,sizeof(char)); snprintf(argp[i++], 7, "%"PRId64, samplerate);
422 argp[i++] = strdup("-ac");
423 argp[i] = (char*) calloc(3,sizeof(char)); snprintf(argp[i++], 3, "%i", m_audio.at(stream).channels);
424 argp[i++] = strdup("-map");
425 argp[i] = (char*) calloc(8,sizeof(char)); snprintf(argp[i++], 8, "0:%s", m_audio.at(stream).stream_id.c_str());
426 argp[i++] = strdup("-vn");
427 argp[i++] = strdup("-acodec");
428 argp[i++] = strdup("pcm_f32le");
429 argp[i++] = strdup("-y");
430 argp[i++] = strdup(outfile.c_str());
431 argp[i++] = (char *)0;
432 /* Note: argp is free()d in ~SystemExec */
434 if (debug_enable) { /* tentative debug mode */
435 printf("EXTRACT AUDIO:\n");
436 for (int i=0; i< 14; ++i) {
437 printf("%s ", argp[i]);
443 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
444 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_a, this, _1 ,_2));
445 ffcmd->Terminated.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffexit, this));
446 if (ffcmd->start(2)) {
455 TranscodeFfmpeg::transcode (std::string outfile, const int outw, const int outh, const int kbitps)
457 if (!probeok) return false;
460 int bitrate = kbitps;
464 if (width < 1 || width > m_width) { width = m_width; } /* don't allow upscaling */
465 if (height < 1 || height > m_height) { height = floor(width / m_aspect); }
468 const double bitperpixel = .7; /* avg quality */
469 bitrate = floor(m_fps * width * height * bitperpixel / 10000.0);
471 bitrate = bitrate / 10;
473 if (bitrate < 10) bitrate = 10;
474 if (bitrate > 1000) bitrate = 1000;
476 argp=(char**) calloc(15,sizeof(char*));
477 argp[0] = strdup(ffmpeg_exe.c_str());
478 argp[1] = strdup("-i");
479 argp[2] = strdup(infile.c_str());
480 argp[3] = strdup("-b:v");
481 argp[4] = (char*) calloc(7,sizeof(char)); snprintf(argp[4], 7, "%i0k", bitrate);
482 argp[5] = strdup("-s");
483 argp[6] = (char*) calloc(10,sizeof(char)); snprintf(argp[6], 10, "%ix%i", width, height);
484 argp[7] = strdup("-y");
485 argp[8] = strdup("-vcodec");
486 argp[9] = strdup("mpeg4");
487 argp[10] = strdup("-an");
488 argp[11] = strdup("-keyint_min");
489 argp[12] = strdup("10");
490 argp[13] = strdup(outfile.c_str());
491 argp[14] = (char *)0;
492 /* Note: these are free()d in ~SystemExec */
494 if (debug_enable) { /* tentative debug mode */
495 printf("TRANSCODE VIDEO:\n");
496 for (int i=0; i< 14; ++i) {
497 printf("%s ", argp[i]);
502 ffcmd = new ARDOUR::SystemExec(ffmpeg_exe, argp);
503 ffcmd->ReadStdout.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffmpegparse_v, this, _1 ,_2));
504 ffcmd->Terminated.connect_same_thread (*this, boost::bind (&TranscodeFfmpeg::ffexit, this));
505 if (ffcmd->start(2)) {
513 TranscodeFfmpeg::cancel ()
515 if (!ffcmd || !ffcmd->is_running()) { return;}
516 ffcmd->write_to_stdin("q");
517 #ifdef PLATFORM_WINDOWS
528 TranscodeFfmpeg::ffexit ()
532 Finished(); /* EMIT SIGNAL */
536 TranscodeFfmpeg::ffprobeparse (std::string d, size_t /* s */)
542 TranscodeFfmpeg::ffmpegparse_a (std::string d, size_t /* s */)
545 int h,m,s; char f[7];
546 ARDOUR::framecnt_t p = -1;
548 if (!(t=strstr(d.c_str(), "time="))) { return; }
550 if (sscanf(t+5, "%d:%d:%d.%s",&h,&m,&s,f) == 4) {
551 p = (ARDOUR::framecnt_t) floor( 100.0 * (
555 + atoi(f) / pow((double)10, (int)strlen(f))
557 p = p * m_fps / 100.0;
558 if (p > m_duration ) { p = m_duration; }
559 Progress(p, m_duration); /* EMIT SIGNAL */
561 Progress(0, 0); /* EMIT SIGNAL */
566 TranscodeFfmpeg::ffmpegparse_v (std::string d, size_t /* s */)
568 if (strstr(d.c_str(), "ERROR") || strstr(d.c_str(), "Error") || strstr(d.c_str(), "error")) {
569 warning << "ffmpeg-error: " << d << endmsg;
571 if (strncmp(d.c_str(), "frame=",6)) {
574 d.erase(d.find_last_not_of(" \t\r\n") + 1);
575 printf("ffmpeg: '%s'\n", d.c_str());
578 Progress(0, 0); /* EMIT SIGNAL */
581 ARDOUR::framecnt_t f = atol(d.substr(6));
583 Progress(0, 0); /* EMIT SIGNAL */
585 Progress(f, m_duration); /* EMIT SIGNAL */