45#include "libavformat/avformat.h"
47#include "libavcodec/avcodec.h"
48#include "libavcodec/bsf.h"
50#include "libavfilter/avfilter.h"
52#include "libavutil/avassert.h"
53#include "libavutil/avstring.h"
54#include "libavutil/avutil.h"
55#include "libavutil/channel_layout.h"
56#include "libavutil/intreadwrite.h"
57#include "libavutil/fifo.h"
58#include "libavutil/mathematics.h"
59#include "libavutil/opt.h"
60#include "libavutil/parseutils.h"
61#include "libavutil/pixdesc.h"
62#include "libavutil/pixfmt.h"
64#define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
66#define SPECIFIER_OPT_FMT_str "%s"
67#define SPECIFIER_OPT_FMT_i "%i"
68#define SPECIFIER_OPT_FMT_i64 "%"PRId64
69#define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
70#define SPECIFIER_OPT_FMT_f "%f"
71#define SPECIFIER_OPT_FMT_dbl "%lf"
73static const char *
const opt_name_codec_names[] = {
"c",
"codec",
"acodec",
"vcodec",
"scodec",
"dcodec", NULL};
118#define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
120 char namestr[128] = "";\
121 const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
122 for (i = 0; opt_name_##name[i]; i++)\
123 av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[i], opt_name_##name[i+1] ? (opt_name_##name[i+2] ? ", " : " or ") : "");\
124 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",\
125 namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
128#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
130 int i, ret, matches = 0;\
132 for (i = 0; i < o->nb_ ## name; i++) {\
133 char *spec = o->name[i].specifier;\
134 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
135 outvar = o->name[i].u.type;\
142 WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
145#define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
148 for (i = 0; i < o->nb_ ## name; i++) {\
149 char *spec = o->name[i].specifier;\
150 if (!strcmp(spec, mediatype))\
151 outvar = o->name[i].u.type;\
156#if CONFIG_VIDEOTOOLBOX
217 void *dst = (uint8_t*)o + po->
u.
off;
221 int i, *count = (
int*)(so + 1);
222 for (i = 0; i < *count; i++) {
223 av_freep(&(*so)[i].specifier);
225 av_freep(&(*so)[i].u.str);
244 memset(o, 0,
sizeof(*o));
259 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
261 av_log(NULL,
AV_LOG_STDERR,
"Hardware acceleration methods:\n");
262 while ((type = av_hwdevice_iterate_types(type)) !=
263 AV_HWDEVICE_TYPE_NONE)
264 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
272 AVDictionaryEntry *e = NULL;
273 AVDictionary *ret = NULL;
275 while ((e = av_dict_get(dict,
"", e, AV_DICT_IGNORE_SUFFIX))) {
276 char *p = strchr(e->key,
':');
280 av_dict_set(&ret, e->key, e->value, 0);
289 const AVOption opts[] = {
290 {
"abort_on" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, (double)INT64_MAX, .unit =
"flags" },
295 const AVClass
class = {
297 .item_name = av_default_item_name,
299 .version = LIBAVUTIL_VERSION_INT,
301 const AVClass *pclass = &
class;
303 return av_opt_eval_flags(&pclass, &opts[0], arg, &
abort_on_flags);
310 if (user_stats_period <= 0) {
311 av_log(NULL, AV_LOG_ERROR,
"stats_period %s must be positive.\n", arg);
312 return AVERROR(EINVAL);
316 av_log(NULL, AV_LOG_INFO,
"ffmpeg stats and -progress period set to %s.\n", arg);
321int opt_sameq(
void *optctx,
const char *opt,
const char *arg)
323 av_log(NULL, AV_LOG_ERROR,
"Option '%s' was removed. "
324 "If you are looking for an option to preserve the quality (which is not "
325 "what -%s was for), use -qscale 0 or an equivalent quality factor option.\n",
327 return AVERROR(EINVAL);
332 av_log(NULL, AV_LOG_WARNING,
"This option is deprecated, use -channel.\n");
338 av_log(NULL, AV_LOG_WARNING,
"This option is deprecated, use -standard.\n");
366int opt_map(
void *optctx,
const char *opt,
const char *arg)
370 int i, negative = 0, file_idx, disabled = 0;
371 int sync_file_idx = -1, sync_stream_idx = 0;
380 map = av_strdup(arg);
382 return AVERROR(ENOMEM);
385 if ((sync = strchr(map,
','))) {
387 sync_file_idx = strtol(sync + 1, &sync, 0);
389 av_log(NULL, AV_LOG_FATAL,
"Invalid sync file index: %d.\n", sync_file_idx);
401 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s does not "
402 "match any streams.\n", arg);
406 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s matches a disabled input "
415 const char *c = map + 1;
420 av_log(NULL, AV_LOG_ERROR,
"Invalid output link label: %s.\n", map);
424 if ((allow_unused = strchr(map,
'?')))
426 file_idx = strtol(map, &p, 0);
428 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index: %d.\n", file_idx);
438 *p ==
':' ? p + 1 : p) > 0)
444 *p ==
':' ? p + 1 : p) <= 0)
456 if (sync_file_idx >= 0) {
468 av_log(NULL, AV_LOG_VERBOSE,
"Stream map '%s' matches no streams; ignoring.\n", arg);
469 }
else if (disabled) {
470 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches disabled streams.\n"
471 "To ignore this, add a trailing '?' to the map.\n", arg);
474 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches no streams.\n"
475 "To ignore this, add a trailing '?' to the map.\n", arg);
484int opt_attach(
void *optctx,
const char *opt,
const char *arg)
500 mapchan = av_strdup(arg);
502 return AVERROR(ENOMEM);
518 n = sscanf(arg,
"%d.%d.%d:%d.%d",
522 if (n != 3 && n != 5) {
523 av_log(NULL, AV_LOG_FATAL,
"Syntax error, mapchan usage: "
524 "[file.stream.channel|-1][:syncfile:syncstream]\n");
533 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file index: %d\n",
539 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file stream index #%d.%d\n",
544 if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
545 av_log(NULL, AV_LOG_FATAL,
"mapchan: stream #%d.%d is not an audio stream.\n",
550 if ((allow_unused = strchr(mapchan,
'?')))
555 av_log(NULL, AV_LOG_VERBOSE,
"mapchan: invalid audio channel #%d.%d.%d\n",
558 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid audio channel #%d.%d.%d\n"
559 "To ignore this, add a trailing '?' to the map_channel.\n",
577int opt_vaapi_device(
void *optctx,
const char *opt,
const char *arg)
579 const char *prefix =
"vaapi:";
582 tmp = av_asprintf(
"%s%s", prefix, arg);
584 return AVERROR(ENOMEM);
592static int opt_qsv_device(
void *optctx,
const char *opt,
const char *arg)
594 const char *prefix =
"qsv=__qsv_device:hw_any,child_device=";
596 char *tmp = av_asprintf(
"%s%s", prefix, arg);
599 return AVERROR(ENOMEM);
610 if (!strcmp(arg,
"list")) {
611 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
612 av_log(NULL,
AV_LOG_STDERR,
"Supported hardware device types:\n");
613 while ((type = av_hwdevice_iterate_types(type)) !=
614 AV_HWDEVICE_TYPE_NONE)
615 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
626 av_log(NULL, AV_LOG_ERROR,
"Only one filter device can be used.\n");
627 return AVERROR(EINVAL);
631 av_log(NULL, AV_LOG_ERROR,
"Invalid filter device %s.\n", arg);
632 return AVERROR(EINVAL);
652 if (*(++arg) && *arg !=
':') {
653 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", arg);
656 *stream_spec = *arg ==
':' ? arg + 1 :
"";
661 *
index = strtol(++arg, NULL, 0);
664 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata type %c.\n", *arg);
673 AVDictionary **meta_in = NULL;
674 AVDictionary **meta_out = NULL;
676 char type_in, type_out;
677 const char *istream_spec = NULL, *ostream_spec = NULL;
678 int idx_in = 0, idx_out = 0;
684 if (type_out ==
'g' || !*outspec)
686 if (type_out ==
's' || !*outspec)
688 if (type_out ==
'c' || !*outspec)
693 if (type_in ==
'g' || type_out ==
'g')
695 if (type_in ==
's' || type_out ==
's')
697 if (type_in ==
'c' || type_out ==
'c')
704#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
705 if ((index) < 0 || (index) >= (nb_elems)) {\
706 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
711#define SET_DICT(type, meta, context, index)\
714 meta = &context->metadata;\
717 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
718 meta = &context->chapters[index]->metadata;\
721 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
722 meta = &context->programs[index]->metadata;\
726 default: av_assert0(0);\
729 SET_DICT(type_in, meta_in, ic, idx_in);
730 SET_DICT(type_out, meta_out, oc, idx_out);
733 if (type_in ==
's') {
734 for (i = 0; i < ic->nb_streams; i++) {
736 meta_in = &ic->streams[i]->metadata;
742 av_log(NULL, AV_LOG_FATAL,
"Stream specifier %s does not match any streams.\n", istream_spec);
747 if (type_out ==
's') {
748 for (i = 0; i < oc->nb_streams; i++) {
750 meta_out = &oc->streams[i]->metadata;
751 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
756 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
766 struct tm time = *gmtime((time_t*)&recording_timestamp);
767 if (!strftime(buf,
sizeof(buf),
"creation_time=%Y-%m-%dT%H:%M:%S%z", &time))
771 av_log(NULL, AV_LOG_WARNING,
"%s is deprecated, set the 'creation_time' metadata "
772 "tag instead.\n", opt);
778 const AVCodecDescriptor *desc;
779 const char *codec_string = encoder ?
"encoder" :
"decoder";
780 const AVCodec *codec;
783 avcodec_find_encoder_by_name(name) :
784 avcodec_find_decoder_by_name(name);
786 if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
787 codec = encoder ? avcodec_find_encoder(desc->id) :
788 avcodec_find_decoder(desc->id);
790 av_log(NULL, AV_LOG_VERBOSE,
"Matched %s '%s' for codec '%s'.\n",
791 codec_string, codec->name, desc->name);
795 av_log(NULL, AV_LOG_FATAL,
"Unknown %s '%s'\n", codec_string, name);
799 av_log(NULL, AV_LOG_FATAL,
"Invalid %s type '%s'\n", codec_string, name);
807 char *codec_name = NULL;
811 const AVCodec *codec =
find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
812 st->codecpar->codec_id = codec->id;
813 if (
recast_media && st->codecpar->codec_type != codec->type)
814 st->codecpar->codec_type = codec->type;
817 return avcodec_find_decoder(st->codecpar->codec_id);
826 for (i = 0; i < ic->nb_streams; i++) {
827 AVStream *st = ic->streams[i];
828 AVCodecParameters *par = st->codecpar;
830 char *framerate = NULL, *hwaccel_device = NULL;
831 const char *hwaccel = NULL;
832 char *hwaccel_output_format = NULL;
833 char *codec_tag = NULL;
835 char *discard_str = NULL;
836 const AVClass *cc = avcodec_get_class();
837 const AVOption *discard_opt = av_opt_find(&cc,
"skip_frame", NULL, 0, 0);
848 st->discard = AVDISCARD_ALL;
850 ist->first_dts = AV_NOPTS_VALUE;
851 ist->min_pts = INT64_MAX;
852 ist->max_pts = INT64_MIN;
862 uint32_t tag = strtol(codec_tag, &next, 0);
864 tag = AV_RL32(codec_tag);
865 st->codecpar->codec_tag = tag;
871 ist->reinit_filters = -1;
875 ist->user_set_discard = AVDISCARD_NONE;
877 if ((o->
video_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
878 (o->
audio_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
880 (o->
data_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
881 ist->user_set_discard = AVDISCARD_ALL;
883 if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &
ist->user_set_discard) < 0) {
884 av_log(NULL, AV_LOG_ERROR,
"Error parsing discard %s.\n",
889 ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
891 ist->dec_ctx = avcodec_alloc_context3(
ist->dec);
893 av_log(NULL, AV_LOG_ERROR,
"Error allocating the decoder context.\n");
897 ret = avcodec_parameters_to_context(
ist->dec_ctx, par);
899 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
904 ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
906 switch (par->codec_type) {
907 case AVMEDIA_TYPE_VIDEO:
909 ist->dec = avcodec_find_decoder(par->codec_id);
912 ist->dec_ctx->framerate = st->avg_frame_rate;
915 if (framerate && av_parse_video_rate(&
ist->framerate,
917 av_log(NULL, AV_LOG_ERROR,
"Error parsing framerate %s.\n",
922 ist->top_field_first = -1;
927 hwaccel_output_format, ic, st);
929 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"cuvid")) {
930 av_log(NULL, AV_LOG_WARNING,
931 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
932 "with old commandlines. This behaviour is DEPRECATED and will be removed "
933 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
934 ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
935 }
else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"qsv")) {
936 av_log(NULL, AV_LOG_WARNING,
937 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
938 "with old commandlines. This behaviour is DEPRECATED and will be removed "
939 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
940 ist->hwaccel_output_format = AV_PIX_FMT_QSV;
941 }
else if (hwaccel_output_format) {
942 ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
943 if (
ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
944 av_log(NULL, AV_LOG_FATAL,
"Unrecognised hwaccel output "
945 "format: %s", hwaccel_output_format);
948 ist->hwaccel_output_format = AV_PIX_FMT_NONE;
953 if (!strcmp(hwaccel,
"nvdec") || !strcmp(hwaccel,
"cuvid"))
956 if (!strcmp(hwaccel,
"none"))
958 else if (!strcmp(hwaccel,
"auto"))
961 enum AVHWDeviceType type;
964 if (!strcmp(
hwaccels[i].name, hwaccel)) {
970 if (!
ist->hwaccel_id) {
971 type = av_hwdevice_find_type_by_name(hwaccel);
972 if (type != AV_HWDEVICE_TYPE_NONE) {
974 ist->hwaccel_device_type = type;
978 if (!
ist->hwaccel_id) {
979 av_log(NULL, AV_LOG_FATAL,
"Unrecognized hwaccel: %s.\n",
981 av_log(NULL, AV_LOG_FATAL,
"Supported hwaccels: ");
982 type = AV_HWDEVICE_TYPE_NONE;
983 while ((type = av_hwdevice_iterate_types(type)) !=
984 AV_HWDEVICE_TYPE_NONE)
985 av_log(NULL, AV_LOG_FATAL,
"%s ",
986 av_hwdevice_get_type_name(type));
987 av_log(NULL, AV_LOG_FATAL,
"\n");
994 if (hwaccel_device) {
995 ist->hwaccel_device = av_strdup(hwaccel_device);
996 if (!
ist->hwaccel_device)
1000 ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
1003 case AVMEDIA_TYPE_AUDIO:
1004 ist->guess_layout_max = INT_MAX;
1008 case AVMEDIA_TYPE_DATA:
1009 case AVMEDIA_TYPE_SUBTITLE: {
1010 char *canvas_size = NULL;
1012 ist->dec = avcodec_find_decoder(par->codec_id);
1016 av_parse_video_size(&
ist->dec_ctx->width, &
ist->dec_ctx->height, canvas_size) < 0) {
1017 av_log(NULL, AV_LOG_FATAL,
"Invalid canvas size: %s.\n", canvas_size);
1022 case AVMEDIA_TYPE_ATTACHMENT:
1023 case AVMEDIA_TYPE_UNKNOWN:
1029 ret = avcodec_parameters_from_context(par,
ist->dec_ctx);
1031 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
1039 const char *proto_name = avio_find_protocol_name(filename);
1042 fprintf(stderr,
"Error, both -y and -n supplied. Exiting.\n");
1047 if (proto_name && !strcmp(proto_name,
"file") && avio_check(filename, 0) == 0) {
1049 fprintf(stderr,
"File '%s' already exists. Overwrite ? [y/N] ", filename);
1052 signal(SIGINT, SIG_DFL);
1054 av_log(NULL, AV_LOG_FATAL,
"Not overwriting - exiting\n");
1060 av_log(NULL, AV_LOG_FATAL,
"File '%s' already exists. Exiting.\n", filename);
1066 if (proto_name && !strcmp(proto_name,
"file")) {
1069 if (file->
ctx->iformat->flags & AVFMT_NOFILE)
1071 if (!strcmp(filename, file->
ctx->url)) {
1072 av_log(NULL, AV_LOG_FATAL,
"Output %s same as Input #%d - exiting\n", filename, i);
1073 av_log(NULL, AV_LOG_WARNING,
"FFmpeg cannot edit existing files in-place.\n");
1083 AVIOContext *out = NULL;
1084 AVDictionaryEntry *e;
1086 if (!st->codecpar->extradata_size) {
1087 av_log(NULL, AV_LOG_WARNING,
"No extradata to dump in stream #%d:%d.\n",
1091 if (!*filename && (e = av_dict_get(st->metadata,
"filename", NULL, 0)))
1092 filename = e->value;
1094 av_log(NULL, AV_LOG_FATAL,
"No filename specified and no 'filename' tag"
1101 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &
int_cb, NULL)) < 0) {
1102 av_log(NULL, AV_LOG_FATAL,
"Could not open file %s for writing.\n",
1107 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1115 AVFormatContext *ic;
1116 const AVInputFormat *file_iformat = NULL;
1119 AVDictionary *unused_opts = NULL;
1120 AVDictionaryEntry *e = NULL;
1121 char * video_codec_name = NULL;
1122 char * audio_codec_name = NULL;
1123 char *subtitle_codec_name = NULL;
1124 char * data_codec_name = NULL;
1125 int scan_all_pmts_set = 0;
1129 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
1135 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
1143 if (!(file_iformat = av_find_input_format(o->
format))) {
1144 av_log(NULL, AV_LOG_FATAL,
"Unknown input format: '%s'\n", o->
format);
1149 if (!strcmp(filename,
"-"))
1153 strcmp(filename,
"/dev/stdin");
1156 ic = avformat_alloc_context();
1165 const AVClass *priv_class;
1169 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1170 av_opt_find(&priv_class,
"channels", NULL, 0,
1171 AV_OPT_SEARCH_FAKE_OBJ)) {
1176 const AVClass *priv_class;
1179 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1180 av_opt_find(&priv_class,
"framerate", NULL, 0,
1181 AV_OPT_SEARCH_FAKE_OBJ)) {
1197 if (video_codec_name)
1199 if (audio_codec_name)
1201 if (subtitle_codec_name)
1202 ic->subtitle_codec =
find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
1203 if (data_codec_name)
1206 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1207 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1208 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1209 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1211 ic->flags |= AVFMT_FLAG_NONBLOCK;
1213 ic->flags |= AVFMT_FLAG_BITEXACT;
1214 ic->interrupt_callback =
int_cb;
1216 if (!av_dict_get(o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1217 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts",
"1", AV_DICT_DONT_OVERWRITE);
1218 scan_all_pmts_set = 1;
1221 err = avformat_open_input(&ic, filename, file_iformat, &o->
g->
format_opts);
1224 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1225 av_log(NULL, AV_LOG_ERROR,
"Did you mean file:%s?\n", filename);
1228 if (scan_all_pmts_set)
1229 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1234 for (i = 0; i < ic->nb_streams; i++)
1239 int orig_nb_streams = ic->nb_streams;
1243 ret = avformat_find_stream_info(ic, opts);
1245 for (i = 0; i < orig_nb_streams; i++)
1246 av_dict_free(&opts[i]);
1250 av_log(NULL, AV_LOG_FATAL,
"%s: could not find codec parameters\n", filename);
1251 if (ic->nb_streams == 0) {
1252 avformat_close_input(&ic);
1259 av_log(NULL, AV_LOG_WARNING,
"Cannot use -ss and -sseof both, using -ss for %s\n", filename);
1265 av_log(NULL, AV_LOG_ERROR,
"-sseof value must be negative; aborting\n");
1268 if (ic->duration > 0) {
1271 av_log(NULL, AV_LOG_WARNING,
"-sseof value seeks to before start of file %s; ignored\n", filename);
1275 av_log(NULL, AV_LOG_WARNING,
"Cannot use -sseof, duration of %s not known\n", filename);
1280 timestamp += ic->start_time;
1284 int64_t seek_timestamp = timestamp;
1286 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1287 int dts_heuristic = 0;
1288 for (i=0; i<ic->nb_streams; i++) {
1289 const AVCodecParameters *par = ic->streams[i]->codecpar;
1290 if (par->video_delay) {
1295 if (dts_heuristic) {
1296 seek_timestamp -= 3*AV_TIME_BASE / 23;
1299 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1301 av_log(NULL, AV_LOG_WARNING,
"%s: could not seek to position %0.3f\n",
1302 filename, (
double)timestamp / AV_TIME_BASE);
1313 f = av_mallocz(
sizeof(*f));
1333 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);
1337 av_log(NULL, AV_LOG_WARNING,
"Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n",
nb_input_files, f->
readrate);
1341 f->
pkt = av_packet_alloc();
1352 while ((e = av_dict_get(
input_streams[i]->decoder_opts,
"", e,
1353 AV_DICT_IGNORE_SUFFIX)))
1354 av_dict_set(&unused_opts, e->key, NULL, 0);
1358 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
1359 const AVClass *
class = avcodec_get_class();
1360 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
1361 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1362 const AVClass *fclass = avformat_get_class();
1363 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1364 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1365 if (!option || foption)
1369 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1370 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
1371 "input file #%d (%s) is not a decoding option.\n", e->key,
1377 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
1378 "input file #%d (%s) has not been used for any stream. The most "
1379 "likely reason is either wrong type (e.g. a video option with "
1380 "no video streams) or that it is a private option of some decoder "
1381 "which was not actually used for any stream.\n", e->key,
1384 av_dict_free(&unused_opts);
1389 for (j = 0; j < ic->nb_streams; j++) {
1390 AVStream *st = ic->streams[j];
1408 if (avio_open_dyn_buf(&line) < 0) {
1409 av_log(NULL, AV_LOG_FATAL,
"Could not alloc buffer for reading preset.\n");
1413 while ((c = avio_r8(s)) && c !=
'\n')
1416 avio_close_dyn_buf(line, &buf);
1424 char filename[1000];
1425 const char *base[3] = { getenv(
"AVCONV_DATADIR"),
1430 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
1434 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
1435 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
1436 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1439 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
1440 i != 1 ?
"" :
"/.avconv", preset_name);
1441 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1449 enum AVMediaType type =
ost->
st->codecpar->codec_type;
1450 char *codec_name = NULL;
1452 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
1455 ost->
st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
1456 NULL,
ost->
st->codecpar->codec_type);
1457 ost->
enc = avcodec_find_encoder(
ost->
st->codecpar->codec_id);
1459 av_log(NULL, AV_LOG_FATAL,
"Automatic encoder selection failed for "
1460 "output stream #%d:%d. Default encoder for format %s (codec %s) is "
1461 "probably disabled. Please choose an encoder manually.\n",
1463 avcodec_get_name(
ost->
st->codecpar->codec_id));
1464 return AVERROR_ENCODER_NOT_FOUND;
1466 }
else if (!strcmp(codec_name,
"copy"))
1485 AVStream *st = avformat_new_stream(oc, NULL);
1486 int idx = oc->nb_streams - 1, ret = 0;
1487 const char *bsfs = NULL, *time_base = NULL;
1488 char *next, *codec_tag = NULL;
1493 av_log(NULL, AV_LOG_FATAL,
"Could not alloc stream.\n");
1501 if (!(
ost = av_mallocz(
sizeof(*
ost))))
1509 st->codecpar->codec_type = type;
1513 av_log(NULL, AV_LOG_FATAL,
"Error selecting an encoder for stream "
1520 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding context.\n");
1527 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding parameters.\n");
1532 AVIOContext *s = NULL;
1533 char *buf = NULL, *arg = NULL, *preset = NULL;
1543 if (!buf[0] || buf[0] ==
'#') {
1547 if (!(arg = strchr(buf,
'='))) {
1548 av_log(NULL, AV_LOG_FATAL,
"Invalid line found in the preset file.\n");
1554 }
while (!s->eof_reached);
1558 av_log(NULL, AV_LOG_FATAL,
1559 "Preset %s specified for stream %d:%d, but could not be opened.\n",
1569 ost->
enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1574 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1575 q.num <= 0 || q.den <= 0) {
1576 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1585 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1587 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1597 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1598 av_log(NULL, AV_LOG_WARNING,
"Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1607 if (bsfs && *bsfs) {
1608 ret = av_bsf_list_parse_str(bsfs, &
ost->
bsf_ctx);
1610 av_log(NULL, AV_LOG_ERROR,
"Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1617 uint32_t tag = strtol(codec_tag, &next, 0);
1619 tag = AV_RL32(codec_tag);
1620 ost->
st->codecpar->codec_tag =
1627 ost->
enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1642 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1643 ost->
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1648 if (
ost->
enc && av_get_exact_bits_per_sample(
ost->
enc->id) == 24)
1649 av_dict_set(&
ost->
swr_opts,
"output_sample_bits",
"24", 0);
1654 if (source_index >= 0) {
1671 const char *p = str;
1678 av_log(NULL, AV_LOG_FATAL,
"Syntax error in matrix \"%s\" at coeff %d\n", str, i);
1688 AVIOContext *pb = NULL;
1689 AVIOContext *dyn_buf = NULL;
1690 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1691 uint8_t buf[1024], *str;
1694 av_log(NULL, AV_LOG_ERROR,
"Error opening file %s.\n", filename);
1698 ret = avio_open_dyn_buf(&dyn_buf);
1703 while ((ret = avio_read(pb, buf,
sizeof(buf))) > 0)
1704 avio_write(dyn_buf, buf, ret);
1705 avio_w8(dyn_buf, 0);
1708 ret = avio_close_dyn_buf(dyn_buf, &str);
1717 AVStream *st =
ost->
st;
1720 av_log(NULL, AV_LOG_ERROR,
"Both -filter and -filter_script set for "
1730 return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
1738 av_log(NULL, AV_LOG_ERROR,
1739 "%s '%s' was defined for %s output stream %d:%d but codec copy was selected.\n"
1740 "Filtering and streamcopy cannot be used together.\n",
1741 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
1752 AVCodecContext *video_enc;
1753 char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
1760 if (frame_rate && av_parse_video_rate(&
ost->
frame_rate, frame_rate) < 0) {
1761 av_log(NULL, AV_LOG_FATAL,
"Invalid framerate value: %s\n", frame_rate);
1766 if (max_frame_rate && av_parse_video_rate(&
ost->
max_frame_rate, max_frame_rate) < 0) {
1767 av_log(NULL, AV_LOG_FATAL,
"Invalid maximum framerate value: %s\n", max_frame_rate);
1771 if (frame_rate && max_frame_rate) {
1772 av_log(NULL, AV_LOG_ERROR,
"Only one of -fpsmax and -r can be set for a stream.\n");
1776 if ((frame_rate || max_frame_rate) &&
1778 av_log(NULL, AV_LOG_ERROR,
"Using -vsync 0 and -r/-fpsmax can produce invalid output files\n");
1781 if (frame_aspect_ratio) {
1783 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1784 q.num <= 0 || q.den <= 0) {
1785 av_log(NULL, AV_LOG_FATAL,
"Invalid aspect ratio: %s\n", frame_aspect_ratio);
1795 const char *p = NULL;
1796 char *frame_size = NULL;
1797 char *frame_pix_fmt = NULL;
1798 char *intra_matrix = NULL, *inter_matrix = NULL;
1799 char *chroma_intra_matrix = NULL;
1804 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
1805 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
1811 if (frame_pix_fmt && *frame_pix_fmt ==
'+') {
1813 if (!*++frame_pix_fmt)
1814 frame_pix_fmt = NULL;
1816 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
1817 av_log(NULL, AV_LOG_FATAL,
"Unknown pixel format requested: %s.\n", frame_pix_fmt);
1820 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
1823 video_enc->gop_size = 0;
1826 if (!(video_enc->intra_matrix = av_mallocz(
sizeof(*video_enc->intra_matrix) * 64))) {
1827 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1833 if (chroma_intra_matrix) {
1834 uint16_t *p = av_mallocz(
sizeof(*video_enc->chroma_intra_matrix) * 64);
1836 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1839 video_enc->chroma_intra_matrix = p;
1844 if (!(video_enc->inter_matrix = av_mallocz(
sizeof(*video_enc->inter_matrix) * 64))) {
1845 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for inter matrix.\n");
1852 for (i = 0; p; i++) {
1854 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
1856 av_log(NULL, AV_LOG_FATAL,
"error parsing rc_override\n");
1859 video_enc->rc_override =
1860 av_realloc_array(video_enc->rc_override,
1861 i + 1,
sizeof(RcOverride));
1862 if (!video_enc->rc_override) {
1863 av_log(NULL, AV_LOG_FATAL,
"Could not (re)allocate memory for rc_override.\n");
1866 video_enc->rc_override[i].start_frame = start;
1867 video_enc->rc_override[i].end_frame = end;
1869 video_enc->rc_override[i].qscale = q;
1870 video_enc->rc_override[i].quality_factor = 1.0;
1873 video_enc->rc_override[i].qscale = 0;
1874 video_enc->rc_override[i].quality_factor = -q/100.0;
1879 video_enc->rc_override_count = i;
1882 video_enc->flags|= AV_CODEC_FLAG_PSNR;
1888 video_enc->flags |= AV_CODEC_FLAG_PASS1;
1892 video_enc->flags |= AV_CODEC_FLAG_PASS2;
1903 char logfilename[1024];
1906 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
1910 if (!strcmp(
ost->
enc->name,
"libx264")) {
1911 av_dict_set(&
ost->
encoder_opts,
"stats", logfilename, AV_DICT_DONT_OVERWRITE);
1913 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
1917 av_log(NULL, AV_LOG_FATAL,
"Error reading log file '%s' for pass-2 encoding\n",
1921 video_enc->stats_in = logbuffer;
1923 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
1924 f = av_fopen_utf8(logfilename,
"wb");
1926 av_log(NULL, AV_LOG_FATAL,
1927 "Cannot write log file '%s' for pass-1 encoding: %s\n",
1928 logfilename, strerror(errno));
1964 AVCodecContext *audio_enc;
1970 audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
1976 char *sample_fmt = NULL;
1982 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
1983 av_log(NULL, AV_LOG_FATAL,
"Invalid sample format '%s'\n", sample_fmt);
2006 av_log(NULL, AV_LOG_FATAL,
"Cannot determine input stream for channel mapping %d.%d\n",
2038 av_log(NULL, AV_LOG_FATAL,
"Data stream encoding not supported yet (only streamcopy)\n");
2051 av_log(NULL, AV_LOG_FATAL,
"Unknown stream encoding not supported yet (only streamcopy)\n");
2070 AVCodecContext *subtitle_enc;
2076 subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2081 char *frame_size = NULL;
2084 if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
2085 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
2101 av_strlcpy(idx_str, arg,
sizeof(idx_str));
2102 p = strchr(idx_str,
':');
2104 av_log(NULL, AV_LOG_FATAL,
2105 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
2118 AVFormatContext *is = ifile->
ctx;
2119 AVFormatContext *os = ofile->
ctx;
2123 tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters,
sizeof(*os->chapters));
2125 return AVERROR(ENOMEM);
2128 for (i = 0; i < is->nb_chapters; i++) {
2129 AVChapter *in_ch = is->chapters[i], *out_ch;
2131 int64_t ts_off = av_rescale_q(start_time - ifile->
ts_offset,
2132 AV_TIME_BASE_Q, in_ch->time_base);
2134 av_rescale_q(ofile->
recording_time, AV_TIME_BASE_Q, in_ch->time_base);
2137 if (in_ch->end < ts_off)
2139 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2142 out_ch = av_mallocz(
sizeof(AVChapter));
2144 return AVERROR(ENOMEM);
2146 out_ch->id = in_ch->id;
2147 out_ch->time_base = in_ch->time_base;
2148 out_ch->start = FFMAX(0, in_ch->start - ts_off);
2149 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2152 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2154 os->chapters[os->nb_chapters++] = out_ch;
2160 AVFormatContext *oc)
2164 switch (ofilter->
type) {
2168 av_log(NULL, AV_LOG_FATAL,
"Only video and audio filters are supported "
2180 av_log(NULL, AV_LOG_ERROR,
"Streamcopy requested for output stream %d:%d, "
2181 "which is fed from a complex filtergraph. Filtering and streamcopy "
2187 const char *opt =
ost->
filters ?
"-vf/-af/-filter" :
"-filter_script";
2188 av_log(NULL, AV_LOG_ERROR,
2189 "%s '%s' was specified through the %s option "
2190 "for output stream %d:%d, which is fed from a complex filtergraph.\n"
2191 "%s and -filter_complex cannot be used together for the same stream.\n",
2192 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
2198 avfilter_inout_free(&ofilter->
out_tmp);
2215 AVFormatContext *oc;
2220 AVDictionary *unused_opts = NULL;
2221 AVDictionaryEntry *e = NULL;
2222 int format_flags = 0;
2226 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
2232 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
2240 of = av_mallocz(
sizeof(*of));
2252 if (!strcmp(filename,
"-"))
2255 err = avformat_alloc_output_context2(&oc, NULL, o->
format, filename);
2265 oc->interrupt_callback =
int_cb;
2269 const AVOption *o = av_opt_find(oc,
"fflags", NULL, 0, 0);
2270 av_opt_eval_flags(oc, o, e->value, &format_flags);
2273 format_flags |= AVFMT_FLAG_BITEXACT;
2274 oc->flags |= AVFMT_FLAG_BITEXACT;
2286 switch (ofilter->
type) {
2296 char *subtitle_codec_name = NULL;
2300 if (!o->
video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) {
2301 int best_score = 0, idx = -1;
2302 int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
2305 int file_best_score = 0, file_best_idx = -1;
2309 score =
ist->st->codecpar->width *
ist->st->codecpar->height
2310 + 100000000 * !!(
ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2311 + 5000000*!!(
ist->st->disposition & AV_DISPOSITION_DEFAULT);
2312 if (
ist->user_set_discard == AVDISCARD_ALL)
2314 if((qcr!=MKTAG(
'A',
'P',
'I',
'C')) && (
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2316 if (
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2317 score > file_best_score) {
2318 if((qcr==MKTAG(
'A',
'P',
'I',
'C')) && !(
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2320 file_best_score = score;
2324 if (file_best_idx >= 0) {
2325 if((qcr == MKTAG(
'A',
'P',
'I',
'C')) || !(
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2326 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
2327 if (file_best_score > best_score) {
2328 best_score = file_best_score;
2329 idx = file_best_idx;
2338 if (!o->
audio_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_AUDIO) != AV_CODEC_ID_NONE) {
2339 int best_score = 0, idx = -1;
2342 int file_best_score = 0, file_best_idx = -1;
2346 score =
ist->st->codecpar->channels
2347 + 100000000 * !!(
ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2348 + 5000000*!!(
ist->st->disposition & AV_DISPOSITION_DEFAULT);
2349 if (
ist->user_set_discard == AVDISCARD_ALL)
2351 if (
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2352 score > file_best_score) {
2353 file_best_score = score;
2357 if (file_best_idx >= 0) {
2358 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->
st->disposition & AV_DISPOSITION_DEFAULT);
2359 if (file_best_score > best_score) {
2360 best_score = file_best_score;
2361 idx = file_best_idx;
2371 if (!o->
subtitle_disable && (avcodec_find_encoder(oc->oformat->subtitle_codec) || subtitle_codec_name)) {
2373 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2374 AVCodecDescriptor
const *input_descriptor =
2375 avcodec_descriptor_get(
input_streams[i]->st->codecpar->codec_id);
2376 AVCodecDescriptor
const *output_descriptor = NULL;
2377 AVCodec
const *output_codec =
2378 avcodec_find_encoder(oc->oformat->subtitle_codec);
2379 int input_props = 0, output_props = 0;
2383 output_descriptor = avcodec_descriptor_get(output_codec->id);
2384 if (input_descriptor)
2385 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2386 if (output_descriptor)
2387 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2388 if (subtitle_codec_name ||
2389 input_props & output_props ||
2391 (input_descriptor && output_descriptor &&
2392 (!input_descriptor->props ||
2393 !output_descriptor->props))) {
2401 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_DATA);
2405 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_DATA
2426 if (out && !strcmp(out->name, map->
linklabel)) {
2434 av_log(NULL, AV_LOG_FATAL,
"Output with label '%s' does not exist "
2435 "in any defined filter graph, or was already used elsewhere.\n", map->
linklabel);
2443 if (
ist->user_set_discard == AVDISCARD_ALL) {
2444 av_log(NULL, AV_LOG_FATAL,
"Stream #%d:%d is disabled and cannot be mapped.\n",
2450 if(o-> audio_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2452 if(o-> video_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2454 if(o-> data_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2458 switch (
ist->st->codecpar->codec_type) {
2464 case AVMEDIA_TYPE_UNKNOWN:
2471 "Cannot map stream #%d:%d - unsupported type.\n",
2474 av_log(NULL, AV_LOG_FATAL,
2475 "If you want unsupported types ignored instead "
2476 "of failing, please use the -ignore_unknown option\n"
2477 "If you want them copied, please use -copy_unknown\n");
2491 uint8_t *attachment;
2495 if ((err = avio_open2(&pb, o->
attachments[i], AVIO_FLAG_READ, &
int_cb, NULL)) < 0) {
2496 av_log(NULL, AV_LOG_FATAL,
"Could not open attachment file %s.\n",
2500 if ((len = avio_size(pb)) <= 0) {
2501 av_log(NULL, AV_LOG_FATAL,
"Could not get size of the attachment %s.\n",
2505 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
2506 !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
2507 av_log(NULL, AV_LOG_FATAL,
"Attachment %s too large.\n",
2511 avio_read(pb, attachment, len);
2512 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2517 ost->
st->codecpar->extradata = attachment;
2518 ost->
st->codecpar->extradata_size = len;
2521 av_dict_set(&
ost->
st->metadata,
"filename", (p && *p) ? p + 1 : o->
attachments[i], AV_DICT_DONT_OVERWRITE);
2525 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2527 av_log(NULL, AV_LOG_ERROR,
"Output file #%d does not contain any stream\n",
nb_output_files - 1);
2536 AV_DICT_IGNORE_SUFFIX)))
2537 av_dict_set(&unused_opts, e->key, NULL, 0);
2541 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
2542 const AVClass *
class = avcodec_get_class();
2543 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
2544 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2545 const AVClass *fclass = avformat_get_class();
2546 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
2547 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2548 if (!option || foption)
2552 if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
2553 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
2554 "output file #%d (%s) is not an encoding option.\n", e->key,
2561 if (!strcmp(e->key,
"gop_timecode"))
2564 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
2565 "output file #%d (%s) has not been used for any stream. The most "
2566 "likely reason is either wrong type (e.g. a video option with "
2567 "no video streams) or that it is a private option of some encoder "
2568 "which was not actually used for any stream.\n", e->key,
2571 av_dict_free(&unused_opts);
2581 if (
ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2582 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2585 av_log(NULL, AV_LOG_ERROR,
2586 "Error initializing a simple filtergraph between streams "
2599 case AVMEDIA_TYPE_VIDEO:
2603 if (
ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
2605 }
else if (
ost->
enc->pix_fmts) {
2607 while (
ost->
enc->pix_fmts[count] != AV_PIX_FMT_NONE)
2615 case AVMEDIA_TYPE_AUDIO:
2616 if (
ost->
enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
2618 }
else if (
ost->
enc->sample_fmts) {
2620 while (
ost->
enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
2629 }
else if (
ost->
enc->supported_samplerates) {
2631 while (
ost->
enc->supported_samplerates[count])
2641 }
else if (
ost->
enc->channel_layouts) {
2643 while (
ost->
enc->channel_layouts[count])
2657 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2658 if (!av_filename_number_test(oc->url)) {
2665 av_log(NULL, AV_LOG_ERROR,
2666 "No input streams but output needs an input stream\n");
2670 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2675 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
2676 &oc->interrupt_callback,
2681 }
else if (strcmp(oc->oformat->name,
"image2")==0 && !av_filename_number_test(filename))
2685 av_dict_set_int(&of->
opts,
"preload", o->
mux_preload*AV_TIME_BASE, 0);
2695 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d while processing metadata maps\n", in_file_index);
2699 in_file_index >= 0 ?
2714 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d in chapter mapping.\n",
2726 AV_DICT_DONT_OVERWRITE);
2728 av_dict_set(&oc->metadata,
"duration", NULL, 0);
2729 av_dict_set(&oc->metadata,
"creation_time", NULL, 0);
2730 av_dict_set(&oc->metadata,
"company_name", NULL, 0);
2731 av_dict_set(&oc->metadata,
"product_name", NULL, 0);
2732 av_dict_set(&oc->metadata,
"product_version", NULL, 0);
2740 av_dict_copy(&
output_streams[i]->st->metadata,
ist->st->metadata, AV_DICT_DONT_OVERWRITE);
2742 av_dict_set(&
output_streams[i]->st->metadata,
"encoder", NULL, 0);
2753 const char *p2 = av_get_token(&p,
":");
2754 const char *to_dealloc = p2;
2761 key = av_get_token(&p2,
"=");
2763 av_freep(&to_dealloc);
2769 if (!strcmp(key,
"program_num"))
2770 progid = strtol(p2, NULL, 0);
2771 av_freep(&to_dealloc);
2775 program = av_new_program(oc, progid);
2779 const char *p2 = av_get_token(&p,
":");
2780 const char *to_dealloc = p2;
2786 key = av_get_token(&p2,
"=");
2788 av_log(NULL, AV_LOG_FATAL,
2789 "No '=' character in program string %s.\n",
2797 if (!strcmp(key,
"title")) {
2798 av_dict_set(&program->metadata,
"title", p2, 0);
2799 }
else if (!strcmp(key,
"program_num")) {
2800 }
else if (!strcmp(key,
"st")) {
2801 int st_num = strtol(p2, NULL, 0);
2802 av_program_add_stream_index(oc, progid, st_num);
2804 av_log(NULL, AV_LOG_FATAL,
"Unknown program key %s.\n", key);
2807 av_freep(&to_dealloc);
2816 const char *stream_spec;
2817 int index = 0, j, ret = 0;
2821 av_log(NULL, AV_LOG_FATAL,
"No '=' character in metadata string %s.\n",
2829 for (j = 0; j < oc->nb_streams; j++) {
2834 double theta = av_strtod(val, &tail);
2840 av_dict_set(&oc->streams[j]->metadata, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
2852 if (index < 0 || index >= oc->nb_chapters) {
2853 av_log(NULL, AV_LOG_FATAL,
"Invalid chapter index %d in metadata specifier.\n",
index);
2856 m = &oc->chapters[
index]->metadata;
2859 if (index < 0 || index >= oc->nb_programs) {
2860 av_log(NULL, AV_LOG_FATAL,
"Invalid program index %d in metadata specifier.\n",
index);
2863 m = &oc->programs[
index]->metadata;
2866 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", o->
metadata[i].
specifier);
2869 av_dict_set(m, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
2880 enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
2881 const char *
const frame_rates[] = {
"25",
"30000/1001",
"24000/1001" };
2883 if (!strncmp(arg,
"pal-", 4)) {
2886 }
else if (!strncmp(arg,
"ntsc-", 5)) {
2889 }
else if (!strncmp(arg,
"film-", 5)) {
2900 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2902 fr = st->time_base.den * 1000LL / st->time_base.num;
2906 }
else if ((fr == 29970) || (fr == 23976)) {
2911 if (norm != UNKNOWN)
2915 if (norm != UNKNOWN)
2916 av_log(NULL, AV_LOG_INFO,
"Assuming %s for target.\n", norm == PAL ?
"PAL" :
"NTSC");
2919 if (norm == UNKNOWN) {
2920 av_log(NULL, AV_LOG_FATAL,
"Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
2921 av_log(NULL, AV_LOG_FATAL,
"Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
2922 av_log(NULL, AV_LOG_FATAL,
"or set a framerate with \"-r xxx\".\n");
2926 if (!strcmp(arg,
"vcd")) {
2931 parse_option(o,
"s", norm == PAL ?
"352x288" :
"352x240", options);
2933 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2953 }
else if (!strcmp(arg,
"svcd")) {
2959 parse_option(o,
"s", norm == PAL ?
"480x576" :
"480x480", options);
2962 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2975 }
else if (!strcmp(arg,
"dvd")) {
2981 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
2984 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2997 }
else if (!strncmp(arg,
"dv", 2)) {
3001 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
3002 parse_option(o,
"pix_fmt", !strncmp(arg,
"dv50", 4) ?
"yuv422p" :
3003 norm == PAL ?
"yuv420p" :
"yuv411p", options);
3010 av_log(NULL, AV_LOG_ERROR,
"Unknown target: %s\n", arg);
3011 return AVERROR(EINVAL);
3030 time_t today2 = time(NULL);
3031 struct tm *today = localtime(&today2);
3034 av_log(NULL, AV_LOG_FATAL,
"Unable to get current time: %s\n", strerror(errno));
3038 snprintf(filename,
sizeof(filename),
"vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
3085 char filename[1000], line[1000], tmp_line[1000];
3086 const char *codec_name = NULL;
3092 if (!(f =
get_preset_file(filename,
sizeof(filename), arg, *opt ==
'f', codec_name))) {
3093 if(!strncmp(arg,
"libx264-lossless", strlen(
"libx264-lossless"))){
3094 av_log(NULL, AV_LOG_FATAL,
"Please use -preset <speed> -qp 0\n");
3096 av_log(NULL, AV_LOG_FATAL,
"File for preset '%s' not found\n", arg);
3100 while (fgets(line,
sizeof(line), f)) {
3101 char *key = tmp_line, *value, *endptr;
3103 if (strcspn(line,
"#\n\r") == 0)
3105 av_strlcpy(tmp_line, line,
sizeof(tmp_line));
3106 if (!av_strtok(key,
"=", &value) ||
3107 !av_strtok(value,
"\r\n", &endptr)) {
3108 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid syntax: '%s'\n", filename, line);
3111 av_log(NULL, AV_LOG_DEBUG,
"ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
3118 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
3119 filename, line, key, value);
3133 char *s = av_asprintf(
"%s:%c", opt + 1, *opt);
3135 return AVERROR(ENOMEM);
3145 if(!strcmp(opt,
"ab")){
3148 }
else if(!strcmp(opt,
"b")){
3149 av_log(NULL, AV_LOG_WARNING,
"Please use -b:a or -b:v, -b is ambiguous\n");
3163 if(!strcmp(opt,
"qscale")){
3164 av_log(NULL, AV_LOG_WARNING,
"Please use -q:a or -q:v, -qscale is ambiguous\n");
3167 s = av_asprintf(
"q%s", opt + 6);
3169 return AVERROR(ENOMEM);
3178 if(!strcmp(opt,
"profile")){
3179 av_log(NULL, AV_LOG_WARNING,
"Please use -profile:a or -profile:v, -profile is ambiguous\n");
3180 av_dict_set(&o->
g->
codec_opts,
"profile:v", arg, 0);
3199int opt_vsync(
void *optctx,
const char *opt,
const char *arg)
3215 char *tcr = av_asprintf(
"timecode=%s", arg);
3217 return AVERROR(ENOMEM);
3220 ret = av_dict_set(&o->
g->
codec_opts,
"gop_timecode", arg, 0);
3228 char layout_str[32];
3231 int ret, channels, ac_str_size;
3234 layout = av_get_channel_layout(arg);
3236 av_log(NULL, AV_LOG_ERROR,
"Unknown channel layout: %s\n", arg);
3237 return AVERROR(EINVAL);
3239 snprintf(layout_str,
sizeof(layout_str),
"%"PRIu64, layout);
3245 channels = av_get_channel_layout_nb_channels(layout);
3246 snprintf(layout_str,
sizeof(layout_str),
"%d", channels);
3247 stream_str = strchr(opt,
':');
3248 ac_str_size = 3 + (stream_str ? strlen(stream_str) : 0);
3249 ac_str = av_mallocz(ac_str_size);
3251 return AVERROR(ENOMEM);
3252 av_strlcpy(ac_str,
"ac", 3);
3254 av_strlcat(ac_str, stream_str, ac_str_size);
3271 return AVERROR(ENOMEM);
3275 return AVERROR(ENOMEM);
3286 return AVERROR(EINVAL);
3290 return AVERROR(ENOMEM);
3304 int show_advanced = 0, show_avoptions = 0;
3307 if (!strcmp(opt,
"long"))
3309 else if (!strcmp(opt,
"full"))
3310 show_advanced = show_avoptions = 1;
3312 av_log(NULL, AV_LOG_ERROR,
"Unknown help option '%s'.\n", opt);
3318 " -h -- print basic options\n"
3319 " -h long -- print more options\n"
3320 " -h full -- print all options (including all format and codec specific options, very long)\n"
3321 " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol\n"
3322 " See man %s for detailed description of the options.\n"
3329 "instead of just one file):",
3357 if (show_avoptions) {
3358 int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
3364#if CONFIG_SWRESAMPLE
3367 show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM);
3368 show_help_children(av_bsf_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM);
3374 av_log(NULL, AV_LOG_INFO,
"Hyper fast Audio and Video encoder\n");
3375 av_log(NULL, AV_LOG_INFO,
"usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n",
program_name);
3376 av_log(NULL, AV_LOG_INFO,
"\n");
3385 [GROUP_OUTFILE] = {
"output url", NULL,
OPT_OUTPUT },
3386 [GROUP_INFILE] = {
"input url",
"i",
OPT_INPUT },
3403 av_log(NULL, AV_LOG_ERROR,
"Error parsing options for %s file "
3404 "%s.\n", inout, g->
arg);
3409 av_log(NULL, AV_LOG_DEBUG,
"Opening an %s file: %s.\n", inout, g->
arg);
3410 ret = open_file(&o, g->
arg);
3413 av_log(NULL, AV_LOG_ERROR,
"Error opening %s file %s.\n",
3417 av_log(NULL, AV_LOG_DEBUG,
"Successfully opened the file.\n");
3429 memset(&octx, 0,
sizeof(octx));
3433 FF_ARRAY_ELEMS(groups));
3435 av_log(NULL, AV_LOG_FATAL,
"Error splitting the argument list: ");
3442 av_log(NULL, AV_LOG_FATAL,
"Error parsing global options: ");
3452 av_log(NULL, AV_LOG_FATAL,
"Error opening input files: ");
3459 av_log(NULL, AV_LOG_FATAL,
"Error initializing complex filters.\n");
3466 av_log(NULL, AV_LOG_FATAL,
"Error opening output files: ");
3475 av_strerror(ret, error,
sizeof(error));
3476 av_log(NULL, AV_LOG_FATAL,
"%s\n", error);
3481int opt_progress(
void *optctx,
const char *opt,
const char *arg)
3483 AVIOContext *avio = NULL;
3486 if (!strcmp(arg,
"-"))
3488 ret = avio_open2(&avio, arg, AVIO_FLAG_WRITE, &
int_cb, NULL);
3490 av_log(NULL, AV_LOG_ERROR,
"Failed to open progress URL \"%s\": %s\n",
3491 arg, av_err2str(ret));
AVDictionary * codec_opts
AVDictionary * format_opts
AVDictionary * resample_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
const char ** attachments
AudioChannelMap * audio_channel_maps
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
uint64_t * channel_layouts
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
AVDictionary * resample_opts
AVRational max_frame_rate
AVFifoBuffer * muxing_queue
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