57#if HAVE_SYS_RESOURCE_H
59#include <sys/resource.h>
66#include "libavformat/avformat.h"
68#include "libavcodec/avcodec.h"
69#include "libavcodec/bsf.h"
71#include "libavfilter/avfilter.h"
73#include "libavutil/avassert.h"
74#include "libavutil/avstring.h"
75#include "libavutil/avutil.h"
76#include "libavutil/bprint.h"
77#include "libavutil/channel_layout.h"
78#include "libavutil/getenv_utf8.h"
79#include "libavutil/intreadwrite.h"
80#include "libavutil/fifo.h"
81#include "libavutil/mathematics.h"
82#include "libavutil/opt.h"
83#include "libavutil/parseutils.h"
84#include "libavutil/pixdesc.h"
85#include "libavutil/pixfmt.h"
87#define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
89#define SPECIFIER_OPT_FMT_str "%s"
90#define SPECIFIER_OPT_FMT_i "%i"
91#define SPECIFIER_OPT_FMT_i64 "%"PRId64
92#define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
93#define SPECIFIER_OPT_FMT_f "%f"
94#define SPECIFIER_OPT_FMT_dbl "%lf"
96static const char *
const opt_name_codec_names[] = {
"c",
"codec",
"acodec",
"vcodec",
"scodec",
"dcodec", NULL};
144#define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
146 char namestr[128] = "";\
147 const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
148 for (i = 0; opt_name_##name[i]; i++)\
149 av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[i], opt_name_##name[i+1] ? (opt_name_##name[i+2] ? ", " : " or ") : "");\
150 av_log(NULL, AV_LOG_WARNING, "Multiple %s options specified for stream %d, only the last option '-%s%s%s "SPECIFIER_OPT_FMT_##type"' will be used.\n",\
151 namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
154#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
156 int i, ret, matches = 0;\
158 for (i = 0; i < o->nb_ ## name; i++) {\
159 char *spec = o->name[i].specifier;\
160 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
161 outvar = o->name[i].u.type;\
168 WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
171#define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
174 for (i = 0; i < o->nb_ ## name; i++) {\
175 char *spec = o->name[i].specifier;\
176 if (!strcmp(spec, mediatype))\
177 outvar = o->name[i].u.type;\
233 void *dst = (uint8_t*)o + po->
u.
off;
237 int i, *count = (
int*)(so + 1);
238 for (i = 0; i < *count; i++) {
239 av_freep(&(*so)[i].specifier);
241 av_freep(&(*so)[i].u.str);
260 memset(o, 0,
sizeof(*o));
276 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
278 av_log(NULL,
AV_LOG_STDERR,
"Hardware acceleration methods:\n");
279 while ((type = av_hwdevice_iterate_types(type)) !=
280 AV_HWDEVICE_TYPE_NONE)
281 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
289 const AVDictionaryEntry *e = NULL;
290 AVDictionary *ret = NULL;
292 while ((e = av_dict_get(dict,
"", e, AV_DICT_IGNORE_SUFFIX))) {
293 char *p = strchr(e->key,
':');
297 av_dict_set(&ret, e->key, e->value, 0);
306 if (!av_strcasecmp(arg,
"cfr")) *vsync_var =
VSYNC_CFR;
307 else if (!av_strcasecmp(arg,
"vfr")) *vsync_var =
VSYNC_VFR;
309 else if (!av_strcasecmp(arg,
"drop")) *vsync_var =
VSYNC_DROP;
310 else if (!is_global && !av_strcasecmp(arg,
"auto")) *vsync_var =
VSYNC_AUTO;
311 else if (!is_global) {
312 av_log(NULL, AV_LOG_FATAL,
"Invalid value %s specified for fps_mode of #%d:%d.\n", arg, file_idx, st_idx);
318 av_log(NULL, AV_LOG_WARNING,
"Passing a number to -vsync is deprecated,"
319 " use a string argument as described in the manual.\n");
329 int64_t self_start_time, ref_start_time, self_seek_start, ref_seek_start;
330 int start_times_set = 1;
334 av_log(NULL, AV_LOG_FATAL,
"-isync for input %d references non-existent input %d.\n", i, self->
input_sync_ref);
339 av_log(NULL, AV_LOG_FATAL,
"Use of -isync requires that start_at_zero be set if copyts is set.\n");
345 av_log(NULL, AV_LOG_ERROR,
"-isync for input %d references a resynced input %d. Sync not set.\n", i, self->
input_sync_ref);
349 if (self->
ctx->start_time_realtime != AV_NOPTS_VALUE && ref->
ctx->start_time_realtime != AV_NOPTS_VALUE) {
350 self_start_time = self->
ctx->start_time_realtime;
351 ref_start_time = ref->
ctx->start_time_realtime;
352 }
else if (self->
ctx->start_time != AV_NOPTS_VALUE && ref->
ctx->start_time != AV_NOPTS_VALUE) {
353 self_start_time = self->
ctx->start_time;
354 ref_start_time = ref->
ctx->start_time;
359 if (start_times_set) {
363 adjustment = (self_start_time - ref_start_time) + !
copy_ts*(self_seek_start - ref_seek_start) + ref->
input_ts_offset;
367 av_log(NULL, AV_LOG_INFO,
"Adjusted ts offset for Input #%d by %"PRId64
" us to sync with Input #%d.\n", i, adjustment, self->
input_sync_ref);
369 av_log(NULL, AV_LOG_INFO,
"Unable to identify start times for Inputs #%d and %d both. No sync adjustment made.\n", i, self->
input_sync_ref);
385 static const AVOption opts[] = {
386 {
"abort_on" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, (double)INT64_MAX, .unit =
"flags" },
391 static const AVClass
class = {
393 .item_name = av_default_item_name,
395 .version = LIBAVUTIL_VERSION_INT,
397 const AVClass *pclass = &
class;
399 return av_opt_eval_flags(&pclass, &opts[0], arg, &
abort_on_flags);
406 if (user_stats_period <= 0) {
407 av_log(NULL, AV_LOG_ERROR,
"stats_period %s must be positive.\n", arg);
408 return AVERROR(EINVAL);
412 av_log(NULL, AV_LOG_INFO,
"ffmpeg stats and -progress period set to %s.\n", arg);
441int opt_map(
void *optctx,
const char *opt,
const char *arg)
445 int i, negative = 0, file_idx, disabled = 0;
446 int sync_file_idx = -1, sync_stream_idx = 0;
455 map = av_strdup(arg);
457 return AVERROR(ENOMEM);
460 if ((sync = strchr(map,
','))) {
462 sync_file_idx = strtol(sync + 1, &sync, 0);
464 av_log(NULL, AV_LOG_FATAL,
"Invalid sync file index: %d.\n", sync_file_idx);
476 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s does not "
477 "match any streams.\n", arg);
481 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s matches a disabled input "
490 const char *c = map + 1;
495 av_log(NULL, AV_LOG_ERROR,
"Invalid output link label: %s.\n", map);
499 if ((allow_unused = strchr(map,
'?')))
501 file_idx = strtol(map, &p, 0);
503 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index: %d.\n", file_idx);
513 *p ==
':' ? p + 1 : p) > 0)
519 *p ==
':' ? p + 1 : p) <= 0)
531 if (sync_file_idx >= 0) {
543 av_log(NULL, AV_LOG_VERBOSE,
"Stream map '%s' matches no streams; ignoring.\n", arg);
544 }
else if (disabled) {
545 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches disabled streams.\n"
546 "To ignore this, add a trailing '?' to the map.\n", arg);
549 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches no streams.\n"
550 "To ignore this, add a trailing '?' to the map.\n", arg);
559int opt_attach(
void *optctx,
const char *opt,
const char *arg)
575 mapchan = av_strdup(arg);
577 return AVERROR(ENOMEM);
593 n = sscanf(arg,
"%d.%d.%d:%d.%d",
597 if (n != 3 && n != 5) {
598 av_log(NULL, AV_LOG_FATAL,
"Syntax error, mapchan usage: "
599 "[file.stream.channel|-1][:syncfile:syncstream]\n");
608 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file index: %d\n",
614 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file stream index #%d.%d\n",
619 if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
620 av_log(NULL, AV_LOG_FATAL,
"mapchan: stream #%d.%d is not an audio stream.\n",
625 if ((allow_unused = strchr(mapchan,
'?')))
630 av_log(NULL, AV_LOG_VERBOSE,
"mapchan: invalid audio channel #%d.%d.%d\n",
633 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid audio channel #%d.%d.%d\n"
634 "To ignore this, add a trailing '?' to the map_channel.\n",
652int opt_vaapi_device(
void *optctx,
const char *opt,
const char *arg)
654 const char *prefix =
"vaapi:";
657 tmp = av_asprintf(
"%s%s", prefix, arg);
659 return AVERROR(ENOMEM);
667int opt_qsv_device(
void *optctx,
const char *opt,
const char *arg)
669 const char *prefix =
"qsv=__qsv_device:hw_any,child_device=";
671 char *tmp = av_asprintf(
"%s%s", prefix, arg);
674 return AVERROR(ENOMEM);
685 if (!strcmp(arg,
"list")) {
686 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
687 av_log(NULL,
AV_LOG_STDERR,
"Supported hardware device types:\n");
688 while ((type = av_hwdevice_iterate_types(type)) !=
689 AV_HWDEVICE_TYPE_NONE)
690 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
701 av_log(NULL, AV_LOG_ERROR,
"Only one filter device can be used.\n");
702 return AVERROR(EINVAL);
706 av_log(NULL, AV_LOG_ERROR,
"Invalid filter device %s.\n", arg);
707 return AVERROR(EINVAL);
727 if (*(++arg) && *arg !=
':') {
728 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", arg);
731 *stream_spec = *arg ==
':' ? arg + 1 :
"";
736 *index = strtol(++arg, NULL, 0);
739 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata type %c.\n", *arg);
748 AVDictionary **meta_in = NULL;
749 AVDictionary **meta_out = NULL;
751 char type_in, type_out;
752 const char *istream_spec = NULL, *ostream_spec = NULL;
753 int idx_in = 0, idx_out = 0;
759 if (type_out ==
'g' || !*outspec)
761 if (type_out ==
's' || !*outspec)
763 if (type_out ==
'c' || !*outspec)
768 if (type_in ==
'g' || type_out ==
'g')
770 if (type_in ==
's' || type_out ==
's')
772 if (type_in ==
'c' || type_out ==
'c')
779#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
780 if ((index) < 0 || (index) >= (nb_elems)) {\
781 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
786#define SET_DICT(type, meta, context, index)\
789 meta = &context->metadata;\
792 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
793 meta = &context->chapters[index]->metadata;\
796 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
797 meta = &context->programs[index]->metadata;\
801 default: av_assert0(0);\
804 SET_DICT(type_in, meta_in, ic, idx_in);
805 SET_DICT(type_out, meta_out, oc, idx_out);
808 if (type_in ==
's') {
809 for (i = 0; i < ic->nb_streams; i++) {
811 meta_in = &ic->streams[i]->metadata;
817 av_log(NULL, AV_LOG_FATAL,
"Stream specifier %s does not match any streams.\n", istream_spec);
822 if (type_out ==
's') {
823 for (i = 0; i < oc->nb_streams; i++) {
825 meta_out = &oc->streams[i]->metadata;
826 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
831 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
841 struct tm time = *gmtime((time_t*)&recording_timestamp);
842 if (!strftime(buf,
sizeof(buf),
"creation_time=%Y-%m-%dT%H:%M:%S%z", &time))
846 av_log(NULL, AV_LOG_WARNING,
"%s is deprecated, set the 'creation_time' metadata "
847 "tag instead.\n", opt);
853 const AVCodecDescriptor *desc;
854 const char *codec_string = encoder ?
"encoder" :
"decoder";
855 const AVCodec *codec;
858 avcodec_find_encoder_by_name(name) :
859 avcodec_find_decoder_by_name(name);
861 if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
862 codec = encoder ? avcodec_find_encoder(desc->id) :
863 avcodec_find_decoder(desc->id);
865 av_log(NULL, AV_LOG_VERBOSE,
"Matched %s '%s' for codec '%s'.\n",
866 codec_string, codec->name, desc->name);
870 av_log(NULL, AV_LOG_FATAL,
"Unknown %s '%s'\n", codec_string, name);
874 av_log(NULL, AV_LOG_FATAL,
"Invalid %s type '%s'\n", codec_string, name);
882 char *codec_name = NULL;
886 const AVCodec *codec =
find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
887 st->codecpar->codec_id = codec->id;
888 if (
recast_media && st->codecpar->codec_type != codec->type)
889 st->codecpar->codec_type = codec->type;
892 return avcodec_find_decoder(st->codecpar->codec_id);
901 for (i = 0; i < ic->nb_streams; i++) {
902 AVStream *st = ic->streams[i];
903 AVCodecParameters *par = st->codecpar;
905 char *framerate = NULL, *hwaccel_device = NULL;
906 const char *hwaccel = NULL;
907 char *hwaccel_output_format = NULL;
908 char *codec_tag = NULL;
910 char *discard_str = NULL;
911 const AVClass *cc = avcodec_get_class();
912 const AVOption *discard_opt = av_opt_find(&cc,
"skip_frame", NULL,
913 0, AV_OPT_SEARCH_FAKE_OBJ);
919 st->discard = AVDISCARD_ALL;
933 uint32_t tag = strtol(codec_tag, &next, 0);
935 tag = AV_RL32(codec_tag);
936 st->codecpar->codec_tag = tag;
948 if ((o->
video_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
949 (o->
audio_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
951 (o->
data_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
954 if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &ist->
user_set_discard) < 0) {
955 av_log(NULL, AV_LOG_ERROR,
"Error parsing discard %s.\n",
963 ist->
dec_ctx = avcodec_alloc_context3(ist->
dec);
965 av_log(NULL, AV_LOG_ERROR,
"Error allocating the decoder context.\n");
969 ret = avcodec_parameters_to_context(ist->
dec_ctx, par);
971 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
979 ist->
pkt = av_packet_alloc();
984 ist->
dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
986 switch (par->codec_type) {
987 case AVMEDIA_TYPE_VIDEO:
989 ist->
dec = avcodec_find_decoder(par->codec_id);
992 ist->
dec_ctx->framerate = st->avg_frame_rate;
995 if (framerate && av_parse_video_rate(&ist->
framerate,
997 av_log(NULL, AV_LOG_ERROR,
"Error parsing framerate %s.\n",
1007 hwaccel_output_format, ic, st);
1009 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"cuvid")) {
1010 av_log(NULL, AV_LOG_WARNING,
1011 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
1012 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1013 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
1015 }
else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"qsv")) {
1016 av_log(NULL, AV_LOG_WARNING,
1017 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
1018 "with old commandlines. This behaviour is DEPRECATED and will be removed "
1019 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
1021 }
else if (hwaccel_output_format) {
1024 av_log(NULL, AV_LOG_FATAL,
"Unrecognised hwaccel output "
1025 "format: %s", hwaccel_output_format);
1033 if (!strcmp(hwaccel,
"nvdec") || !strcmp(hwaccel,
"cuvid"))
1036 if (!strcmp(hwaccel,
"none"))
1038 else if (!strcmp(hwaccel,
"auto"))
1041 enum AVHWDeviceType type = av_hwdevice_find_type_by_name(hwaccel);
1042 if (type != AV_HWDEVICE_TYPE_NONE) {
1048 av_log(NULL, AV_LOG_FATAL,
"Unrecognized hwaccel: %s.\n",
1050 av_log(NULL, AV_LOG_FATAL,
"Supported hwaccels: ");
1051 type = AV_HWDEVICE_TYPE_NONE;
1052 while ((type = av_hwdevice_iterate_types(type)) !=
1053 AV_HWDEVICE_TYPE_NONE)
1054 av_log(NULL, AV_LOG_FATAL,
"%s ",
1055 av_hwdevice_get_type_name(type));
1056 av_log(NULL, AV_LOG_FATAL,
"\n");
1063 if (hwaccel_device) {
1072 case AVMEDIA_TYPE_AUDIO:
1077 case AVMEDIA_TYPE_DATA:
1078 case AVMEDIA_TYPE_SUBTITLE: {
1079 char *canvas_size = NULL;
1081 ist->
dec = avcodec_find_decoder(par->codec_id);
1085 av_parse_video_size(&ist->
dec_ctx->width, &ist->
dec_ctx->height, canvas_size) < 0) {
1086 av_log(NULL, AV_LOG_FATAL,
"Invalid canvas size: %s.\n", canvas_size);
1091 case AVMEDIA_TYPE_ATTACHMENT:
1092 case AVMEDIA_TYPE_UNKNOWN:
1098 ret = avcodec_parameters_from_context(par, ist->
dec_ctx);
1100 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
1108 const char *proto_name = avio_find_protocol_name(filename);
1111 av_log(NULL, AV_LOG_FATAL,
"Error, both -y and -n supplied. Exiting.\n");
1116 if (proto_name && !strcmp(proto_name,
"file") && avio_check(filename, 0) == 0) {
1118 av_log(NULL, AV_LOG_FATAL,
"File '%s' already exists. Overwrite? [y/N] ", filename);
1120 signal(SIGINT, SIG_DFL);
1122 av_log(NULL, AV_LOG_FATAL,
"Not overwriting - exiting\n");
1128 av_log(NULL, AV_LOG_FATAL,
"File '%s' already exists. Exiting.\n", filename);
1134 if (proto_name && !strcmp(proto_name,
"file")) {
1137 if (file->
ctx->iformat->flags & AVFMT_NOFILE)
1139 if (!strcmp(filename, file->
ctx->url)) {
1140 av_log(NULL, AV_LOG_FATAL,
"Output %s same as Input #%d - exiting\n", filename, i);
1141 av_log(NULL, AV_LOG_WARNING,
"FFmpeg cannot edit existing files in-place.\n");
1151 AVIOContext *out = NULL;
1152 const AVDictionaryEntry *e;
1154 if (!st->codecpar->extradata_size) {
1155 av_log(NULL, AV_LOG_WARNING,
"No extradata to dump in stream #%d:%d.\n",
1159 if (!*filename && (e = av_dict_get(st->metadata,
"filename", NULL, 0)))
1160 filename = e->value;
1162 av_log(NULL, AV_LOG_FATAL,
"No filename specified and no 'filename' tag"
1169 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &
int_cb, NULL)) < 0) {
1170 av_log(NULL, AV_LOG_FATAL,
"Could not open file %s for writing.\n",
1175 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1183 AVFormatContext *ic;
1184 const AVInputFormat *file_iformat = NULL;
1187 AVDictionary *unused_opts = NULL;
1188 const AVDictionaryEntry *e = NULL;
1189 char * video_codec_name = NULL;
1190 char * audio_codec_name = NULL;
1191 char *subtitle_codec_name = NULL;
1192 char * data_codec_name = NULL;
1193 int scan_all_pmts_set = 0;
1197 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
1203 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
1211 if (!(file_iformat = av_find_input_format(o->
format))) {
1212 av_log(NULL, AV_LOG_FATAL,
"Unknown input format: '%s'\n", o->
format);
1217 if (!strcmp(filename,
"-"))
1221 strcmp(filename,
"/dev/stdin");
1224 ic = avformat_alloc_context();
1233 const AVClass *priv_class;
1234 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1235 av_opt_find(&priv_class,
"ch_layout", NULL, 0,
1236 AV_OPT_SEARCH_FAKE_OBJ)) {
1243 const AVClass *priv_class;
1244 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1245 av_opt_find(&priv_class,
"ch_layout", NULL, 0,
1246 AV_OPT_SEARCH_FAKE_OBJ)) {
1251 const AVClass *priv_class;
1254 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1255 av_opt_find(&priv_class,
"framerate", NULL, 0,
1256 AV_OPT_SEARCH_FAKE_OBJ)) {
1272 if (video_codec_name)
1274 if (audio_codec_name)
1276 if (subtitle_codec_name)
1277 ic->subtitle_codec =
find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
1278 if (data_codec_name)
1281 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1282 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1283 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1284 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1286 ic->flags |= AVFMT_FLAG_NONBLOCK;
1288 ic->flags |= AVFMT_FLAG_BITEXACT;
1289 ic->interrupt_callback =
int_cb;
1291 if (!av_dict_get(o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1292 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts",
"1", AV_DICT_DONT_OVERWRITE);
1293 scan_all_pmts_set = 1;
1296 err = avformat_open_input(&ic, filename, file_iformat, &o->
g->
format_opts);
1299 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1300 av_log(NULL, AV_LOG_ERROR,
"Did you mean file:%s?\n", filename);
1303 if (scan_all_pmts_set)
1304 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1309 for (i = 0; i < ic->nb_streams; i++)
1314 int orig_nb_streams = ic->nb_streams;
1318 ret = avformat_find_stream_info(ic, opts);
1320 for (i = 0; i < orig_nb_streams; i++)
1321 av_dict_free(&opts[i]);
1325 av_log(NULL, AV_LOG_FATAL,
"%s: could not find codec parameters\n", filename);
1326 if (ic->nb_streams == 0) {
1327 avformat_close_input(&ic);
1334 av_log(NULL, AV_LOG_WARNING,
"Cannot use -ss and -sseof both, using -ss for %s\n", filename);
1340 av_log(NULL, AV_LOG_ERROR,
"-sseof value must be negative; aborting\n");
1343 if (ic->duration > 0) {
1346 av_log(NULL, AV_LOG_WARNING,
"-sseof value seeks to before start of file %s; ignored\n", filename);
1350 av_log(NULL, AV_LOG_WARNING,
"Cannot use -sseof, duration of %s not known\n", filename);
1355 timestamp += ic->start_time;
1359 int64_t seek_timestamp = timestamp;
1361 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1362 int dts_heuristic = 0;
1363 for (i=0; i<ic->nb_streams; i++) {
1364 const AVCodecParameters *par = ic->streams[i]->codecpar;
1365 if (par->video_delay) {
1370 if (dts_heuristic) {
1371 seek_timestamp -= 3*AV_TIME_BASE / 23;
1374 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1376 av_log(NULL, AV_LOG_WARNING,
"%s: could not seek to position %0.3f\n",
1377 filename, (
double)timestamp / AV_TIME_BASE);
1405 av_log(NULL, AV_LOG_ERROR,
"Option -readrate for Input #%d is %0.3f; it must be non-negative.\n",
nb_input_files, f->
readrate);
1409 av_log(NULL, AV_LOG_WARNING,
"Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n",
nb_input_files, f->
readrate);
1413 f->
pkt = av_packet_alloc();
1424 while ((e = av_dict_get(
input_streams[i]->decoder_opts,
"", e,
1425 AV_DICT_IGNORE_SUFFIX)))
1426 av_dict_set(&unused_opts, e->key, NULL, 0);
1430 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
1431 const AVClass *
class = avcodec_get_class();
1432 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
1433 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1434 const AVClass *fclass = avformat_get_class();
1435 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1436 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1437 if (!option || foption)
1441 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1442 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
1443 "input file #%d (%s) is not a decoding option.\n", e->key,
1449 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
1450 "input file #%d (%s) has not been used for any stream. The most "
1451 "likely reason is either wrong type (e.g. a video option with "
1452 "no video streams) or that it is a private option of some decoder "
1453 "which was not actually used for any stream.\n", e->key,
1456 av_dict_free(&unused_opts);
1461 for (j = 0; j < ic->nb_streams; j++) {
1462 AVStream *st = ic->streams[j];
1478 while ((c = avio_r8(s)) && c !=
'\n')
1479 av_bprint_chars(bprint, c, 1);
1481 if (!av_bprint_is_complete(bprint)) {
1482 av_log(NULL, AV_LOG_FATAL,
"Could not alloc buffer for reading preset.\n");
1491 char filename[1000];
1492 char *env_avconv_datadir = getenv_utf8(
"AVCONV_DATADIR");
1493 char *env_home = getenv_utf8(
"HOME");
1494 const char *base[3] = { env_avconv_datadir,
1499 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
1503 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
1504 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
1505 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1508 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
1509 i != 1 ?
"" :
"/.avconv", preset_name);
1510 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1513 freeenv_utf8(env_home);
1514 freeenv_utf8(env_avconv_datadir);
1520 enum AVMediaType type = ost->
st->codecpar->codec_type;
1521 char *codec_name = NULL;
1523 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
1526 ost->
st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
1527 NULL, ost->
st->codecpar->codec_type);
1528 ost->
enc = avcodec_find_encoder(ost->
st->codecpar->codec_id);
1530 av_log(NULL, AV_LOG_FATAL,
"Automatic encoder selection failed for "
1531 "output stream #%d:%d. Default encoder for format %s (codec %s) is "
1532 "probably disabled. Please choose an encoder manually.\n",
1534 avcodec_get_name(ost->
st->codecpar->codec_id));
1535 return AVERROR_ENCODER_NOT_FOUND;
1537 }
else if (!strcmp(codec_name,
"copy"))
1541 ost->
st->codecpar->codec_id = ost->
enc->id;
1556 AVStream *st = avformat_new_stream(oc, NULL);
1557 int idx = oc->nb_streams - 1, ret = 0;
1558 const char *bsfs = NULL, *time_base = NULL;
1559 char *next, *codec_tag = NULL;
1564 av_log(NULL, AV_LOG_FATAL,
"Could not alloc stream.\n");
1577 st->codecpar->codec_type = type;
1581 av_log(NULL, AV_LOG_FATAL,
"Error selecting an encoder for stream "
1586 ost->
enc_ctx = avcodec_alloc_context3(ost->
enc);
1588 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding context.\n");
1591 ost->
enc_ctx->codec_type = type;
1593 ost->
ref_par = avcodec_parameters_alloc();
1595 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding parameters.\n");
1603 ost->
pkt = av_packet_alloc();
1608 AVIOContext *s = NULL;
1609 char *buf = NULL, *arg = NULL, *preset = NULL;
1618 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1620 av_bprint_clear(&bprint);
1622 if (!buf[0] || buf[0] ==
'#')
1624 if (!(arg = strchr(buf,
'='))) {
1625 av_log(NULL, AV_LOG_FATAL,
"Invalid line found in the preset file.\n");
1629 av_dict_set(&ost->
encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
1630 }
while (!s->eof_reached);
1631 av_bprint_finalize(&bprint, NULL);
1635 av_log(NULL, AV_LOG_FATAL,
1636 "Preset %s specified for stream %d:%d, but could not be opened.\n",
1646 ost->
enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1651 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1652 q.num <= 0 || q.den <= 0) {
1653 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1662 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1664 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1674 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1675 av_log(NULL, AV_LOG_WARNING,
"Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1684 if (bsfs && *bsfs) {
1685 ret = av_bsf_list_parse_str(bsfs, &ost->
bsf_ctx);
1687 av_log(NULL, AV_LOG_ERROR,
"Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1694 uint32_t tag = strtol(codec_tag, &next, 0);
1696 tag = AV_RL32(codec_tag);
1697 ost->
st->codecpar->codec_tag =
1698 ost->
enc_ctx->codec_tag = tag;
1703 ost->
enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
1704 ost->
enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1721 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1722 ost->
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1727 if (ost->
enc && av_get_exact_bits_per_sample(ost->
enc->id) == 24)
1728 av_dict_set(&ost->
swr_opts,
"output_sample_bits",
"24", 0);
1731 if (source_index >= 0) {
1738 ost->
muxing_queue = av_fifo_alloc2(8,
sizeof(AVPacket*), 0);
1751 const char *p = str;
1758 av_log(NULL, AV_LOG_FATAL,
"Syntax error in matrix \"%s\" at coeff %d\n", str, i);
1768 AVIOContext *pb = NULL;
1769 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1774 av_log(NULL, AV_LOG_ERROR,
"Error opening file %s.\n", filename);
1778 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
1779 ret = avio_read_to_bprint(pb, &bprint, SIZE_MAX);
1782 av_bprint_finalize(&bprint, NULL);
1785 ret = av_bprint_finalize(&bprint, &str);
1794 AVStream *st = ost->
st;
1797 av_log(NULL, AV_LOG_ERROR,
"Both -filter and -filter_script set for "
1805 return av_strdup(ost->
filters);
1807 return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
1815 av_log(NULL, AV_LOG_ERROR,
1816 "%s '%s' was defined for %s output stream %d:%d but codec copy was selected.\n"
1817 "Filtering and streamcopy cannot be used together.\n",
1818 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
1829 AVCodecContext *video_enc;
1830 char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
1837 if (frame_rate && av_parse_video_rate(&ost->
frame_rate, frame_rate) < 0) {
1838 av_log(NULL, AV_LOG_FATAL,
"Invalid framerate value: %s\n", frame_rate);
1843 if (max_frame_rate && av_parse_video_rate(&ost->
max_frame_rate, max_frame_rate) < 0) {
1844 av_log(NULL, AV_LOG_FATAL,
"Invalid maximum framerate value: %s\n", max_frame_rate);
1848 if (frame_rate && max_frame_rate) {
1849 av_log(NULL, AV_LOG_ERROR,
"Only one of -fpsmax and -r can be set for a stream.\n");
1853 if ((frame_rate || max_frame_rate) &&
1855 av_log(NULL, AV_LOG_ERROR,
"Using -vsync passthrough and -r/-fpsmax can produce invalid output files\n");
1858 if (frame_aspect_ratio) {
1860 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1861 q.num <= 0 || q.den <= 0) {
1862 av_log(NULL, AV_LOG_FATAL,
"Invalid aspect ratio: %s\n", frame_aspect_ratio);
1872 const char *p = NULL;
1873 char *frame_size = NULL;
1874 char *frame_pix_fmt = NULL;
1875 char *intra_matrix = NULL, *inter_matrix = NULL;
1876 char *chroma_intra_matrix = NULL;
1881 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
1882 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
1887 if (frame_pix_fmt && *frame_pix_fmt ==
'+') {
1889 if (!*++frame_pix_fmt)
1890 frame_pix_fmt = NULL;
1892 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
1893 av_log(NULL, AV_LOG_FATAL,
"Unknown pixel format requested: %s.\n", frame_pix_fmt);
1896 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
1900 if (!(video_enc->intra_matrix = av_mallocz(
sizeof(*video_enc->intra_matrix) * 64))) {
1901 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1907 if (chroma_intra_matrix) {
1908 uint16_t *p = av_mallocz(
sizeof(*video_enc->chroma_intra_matrix) * 64);
1910 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1913 video_enc->chroma_intra_matrix = p;
1918 if (!(video_enc->inter_matrix = av_mallocz(
sizeof(*video_enc->inter_matrix) * 64))) {
1919 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for inter matrix.\n");
1926 for (i = 0; p; i++) {
1928 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
1930 av_log(NULL, AV_LOG_FATAL,
"error parsing rc_override\n");
1933 video_enc->rc_override =
1934 av_realloc_array(video_enc->rc_override,
1935 i + 1,
sizeof(RcOverride));
1936 if (!video_enc->rc_override) {
1937 av_log(NULL, AV_LOG_FATAL,
"Could not (re)allocate memory for rc_override.\n");
1940 video_enc->rc_override[i].start_frame = start;
1941 video_enc->rc_override[i].end_frame = end;
1943 video_enc->rc_override[i].qscale = q;
1944 video_enc->rc_override[i].quality_factor = 1.0;
1947 video_enc->rc_override[i].qscale = 0;
1948 video_enc->rc_override[i].quality_factor = -q/100.0;
1953 video_enc->rc_override_count = i;
1956 video_enc->flags|= AV_CODEC_FLAG_PSNR;
1962 video_enc->flags |= AV_CODEC_FLAG_PASS1;
1963 av_dict_set(&ost->
encoder_opts,
"flags",
"+pass1", AV_DICT_APPEND);
1966 video_enc->flags |= AV_CODEC_FLAG_PASS2;
1967 av_dict_set(&ost->
encoder_opts,
"flags",
"+pass2", AV_DICT_APPEND);
1977 char logfilename[1024];
1980 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
1984 if (!strcmp(ost->
enc->name,
"libx264")) {
1985 av_dict_set(&ost->
encoder_opts,
"stats", logfilename, AV_DICT_DONT_OVERWRITE);
1987 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
1991 av_log(NULL, AV_LOG_FATAL,
"Error reading log file '%s' for pass-2 encoding\n",
1995 video_enc->stats_in = logbuffer;
1997 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
2000 av_log(NULL, AV_LOG_FATAL,
2001 "Cannot write log file '%s' for pass-1 encoding: %s\n",
2002 logfilename, strerror(errno));
2025 if (!strcmp(oc->oformat->name,
"avi")) {
2028 ost->
vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
2029 ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
2068 AVCodecContext *audio_enc;
2074 audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
2081 char *layout = NULL;
2082 char *sample_fmt = NULL;
2086 audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
2087 audio_enc->ch_layout.nb_channels = channels;
2092 if (av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
2093#if FF_API_OLD_CHANNEL_LAYOUT
2095 AV_NOWARN_DEPRECATED({
2096 mask = av_get_channel_layout(layout);
2100 av_log(NULL, AV_LOG_FATAL,
"Unknown channel layout: %s\n", layout);
2102#if FF_API_OLD_CHANNEL_LAYOUT
2104 av_log(NULL, AV_LOG_WARNING,
"Channel layout '%s' uses a deprecated syntax.\n",
2106 av_channel_layout_from_mask(&audio_enc->ch_layout, mask);
2113 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
2114 av_log(NULL, AV_LOG_FATAL,
"Invalid sample format '%s'\n", sample_fmt);
2137 av_log(NULL, AV_LOG_FATAL,
"Cannot determine input stream for channel mapping %d.%d\n",
2169 av_log(NULL, AV_LOG_FATAL,
"Data stream encoding not supported yet (only streamcopy)\n");
2182 av_log(NULL, AV_LOG_FATAL,
"Unknown stream encoding not supported yet (only streamcopy)\n");
2201 AVCodecContext *subtitle_enc;
2207 subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2210 char *frame_size = NULL;
2213 if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
2214 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
2230 av_strlcpy(idx_str, arg,
sizeof(idx_str));
2231 p = strchr(idx_str,
':');
2233 av_log(NULL, AV_LOG_FATAL,
2234 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
2248 AVFormatContext *is = ifile->
ctx;
2252 tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters,
sizeof(*os->chapters));
2254 return AVERROR(ENOMEM);
2257 for (i = 0; i < is->nb_chapters; i++) {
2258 AVChapter *in_ch = is->chapters[i], *out_ch;
2260 int64_t ts_off = av_rescale_q(start_time - ifile->
ts_offset,
2261 AV_TIME_BASE_Q, in_ch->time_base);
2263 av_rescale_q(ofile->
recording_time, AV_TIME_BASE_Q, in_ch->time_base);
2266 if (in_ch->end < ts_off)
2268 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2271 out_ch = av_mallocz(
sizeof(AVChapter));
2273 return AVERROR(ENOMEM);
2275 out_ch->id = in_ch->id;
2276 out_ch->time_base = in_ch->time_base;
2277 out_ch->start = FFMAX(0, in_ch->start - ts_off);
2278 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2281 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2283 os->chapters[os->nb_chapters++] = out_ch;
2291 int have_default[AVMEDIA_TYPE_NB] = { 0 };
2292 int have_manual = 0;
2295 for (
int i = 0; i < ctx->nb_streams; i++) {
2305 if (ost->
st->disposition & AV_DISPOSITION_DEFAULT)
2306 have_default[ost->
st->codecpar->codec_type] = 1;
2312 for (
int i = 0; i < ctx->nb_streams; i++) {
2319#if LIBAVFORMAT_VERSION_MAJOR >= 60
2320 ret = av_opt_set(ost->
st,
"disposition", ost->
disposition, 0);
2323 const AVClass *
class = av_stream_get_class();
2324 const AVOption *o = av_opt_find(&
class,
"disposition", NULL, 0, AV_OPT_SEARCH_FAKE_OBJ);
2327 ret = av_opt_eval_flags(&
class, o, ost->
disposition, &ost->
st->disposition);
2338 for (
int i = 0; i < ctx->nb_streams; i++) {
2340 enum AVMediaType type = ost->
st->codecpar->codec_type;
2342 if (
nb_streams[type] < 2 || have_default[type] ||
2343 ost->
st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2346 ost->
st->disposition |= AV_DISPOSITION_DEFAULT;
2347 have_default[type] = 1;
2355 AVFormatContext *oc)
2359 switch (ofilter->
type) {
2363 av_log(NULL, AV_LOG_FATAL,
"Only video and audio filters are supported "
2374 av_log(NULL, AV_LOG_ERROR,
"Streamcopy requested for output stream %d:%d, "
2375 "which is fed from a complex filtergraph. Filtering and streamcopy "
2381 const char *opt = ost->
filters ?
"-vf/-af/-filter" :
"-filter_script";
2382 av_log(NULL, AV_LOG_ERROR,
2383 "%s '%s' was specified through the %s option "
2384 "for output stream %d:%d, which is fed from a complex filtergraph.\n"
2385 "%s and -filter_complex cannot be used together for the same stream.\n",
2386 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
2392 avfilter_inout_free(&ofilter->
out_tmp);
2411 if (ost->
enc_ctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
2420 if (!ost->
enc->ch_layouts) {
2423 av_channel_layout_default(&f->
ch_layout, ost->
enc_ctx->ch_layout.nb_channels);
2428 for (i = 0; ost->
enc->ch_layouts[i].nb_channels; i++) {
2429 if (ost->
enc->ch_layouts[i].nb_channels == ost->
enc_ctx->ch_layout.nb_channels)
2432 if (ost->
enc->ch_layouts[i].nb_channels) {
2434 err = av_channel_layout_copy(&f->
ch_layout, &ost->
enc->ch_layouts[i]);
2441 av_channel_layout_default(&f->
ch_layout, ost->
enc_ctx->ch_layout.nb_channels);
2446 AVFormatContext *oc;
2451 AVDictionary *unused_opts = NULL;
2452 const AVDictionaryEntry *e = NULL;
2456 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
2462 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
2479 if (!strcmp(filename,
"-"))
2482 err = avformat_alloc_output_context2(&oc, NULL, o->
format, filename);
2489 of->
format = oc->oformat;
2493 oc->interrupt_callback =
int_cb;
2496 oc->flags |= AVFMT_FLAG_BITEXACT;
2508 switch (ofilter->
type) {
2518 char *subtitle_codec_name = NULL;
2522 if (!o->
video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) {
2523 int best_score = 0, idx = -1;
2524 int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
2527 int file_best_score = 0, file_best_idx = -1;
2531 score = ist->
st->codecpar->width * ist->
st->codecpar->height
2532 + 100000000 * !!(ist->
st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2533 + 5000000*!!(ist->
st->disposition & AV_DISPOSITION_DEFAULT);
2536 if((qcr!=MKTAG(
'A',
'P',
'I',
'C')) && (ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2538 if (ist->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2539 score > file_best_score) {
2540 if((qcr==MKTAG(
'A',
'P',
'I',
'C')) && !(ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2542 file_best_score = score;
2546 if (file_best_idx >= 0) {
2547 if((qcr == MKTAG(
'A',
'P',
'I',
'C')) || !(ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2548 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
2549 if (file_best_score > best_score) {
2550 best_score = file_best_score;
2551 idx = file_best_idx;
2560 if (!o->
audio_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_AUDIO) != AV_CODEC_ID_NONE) {
2561 int best_score = 0, idx = -1;
2564 int file_best_score = 0, file_best_idx = -1;
2568 score = ist->
st->codecpar->ch_layout.nb_channels
2569 + 100000000 * !!(ist->
st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2570 + 5000000*!!(ist->
st->disposition & AV_DISPOSITION_DEFAULT);
2573 if (ist->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2574 score > file_best_score) {
2575 file_best_score = score;
2579 if (file_best_idx >= 0) {
2580 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->
st->disposition & AV_DISPOSITION_DEFAULT);
2581 if (file_best_score > best_score) {
2582 best_score = file_best_score;
2583 idx = file_best_idx;
2593 if (!o->
subtitle_disable && (avcodec_find_encoder(oc->oformat->subtitle_codec) || subtitle_codec_name)) {
2595 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2596 AVCodecDescriptor
const *input_descriptor =
2597 avcodec_descriptor_get(
input_streams[i]->st->codecpar->codec_id);
2598 AVCodecDescriptor
const *output_descriptor = NULL;
2599 AVCodec
const *output_codec =
2600 avcodec_find_encoder(oc->oformat->subtitle_codec);
2601 int input_props = 0, output_props = 0;
2605 output_descriptor = avcodec_descriptor_get(output_codec->id);
2606 if (input_descriptor)
2607 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2608 if (output_descriptor)
2609 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2610 if (subtitle_codec_name ||
2611 input_props & output_props ||
2613 (input_descriptor && output_descriptor &&
2614 (!input_descriptor->props ||
2615 !output_descriptor->props))) {
2623 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_DATA);
2627 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_DATA
2648 if (out && !strcmp(out->name, map->
linklabel)) {
2656 av_log(NULL, AV_LOG_FATAL,
"Output with label '%s' does not exist "
2657 "in any defined filter graph, or was already used elsewhere.\n", map->
linklabel);
2666 av_log(NULL, AV_LOG_FATAL,
"Stream #%d:%d is disabled and cannot be mapped.\n",
2672 if(o-> audio_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2674 if(o-> video_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2676 if(o-> data_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2680 switch (ist->
st->codecpar->codec_type) {
2684 case AVMEDIA_TYPE_DATA: ost =
new_data_stream (o, oc, src_idx);
break;
2686 case AVMEDIA_TYPE_UNKNOWN:
2693 "Cannot map stream #%d:%d - unsupported type.\n",
2696 av_log(NULL, AV_LOG_FATAL,
2697 "If you want unsupported types ignored instead "
2698 "of failing, please use the -ignore_unknown option\n"
2699 "If you want them copied, please use -copy_unknown\n");
2713 uint8_t *attachment;
2717 if ((err = avio_open2(&pb, o->
attachments[i], AVIO_FLAG_READ, &
int_cb, NULL)) < 0) {
2718 av_log(NULL, AV_LOG_FATAL,
"Could not open attachment file %s.\n",
2722 if ((len = avio_size(pb)) <= 0) {
2723 av_log(NULL, AV_LOG_FATAL,
"Could not get size of the attachment %s.\n",
2727 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
2728 !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
2729 av_log(NULL, AV_LOG_FATAL,
"Attachment %s too large.\n",
2733 avio_read(pb, attachment, len);
2734 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2739 ost->
st->codecpar->extradata = attachment;
2740 ost->
st->codecpar->extradata_size = len;
2743 av_dict_set(&ost->
st->metadata,
"filename", (p && *p) ? p + 1 : o->
attachments[i], AV_DICT_DONT_OVERWRITE);
2747 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2749 av_log(NULL, AV_LOG_ERROR,
"Output file #%d does not contain any stream\n",
nb_output_files - 1);
2758 AV_DICT_IGNORE_SUFFIX)))
2759 av_dict_set(&unused_opts, e->key, NULL, 0);
2763 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
2764 const AVClass *
class = avcodec_get_class();
2765 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
2766 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2767 const AVClass *fclass = avformat_get_class();
2768 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
2769 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2770 if (!option || foption)
2774 if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
2775 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
2776 "output file #%d (%s) is not an encoding option.\n", e->key,
2783 if (!strcmp(e->key,
"gop_timecode"))
2786 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
2787 "output file #%d (%s) has not been used for any stream. The most "
2788 "likely reason is either wrong type (e.g. a video option with "
2789 "no video streams) or that it is a private option of some encoder "
2790 "which was not actually used for any stream.\n", e->key,
2793 av_dict_free(&unused_opts);
2804 if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2805 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2808 av_log(NULL, AV_LOG_ERROR,
2809 "Error initializing a simple filtergraph between streams "
2823 switch (ost->
enc_ctx->codec_type) {
2824 case AVMEDIA_TYPE_VIDEO:
2828 if (ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
2834 case AVMEDIA_TYPE_AUDIO:
2835 if (ost->
enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
2840 if (ost->
enc_ctx->sample_rate) {
2845 if (ost->
enc_ctx->ch_layout.nb_channels) {
2847 }
else if (ost->
enc->ch_layouts) {
2856 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2857 if (!av_filename_number_test(oc->url)) {
2864 av_log(NULL, AV_LOG_ERROR,
2865 "No input streams but output needs an input stream\n");
2869 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2874 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
2875 &oc->interrupt_callback,
2880 }
else if (strcmp(oc->oformat->name,
"image2")==0 && !av_filename_number_test(filename))
2884 av_dict_set_int(&of->
opts,
"preload", o->
mux_preload*AV_TIME_BASE, 0);
2894 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d while processing metadata maps\n", in_file_index);
2898 in_file_index >= 0 ?
2913 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d in chapter mapping.\n",
2925 AV_DICT_DONT_OVERWRITE);
2927 av_dict_set(&oc->metadata,
"duration", NULL, 0);
2928 av_dict_set(&oc->metadata,
"creation_time", NULL, 0);
2929 av_dict_set(&oc->metadata,
"company_name", NULL, 0);
2930 av_dict_set(&oc->metadata,
"product_name", NULL, 0);
2931 av_dict_set(&oc->metadata,
"product_version", NULL, 0);
2939 av_dict_copy(&
output_streams[i]->st->metadata, ist->
st->metadata, AV_DICT_DONT_OVERWRITE);
2941 av_dict_set(&
output_streams[i]->st->metadata,
"encoder", NULL, 0);
2952 const char *p2 = av_get_token(&p,
":");
2953 const char *to_dealloc = p2;
2960 key = av_get_token(&p2,
"=");
2962 av_freep(&to_dealloc);
2968 if (!strcmp(key,
"program_num"))
2969 progid = strtol(p2, NULL, 0);
2970 av_freep(&to_dealloc);
2974 program = av_new_program(oc, progid);
2978 const char *p2 = av_get_token(&p,
":");
2979 const char *to_dealloc = p2;
2985 key = av_get_token(&p2,
"=");
2987 av_log(NULL, AV_LOG_FATAL,
2988 "No '=' character in program string %s.\n",
2996 if (!strcmp(key,
"title")) {
2997 av_dict_set(&program->metadata,
"title", p2, 0);
2998 }
else if (!strcmp(key,
"program_num")) {
2999 }
else if (!strcmp(key,
"st")) {
3000 int st_num = strtol(p2, NULL, 0);
3001 av_program_add_stream_index(oc, progid, st_num);
3003 av_log(NULL, AV_LOG_FATAL,
"Unknown program key %s.\n", key);
3006 av_freep(&to_dealloc);
3015 const char *stream_spec;
3016 int index = 0, j, ret = 0;
3020 av_log(NULL, AV_LOG_FATAL,
"No '=' character in metadata string %s.\n",
3028 for (j = 0; j < oc->nb_streams; j++) {
3033 double theta = av_strtod(val, &tail);
3039 av_dict_set(&oc->streams[j]->metadata, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
3051 if (index < 0 || index >= oc->nb_chapters) {
3052 av_log(NULL, AV_LOG_FATAL,
"Invalid chapter index %d in metadata specifier.\n", index);
3055 m = &oc->chapters[index]->metadata;
3058 if (index < 0 || index >= oc->nb_programs) {
3059 av_log(NULL, AV_LOG_FATAL,
"Invalid program index %d in metadata specifier.\n", index);
3062 m = &oc->programs[index]->metadata;
3065 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", o->
metadata[i].
specifier);
3068 av_dict_set(m, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
3074 av_log(NULL, AV_LOG_FATAL,
"Error setting output stream dispositions\n");
3085 enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
3086 static const char *
const frame_rates[] = {
"25",
"30000/1001",
"24000/1001" };
3088 if (!strncmp(arg,
"pal-", 4)) {
3091 }
else if (!strncmp(arg,
"ntsc-", 5)) {
3094 }
else if (!strncmp(arg,
"film-", 5)) {
3105 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
3107 fr = st->time_base.den * 1000LL / st->time_base.num;
3111 }
else if ((fr == 29970) || (fr == 23976)) {
3116 if (norm != UNKNOWN)
3120 if (norm != UNKNOWN)
3121 av_log(NULL, AV_LOG_INFO,
"Assuming %s for target.\n", norm == PAL ?
"PAL" :
"NTSC");
3124 if (norm == UNKNOWN) {
3125 av_log(NULL, AV_LOG_FATAL,
"Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
3126 av_log(NULL, AV_LOG_FATAL,
"Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
3127 av_log(NULL, AV_LOG_FATAL,
"or set a framerate with \"-r xxx\".\n");
3131 if (!strcmp(arg,
"vcd")) {
3136 parse_option(o,
"s", norm == PAL ?
"352x288" :
"352x240", options);
3138 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
3158 }
else if (!strcmp(arg,
"svcd")) {
3164 parse_option(o,
"s", norm == PAL ?
"480x576" :
"480x480", options);
3167 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
3180 }
else if (!strcmp(arg,
"dvd")) {
3186 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
3189 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
3202 }
else if (!strncmp(arg,
"dv", 2)) {
3206 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
3207 parse_option(o,
"pix_fmt", !strncmp(arg,
"dv50", 4) ?
"yuv422p" :
3208 norm == PAL ?
"yuv420p" :
"yuv411p", options);
3215 av_log(NULL, AV_LOG_ERROR,
"Unknown target: %s\n", arg);
3216 return AVERROR(EINVAL);
3235 time_t today2 = time(NULL);
3236 struct tm *today = localtime(&today2);
3239 av_log(NULL, AV_LOG_FATAL,
"Unable to get current time: %s\n", strerror(errno));
3243 snprintf(filename,
sizeof(filename),
"vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
3290 char filename[1000], line[1000], tmp_line[1000];
3291 const char *codec_name = NULL;
3297 if (!(f =
get_preset_file(filename,
sizeof(filename), arg, *opt ==
'f', codec_name))) {
3298 if(!strncmp(arg,
"libx264-lossless", strlen(
"libx264-lossless"))){
3299 av_log(NULL, AV_LOG_FATAL,
"Please use -preset <speed> -qp 0\n");
3301 av_log(NULL, AV_LOG_FATAL,
"File for preset '%s' not found\n", arg);
3305 while (fgets(line,
sizeof(line), f)) {
3306 char *key = tmp_line, *value, *endptr;
3308 if (strcspn(line,
"#\n\r") == 0)
3310 av_strlcpy(tmp_line, line,
sizeof(tmp_line));
3311 if (!av_strtok(key,
"=", &value) ||
3312 !av_strtok(value,
"\r\n", &endptr)) {
3313 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid syntax: '%s'\n", filename, line);
3316 av_log(NULL, AV_LOG_DEBUG,
"ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
3323 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
3324 filename, line, key, value);
3338 char *s = av_asprintf(
"%s:%c", opt + 1, *opt);
3340 return AVERROR(ENOMEM);
3350 if(!strcmp(opt,
"ab")){
3353 }
else if(!strcmp(opt,
"b")){
3354 av_log(NULL, AV_LOG_WARNING,
"Please use -b:a or -b:v, -b is ambiguous\n");
3368 if(!strcmp(opt,
"qscale")){
3369 av_log(NULL, AV_LOG_WARNING,
"Please use -q:a or -q:v, -qscale is ambiguous\n");
3372 s = av_asprintf(
"q%s", opt + 6);
3374 return AVERROR(ENOMEM);
3383 if(!strcmp(opt,
"profile")){
3384 av_log(NULL, AV_LOG_WARNING,
"Please use -profile:a or -profile:v, -profile is ambiguous\n");
3385 av_dict_set(&o->
g->
codec_opts,
"profile:v", arg, 0);
3404int opt_vsync(
void *optctx,
const char *opt,
const char *arg)
3406 av_log(NULL, AV_LOG_WARNING,
"-vsync is deprecated. Use -fps_mode\n");
3415 char *tcr = av_asprintf(
"timecode=%s", arg);
3417 return AVERROR(ENOMEM);
3420 ret = av_dict_set(&o->
g->
codec_opts,
"gop_timecode", arg, 0);
3438 return AVERROR(ENOMEM);
3450 return AVERROR(EINVAL);
3466 int show_advanced = 0, show_avoptions = 0;
3469 if (!strcmp(opt,
"long"))
3471 else if (!strcmp(opt,
"full"))
3472 show_advanced = show_avoptions = 1;
3474 av_log(NULL, AV_LOG_ERROR,
"Unknown help option '%s'.\n", opt);
3480 " -h -- print basic options\n"
3481 " -h long -- print more options\n"
3482 " -h full -- print all options (including all format and codec specific options, very long)\n"
3483 " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol\n"
3484 " See man %s for detailed description of the options.\n"
3491 "instead of just one file):",
3519 if (show_avoptions) {
3520 int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
3526#if CONFIG_SWRESAMPLE
3529 show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM);
3530 show_help_children(av_bsf_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM);
3536 av_log(NULL, AV_LOG_INFO,
"Hyper fast Audio and Video encoder\n");
3537 av_log(NULL, AV_LOG_INFO,
"usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n",
program_name);
3538 av_log(NULL, AV_LOG_INFO,
"\n");
3565 av_log(NULL, AV_LOG_ERROR,
"Error parsing options for %s file "
3566 "%s.\n", inout, g->
arg);
3571 av_log(NULL, AV_LOG_DEBUG,
"Opening an %s file: %s.\n", inout, g->
arg);
3572 ret = open_file(&o, g->
arg);
3575 av_log(NULL, AV_LOG_ERROR,
"Error opening %s file %s.\n",
3579 av_log(NULL, AV_LOG_DEBUG,
"Successfully opened the file.\n");
3591 memset(&octx, 0,
sizeof(octx));
3597 av_log(NULL, AV_LOG_FATAL,
"Error splitting the argument list: ");
3604 av_log(NULL, AV_LOG_FATAL,
"Error parsing global options: ");
3614 av_log(NULL, AV_LOG_FATAL,
"Error opening input files: ");
3623 av_log(NULL, AV_LOG_FATAL,
"Error initializing complex filters.\n");
3630 av_log(NULL, AV_LOG_FATAL,
"Error opening output files: ");
3639 av_strerror(ret, error,
sizeof(error));
3640 av_log(NULL, AV_LOG_FATAL,
"%s\n", error);
3647 AVIOContext *avio = NULL;
3650 if (!strcmp(arg,
"-"))
3652 ret = avio_open2(&avio, arg, AVIO_FLAG_WRITE, &
int_cb, NULL);
3654 av_log(NULL, AV_LOG_ERROR,
"Failed to open progress URL \"%s\": %s\n",
3655 arg, av_err2str(ret));
3666 struct rlimit rl = { lim, lim + 1 };
3667 if (setrlimit(RLIMIT_CPU, &rl))
3668 perror(
"setrlimit");
3670 av_log(NULL, AV_LOG_WARNING,
"-%s not implemented on this OS\n", opt);
AVDictionary * codec_opts
AVDictionary * format_opts
SpecifierOpt * metadata_map
SpecifierOpt * frame_pix_fmts
SpecifierOpt * dump_attachment
SpecifierOpt * audio_sample_rate
int nb_audio_channel_maps
SpecifierOpt * max_frames
int metadata_global_manual
SpecifierOpt * frame_sizes
int metadata_streams_manual
SpecifierOpt * audio_channels
SpecifierOpt * frame_rates
int metadata_chapters_manual
SpecifierOpt * audio_ch_layouts
const char ** attachments
AudioChannelMap * audio_channel_maps
const AVOutputFormat * format
int64_t start_time
start time in microseconds == AV_TIME_BASE units
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
struct OutputStream * ost
const AVChannelLayout * ch_layouts
AVChannelLayout ch_layout
int max_muxing_queue_size
int copy_initial_nonkeyframes
AVRational frame_aspect_ratio
double rotate_override_value
int audio_channels_mapped
size_t muxing_queue_data_threshold
enum VideoSyncMethod vsync_method
AVRational max_frame_rate
AVCodecParameters * ref_par
const char * attachment_filename
struct InputStream * sync_ist
AVDictionary * encoder_opts
char * filters
filtergraph associated to the -filter option
int64_t forced_kf_ref_pts
char * filters_script
filtergraph script associated to the -filter_script option
size_t muxing_queue_data_size