23 #include <sys/types.h>
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;
89 argp=(
char**) calloc(7,
sizeof(
char*));
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());
111 while (
ffcmd && --timeout > 0) {
114 if (timeout == 0 ||
ffoutput.empty()) {
120 std::vector<std::vector<std::string> > lines;
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")) {
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")) {
155 }
else if (key ==
X_(
"width")) {
157 }
else if (key ==
X_(
"height")) {
159 }
else if (key ==
X_(
"codec_name")) {
162 }
else if (key ==
X_(
"codec_long_name")) {
165 }
else if (key ==
X_(
"codec_tag_string")) {
168 }
else if (key ==
X_(
"r_frame_rate")) {
170 }
else if (key ==
X_(
"avg_frame_rate") &&
m_fps == 0) {
172 }
else if (key ==
X_(
"time_base")) {
175 int h,m,s;
char f[7];
176 if (sscanf(i->at(16).c_str(),
"%d:%d:%d:%s",&h,&m,&s,
f) == 4) {
181 +
atoi(f) / pow((
double)10, (
int)strlen(f))
184 }
else if (key ==
X_(
"duration_ts") &&
m_fps == 0 && timebase !=0 ) {
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) {
208 }
else if (i->at(5) ==
X_(
"codec_type=audio")) {
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")) {
218 }
else if (key ==
X_(
"index")) {
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;
242 printf(
"FPS: %f\n",
m_fps);
243 printf(
"Duration: %lu frames\n",(
unsigned long)
m_duration);
246 printf(
"codec: %s\n",
m_codec.c_str());
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");
264 ffs[
"-vcodec"] =
"mpeg4";
265 ffs[
"-acodec"] =
"ac3";
266 ffs[
"-b:v"] =
"5000k";
267 ffs[
"-b:a"] =
"160k";
276 ffm[
"comment"] =
"Created with " PROGRAM_NAME;
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());
308 #define MAX_FFMPEG_ENCODER_ARGS (100)
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 for(TranscodeFfmpeg::FFSettings::const_iterator it = meta.begin(); it != meta.end(); ++it) {
330 argp[a++] = strdup(
"-metadata");
340 std::ostringstream osstream;
341 argp[a++] = strdup(
"-vf");
344 osstream <<
X_(
" [pre]; ");
347 osstream <<
X_(
" [post]; ");
348 osstream <<
X_(
"[pre] [in] [post] concat=n=3");
349 argp[a++] = strdup(osstream.str().c_str());
351 std::ostringstream osstream;
352 argp[a++] = strdup(
"-vf");
355 osstream <<
X_(
" [pre]; ");
356 osstream <<
X_(
"[pre] [in] concat=n=2");
357 argp[a++] = strdup(osstream.str().c_str());
359 std::ostringstream osstream;
360 argp[a++] = strdup(
"-vf");
363 osstream <<
X_(
" [post]; ");
364 osstream <<
X_(
"[in] [post] concat=n=2");
365 argp[a++] = strdup(osstream.str().c_str());
369 std::ostringstream osstream;
370 argp[a++] = strdup(
"-map");
372 argp[a++] = strdup(osstream.str().c_str());
373 argp[a++] = strdup(
"-map");
374 argp[a++] = strdup(
"1:0");
377 argp[a++] = strdup(
"-y");
378 argp[a++] = strdup(outfile.c_str());
384 printf(
"EXPORT ENCODE:\n");
385 for (
int i=0; i< a; ++i) {
386 printf(
"%s ", argp[i]);
406 if (stream >=
m_audio.size())
return false;
411 argp=(
char**) calloc(15,
sizeof(
char*));
413 argp[i++] = strdup(
"-i");
414 argp[i++] = strdup(
infile.c_str());
416 argp[i++] = strdup(
"-ar");
417 argp[i] = (
char*) calloc(7,
sizeof(
char)); snprintf(argp[i++], 7,
"%"PRId64, samplerate);
419 argp[i++] = strdup(
"-ac");
420 argp[i] = (
char*) calloc(3,
sizeof(
char)); snprintf(argp[i++], 3,
"%i",
m_audio.at(stream).channels);
421 argp[i++] = strdup(
"-map");
422 argp[i] = (
char*) calloc(8,
sizeof(
char)); snprintf(argp[i++], 8,
"0:%s",
m_audio.at(stream).stream_id.c_str());
423 argp[i++] = strdup(
"-vn");
424 argp[i++] = strdup(
"-acodec");
425 argp[i++] = strdup(
"pcm_f32le");
426 argp[i++] = strdup(
"-y");
427 argp[i++] = strdup(outfile.c_str());
428 argp[i++] = (
char *)0;
432 printf(
"EXTRACT AUDIO:\n");
433 for (
int i=0; i< 14; ++i) {
434 printf(
"%s ", argp[i]);
457 int bitrate = kbitps;
465 const double bitperpixel = .7;
466 bitrate = floor(
m_fps * width * height * bitperpixel / 10000.0);
468 bitrate = bitrate / 10;
470 if (bitrate < 10) bitrate = 10;
471 if (bitrate > 1000) bitrate = 1000;
473 argp=(
char**) calloc(16,
sizeof(
char*));
475 argp[1] = strdup(
"-i");
476 argp[2] = strdup(
infile.c_str());
477 argp[3] = strdup(
"-b:v");
478 argp[4] = (
char*) calloc(7,
sizeof(
char)); snprintf(argp[4], 7,
"%i0k", bitrate);
479 argp[5] = strdup(
"-s");
480 argp[6] = (
char*) calloc(10,
sizeof(
char)); snprintf(argp[6], 10,
"%ix%i", width, height);
481 argp[7] = strdup(
"-y");
482 argp[8] = strdup(
"-vcodec");
483 argp[9] = strdup(
"mjpeg");
484 argp[10] = strdup(
"-an");
485 argp[11] = strdup(
"-intra");
486 argp[12] = strdup(
"-g");
487 argp[13] = strdup(
"1");
488 argp[14] = strdup(outfile.c_str());
489 argp[15] = (
char *)0;
493 printf(
"TRANSCODE VIDEO:\n");
494 for (
int i=0; i< 15; ++i) {
495 printf(
"%s ", argp[i]);
515 #ifdef PLATFORM_WINDOWS
543 int h,m,s;
char f[7];
546 if (!(t=strstr(d.c_str(),
"time="))) {
return; }
548 if (sscanf(t+5,
"%d:%d:%d.%s",&h,&m,&s,f) == 4) {
553 +
atoi(f) / pow((
double)10, (
int)strlen(f))
555 p = p *
m_fps / 100.0;
566 if (strstr(d.c_str(),
"ERROR") || strstr(d.c_str(),
"Error") || strstr(d.c_str(),
"error")) {
569 if (strncmp(d.c_str(),
"frame=",6)) {
572 d.erase(d.find_last_not_of(
" \t\r\n") + 1);
573 printf(
"ffmpeg: '%s'\n", d.c_str());
int atoi(const string &s)
void ffmpegparse_v(std::string d, size_t s)
PBD::Signal0< void > Terminated
FFSettings default_encoder_settings()
ARDOUR::framecnt_t m_duration
void ParseCSV(const std::string &csv, std::vector< std::vector< std::string > > &lines)
LIBPBD_API Transmitter warning
virtual ~TranscodeFfmpeg()
std::ostream & endmsg(std::ostream &ostr)
bool encode(std::string outfile, std::string inf_a, std::string inf_v, FFSettings ffs, FFSettings meta, bool map=true)
TranscodeFfmpeg(std::string f)
int32_t atol(const string &s)
PBD::Signal2< void, ARDOUR::framecnt_t, ARDOUR::framecnt_t > Progress
void ffmpegparse_a(std::string d, size_t s)
std::map< std::string, std::string > FFSettings
int write_to_stdin(std::string d, size_t len=0)
common functions used for video-file im/export
bool transcode(std::string, const int outwidth=0, const int outheight=0, const int kbitps=0)
bool extract_audio(std::string outfile, ARDOUR::framecnt_t samplerate, unsigned int stream=0)
int start(int stderr_mode=1)
void ffprobeparse(std::string d, size_t s)
#define MAX_FFMPEG_ENCODER_ARGS
#define PARSE_FRACTIONAL_FPS(VAR)
PBD::Signal2< void, std::string, size_t > ReadStdout
PBD::Signal0< void > Finished
char * format_metadata(std::string, std::string)
FFSettings default_meta_data()
ARDOUR::SystemExec * ffcmd
std::string string_compose(const std::string &fmt, const T1 &o1)
double atof(const string &s)