42#include "libavformat/avformat.h"
44#include "libavcodec/avcodec.h"
45#include "libavcodec/bsf.h"
47#include "libavfilter/avfilter.h"
49#include "libavutil/avassert.h"
50#include "libavutil/avstring.h"
51#include "libavutil/avutil.h"
52#include "libavutil/channel_layout.h"
53#include "libavutil/intreadwrite.h"
54#include "libavutil/fifo.h"
55#include "libavutil/mathematics.h"
56#include "libavutil/opt.h"
57#include "libavutil/parseutils.h"
58#include "libavutil/pixdesc.h"
59#include "libavutil/pixfmt.h"
61#define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
63#define SPECIFIER_OPT_FMT_str "%s"
64#define SPECIFIER_OPT_FMT_i "%i"
65#define SPECIFIER_OPT_FMT_i64 "%"PRId64
66#define SPECIFIER_OPT_FMT_ui64 "%"PRIu64
67#define SPECIFIER_OPT_FMT_f "%f"
68#define SPECIFIER_OPT_FMT_dbl "%lf"
70static const char *
const opt_name_codec_names[] = {
"c",
"codec",
"acodec",
"vcodec",
"scodec",
"dcodec", NULL};
115#define WARN_MULTIPLE_OPT_USAGE(name, type, so, st)\
117 char namestr[128] = "";\
118 const char *spec = so->specifier && so->specifier[0] ? so->specifier : "";\
119 for (i = 0; opt_name_##name[i]; i++)\
120 av_strlcatf(namestr, sizeof(namestr), "-%s%s", opt_name_##name[i], opt_name_##name[i+1] ? (opt_name_##name[i+2] ? ", " : " or ") : "");\
121 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",\
122 namestr, st->index, opt_name_##name[0], spec[0] ? ":" : "", spec, so->u.type);\
125#define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
127 int i, ret, matches = 0;\
129 for (i = 0; i < o->nb_ ## name; i++) {\
130 char *spec = o->name[i].specifier;\
131 if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0) {\
132 outvar = o->name[i].u.type;\
139 WARN_MULTIPLE_OPT_USAGE(name, type, so, st);\
142#define MATCH_PER_TYPE_OPT(name, type, outvar, fmtctx, mediatype)\
145 for (i = 0; i < o->nb_ ## name; i++) {\
146 char *spec = o->name[i].specifier;\
147 if (!strcmp(spec, mediatype))\
148 outvar = o->name[i].u.type;\
153#if CONFIG_VIDEOTOOLBOX
214 void *dst = (uint8_t*)o + po->
u.
off;
218 int i, *count = (
int*)(so + 1);
219 for (i = 0; i < *count; i++) {
220 av_freep(&(*so)[i].specifier);
222 av_freep(&(*so)[i].u.str);
241 memset(o, 0,
sizeof(*o));
256 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
258 av_log(NULL,
AV_LOG_STDERR,
"Hardware acceleration methods:\n");
259 while ((type = av_hwdevice_iterate_types(type)) !=
260 AV_HWDEVICE_TYPE_NONE)
261 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
269 AVDictionaryEntry *e = NULL;
270 AVDictionary *ret = NULL;
272 while ((e = av_dict_get(dict,
"", e, AV_DICT_IGNORE_SUFFIX))) {
273 char *p = strchr(e->key,
':');
277 av_dict_set(&ret, e->key, e->value, 0);
286 const AVOption opts[] = {
287 {
"abort_on" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, (double)INT64_MAX, .unit =
"flags" },
292 const AVClass
class = {
294 .item_name = av_default_item_name,
296 .version = LIBAVUTIL_VERSION_INT,
298 const AVClass *pclass = &
class;
300 return av_opt_eval_flags(&pclass, &opts[0], arg, &
abort_on_flags);
307 if (user_stats_period <= 0) {
308 av_log(NULL, AV_LOG_ERROR,
"stats_period %s must be positive.\n", arg);
309 return AVERROR(EINVAL);
313 av_log(NULL, AV_LOG_INFO,
"ffmpeg stats and -progress period set to %s.\n", arg);
318int opt_sameq(
void *optctx,
const char *opt,
const char *arg)
320 av_log(NULL, AV_LOG_ERROR,
"Option '%s' was removed. "
321 "If you are looking for an option to preserve the quality (which is not "
322 "what -%s was for), use -qscale 0 or an equivalent quality factor option.\n",
324 return AVERROR(EINVAL);
329 av_log(NULL, AV_LOG_WARNING,
"This option is deprecated, use -channel.\n");
335 av_log(NULL, AV_LOG_WARNING,
"This option is deprecated, use -standard.\n");
363int opt_map(
void *optctx,
const char *opt,
const char *arg)
367 int i, negative = 0, file_idx, disabled = 0;
368 int sync_file_idx = -1, sync_stream_idx = 0;
377 map = av_strdup(arg);
379 return AVERROR(ENOMEM);
382 if ((sync = strchr(map,
','))) {
384 sync_file_idx = strtol(sync + 1, &sync, 0);
386 av_log(NULL, AV_LOG_FATAL,
"Invalid sync file index: %d.\n", sync_file_idx);
398 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s does not "
399 "match any streams.\n", arg);
403 av_log(NULL, AV_LOG_FATAL,
"Sync stream specification in map %s matches a disabled input "
412 const char *c = map + 1;
417 av_log(NULL, AV_LOG_ERROR,
"Invalid output link label: %s.\n", map);
421 if ((allow_unused = strchr(map,
'?')))
423 file_idx = strtol(map, &p, 0);
425 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index: %d.\n", file_idx);
435 *p ==
':' ? p + 1 : p) > 0)
441 *p ==
':' ? p + 1 : p) <= 0)
453 if (sync_file_idx >= 0) {
465 av_log(NULL, AV_LOG_VERBOSE,
"Stream map '%s' matches no streams; ignoring.\n", arg);
466 }
else if (disabled) {
467 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches disabled streams.\n"
468 "To ignore this, add a trailing '?' to the map.\n", arg);
471 av_log(NULL, AV_LOG_FATAL,
"Stream map '%s' matches no streams.\n"
472 "To ignore this, add a trailing '?' to the map.\n", arg);
481int opt_attach(
void *optctx,
const char *opt,
const char *arg)
497 mapchan = av_strdup(arg);
499 return AVERROR(ENOMEM);
515 n = sscanf(arg,
"%d.%d.%d:%d.%d",
519 if (n != 3 && n != 5) {
520 av_log(NULL, AV_LOG_FATAL,
"Syntax error, mapchan usage: "
521 "[file.stream.channel|-1][:syncfile:syncstream]\n");
530 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file index: %d\n",
536 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid input file stream index #%d.%d\n",
541 if (st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
542 av_log(NULL, AV_LOG_FATAL,
"mapchan: stream #%d.%d is not an audio stream.\n",
547 if ((allow_unused = strchr(mapchan,
'?')))
552 av_log(NULL, AV_LOG_VERBOSE,
"mapchan: invalid audio channel #%d.%d.%d\n",
555 av_log(NULL, AV_LOG_FATAL,
"mapchan: invalid audio channel #%d.%d.%d\n"
556 "To ignore this, add a trailing '?' to the map_channel.\n",
574int opt_vaapi_device(
void *optctx,
const char *opt,
const char *arg)
576 const char *prefix =
"vaapi:";
579 tmp = av_asprintf(
"%s%s", prefix, arg);
581 return AVERROR(ENOMEM);
589static int opt_qsv_device(
void *optctx,
const char *opt,
const char *arg)
591 const char *prefix =
"qsv=__qsv_device:hw_any,child_device=";
593 char *tmp = av_asprintf(
"%s%s", prefix, arg);
596 return AVERROR(ENOMEM);
607 if (!strcmp(arg,
"list")) {
608 enum AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
609 av_log(NULL,
AV_LOG_STDERR,
"Supported hardware device types:\n");
610 while ((type = av_hwdevice_iterate_types(type)) !=
611 AV_HWDEVICE_TYPE_NONE)
612 av_log(NULL,
AV_LOG_STDERR,
"%s\n", av_hwdevice_get_type_name(type));
623 av_log(NULL, AV_LOG_ERROR,
"Only one filter device can be used.\n");
624 return AVERROR(EINVAL);
628 av_log(NULL, AV_LOG_ERROR,
"Invalid filter device %s.\n", arg);
629 return AVERROR(EINVAL);
649 if (*(++arg) && *arg !=
':') {
650 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", arg);
653 *stream_spec = *arg ==
':' ? arg + 1 :
"";
658 *
index = strtol(++arg, NULL, 0);
661 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata type %c.\n", *arg);
670 AVDictionary **meta_in = NULL;
671 AVDictionary **meta_out = NULL;
673 char type_in, type_out;
674 const char *istream_spec = NULL, *ostream_spec = NULL;
675 int idx_in = 0, idx_out = 0;
681 if (type_out ==
'g' || !*outspec)
683 if (type_out ==
's' || !*outspec)
685 if (type_out ==
'c' || !*outspec)
690 if (type_in ==
'g' || type_out ==
'g')
692 if (type_in ==
's' || type_out ==
's')
694 if (type_in ==
'c' || type_out ==
'c')
701#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
702 if ((index) < 0 || (index) >= (nb_elems)) {\
703 av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
708#define SET_DICT(type, meta, context, index)\
711 meta = &context->metadata;\
714 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
715 meta = &context->chapters[index]->metadata;\
718 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
719 meta = &context->programs[index]->metadata;\
723 default: av_assert0(0);\
726 SET_DICT(type_in, meta_in, ic, idx_in);
727 SET_DICT(type_out, meta_out, oc, idx_out);
730 if (type_in ==
's') {
731 for (i = 0; i < ic->nb_streams; i++) {
733 meta_in = &ic->streams[i]->metadata;
739 av_log(NULL, AV_LOG_FATAL,
"Stream specifier %s does not match any streams.\n", istream_spec);
744 if (type_out ==
's') {
745 for (i = 0; i < oc->nb_streams; i++) {
747 meta_out = &oc->streams[i]->metadata;
748 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
753 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
763 struct tm time = *gmtime((time_t*)&recording_timestamp);
764 if (!strftime(buf,
sizeof(buf),
"creation_time=%Y-%m-%dT%H:%M:%S%z", &time))
768 av_log(NULL, AV_LOG_WARNING,
"%s is deprecated, set the 'creation_time' metadata "
769 "tag instead.\n", opt);
775 const AVCodecDescriptor *desc;
776 const char *codec_string = encoder ?
"encoder" :
"decoder";
777 const AVCodec *codec;
780 avcodec_find_encoder_by_name(name) :
781 avcodec_find_decoder_by_name(name);
783 if (!codec && (desc = avcodec_descriptor_get_by_name(name))) {
784 codec = encoder ? avcodec_find_encoder(desc->id) :
785 avcodec_find_decoder(desc->id);
787 av_log(NULL, AV_LOG_VERBOSE,
"Matched %s '%s' for codec '%s'.\n",
788 codec_string, codec->name, desc->name);
792 av_log(NULL, AV_LOG_FATAL,
"Unknown %s '%s'\n", codec_string, name);
796 av_log(NULL, AV_LOG_FATAL,
"Invalid %s type '%s'\n", codec_string, name);
804 char *codec_name = NULL;
808 const AVCodec *codec =
find_codec_or_die(codec_name, st->codecpar->codec_type, 0);
809 st->codecpar->codec_id = codec->id;
810 if (
recast_media && st->codecpar->codec_type != codec->type)
811 st->codecpar->codec_type = codec->type;
814 return avcodec_find_decoder(st->codecpar->codec_id);
823 for (i = 0; i < ic->nb_streams; i++) {
824 AVStream *st = ic->streams[i];
825 AVCodecParameters *par = st->codecpar;
827 char *framerate = NULL, *hwaccel_device = NULL;
828 const char *hwaccel = NULL;
829 char *hwaccel_output_format = NULL;
830 char *codec_tag = NULL;
832 char *discard_str = NULL;
833 const AVClass *cc = avcodec_get_class();
834 const AVOption *discard_opt = av_opt_find(&cc,
"skip_frame", NULL, 0, 0);
845 st->discard = AVDISCARD_ALL;
847 ist->first_dts = AV_NOPTS_VALUE;
848 ist->min_pts = INT64_MAX;
849 ist->max_pts = INT64_MIN;
859 uint32_t tag = strtol(codec_tag, &next, 0);
861 tag = AV_RL32(codec_tag);
862 st->codecpar->codec_tag = tag;
868 ist->reinit_filters = -1;
872 ist->user_set_discard = AVDISCARD_NONE;
874 if ((o->
video_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) ||
875 (o->
audio_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) ||
877 (o->
data_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA))
878 ist->user_set_discard = AVDISCARD_ALL;
880 if (discard_str && av_opt_eval_int(&cc, discard_opt, discard_str, &
ist->user_set_discard) < 0) {
881 av_log(NULL, AV_LOG_ERROR,
"Error parsing discard %s.\n",
886 ist->filter_in_rescale_delta_last = AV_NOPTS_VALUE;
888 ist->dec_ctx = avcodec_alloc_context3(
ist->dec);
890 av_log(NULL, AV_LOG_ERROR,
"Error allocating the decoder context.\n");
894 ret = avcodec_parameters_to_context(
ist->dec_ctx, par);
896 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
901 ist->dec_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
903 switch (par->codec_type) {
904 case AVMEDIA_TYPE_VIDEO:
906 ist->dec = avcodec_find_decoder(par->codec_id);
909 ist->dec_ctx->framerate = st->avg_frame_rate;
912 if (framerate && av_parse_video_rate(&
ist->framerate,
914 av_log(NULL, AV_LOG_ERROR,
"Error parsing framerate %s.\n",
919 ist->top_field_first = -1;
924 hwaccel_output_format, ic, st);
926 if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"cuvid")) {
927 av_log(NULL, AV_LOG_WARNING,
928 "WARNING: defaulting hwaccel_output_format to cuda for compatibility "
929 "with old commandlines. This behaviour is DEPRECATED and will be removed "
930 "in the future. Please explicitly set \"-hwaccel_output_format cuda\".\n");
931 ist->hwaccel_output_format = AV_PIX_FMT_CUDA;
932 }
else if (!hwaccel_output_format && hwaccel && !strcmp(hwaccel,
"qsv")) {
933 av_log(NULL, AV_LOG_WARNING,
934 "WARNING: defaulting hwaccel_output_format to qsv for compatibility "
935 "with old commandlines. This behaviour is DEPRECATED and will be removed "
936 "in the future. Please explicitly set \"-hwaccel_output_format qsv\".\n");
937 ist->hwaccel_output_format = AV_PIX_FMT_QSV;
938 }
else if (hwaccel_output_format) {
939 ist->hwaccel_output_format = av_get_pix_fmt(hwaccel_output_format);
940 if (
ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
941 av_log(NULL, AV_LOG_FATAL,
"Unrecognised hwaccel output "
942 "format: %s", hwaccel_output_format);
945 ist->hwaccel_output_format = AV_PIX_FMT_NONE;
950 if (!strcmp(hwaccel,
"nvdec") || !strcmp(hwaccel,
"cuvid"))
953 if (!strcmp(hwaccel,
"none"))
955 else if (!strcmp(hwaccel,
"auto"))
958 enum AVHWDeviceType type;
961 if (!strcmp(
hwaccels[i].name, hwaccel)) {
967 if (!
ist->hwaccel_id) {
968 type = av_hwdevice_find_type_by_name(hwaccel);
969 if (type != AV_HWDEVICE_TYPE_NONE) {
971 ist->hwaccel_device_type = type;
975 if (!
ist->hwaccel_id) {
976 av_log(NULL, AV_LOG_FATAL,
"Unrecognized hwaccel: %s.\n",
978 av_log(NULL, AV_LOG_FATAL,
"Supported hwaccels: ");
979 type = AV_HWDEVICE_TYPE_NONE;
980 while ((type = av_hwdevice_iterate_types(type)) !=
981 AV_HWDEVICE_TYPE_NONE)
982 av_log(NULL, AV_LOG_FATAL,
"%s ",
983 av_hwdevice_get_type_name(type));
984 av_log(NULL, AV_LOG_FATAL,
"\n");
991 if (hwaccel_device) {
992 ist->hwaccel_device = av_strdup(hwaccel_device);
993 if (!
ist->hwaccel_device)
997 ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;
1000 case AVMEDIA_TYPE_AUDIO:
1001 ist->guess_layout_max = INT_MAX;
1005 case AVMEDIA_TYPE_DATA:
1006 case AVMEDIA_TYPE_SUBTITLE: {
1007 char *canvas_size = NULL;
1009 ist->dec = avcodec_find_decoder(par->codec_id);
1013 av_parse_video_size(&
ist->dec_ctx->width, &
ist->dec_ctx->height, canvas_size) < 0) {
1014 av_log(NULL, AV_LOG_FATAL,
"Invalid canvas size: %s.\n", canvas_size);
1019 case AVMEDIA_TYPE_ATTACHMENT:
1020 case AVMEDIA_TYPE_UNKNOWN:
1026 ret = avcodec_parameters_from_context(par,
ist->dec_ctx);
1028 av_log(NULL, AV_LOG_ERROR,
"Error initializing the decoder context.\n");
1036 const char *proto_name = avio_find_protocol_name(filename);
1039 fprintf(stderr,
"Error, both -y and -n supplied. Exiting.\n");
1044 if (proto_name && !strcmp(proto_name,
"file") && avio_check(filename, 0) == 0) {
1046 fprintf(stderr,
"File '%s' already exists. Overwrite ? [y/N] ", filename);
1049 signal(SIGINT, SIG_DFL);
1051 av_log(NULL, AV_LOG_FATAL,
"Not overwriting - exiting\n");
1057 av_log(NULL, AV_LOG_FATAL,
"File '%s' already exists. Exiting.\n", filename);
1063 if (proto_name && !strcmp(proto_name,
"file")) {
1066 if (file->
ctx->iformat->flags & AVFMT_NOFILE)
1068 if (!strcmp(filename, file->
ctx->url)) {
1069 av_log(NULL, AV_LOG_FATAL,
"Output %s same as Input #%d - exiting\n", filename, i);
1070 av_log(NULL, AV_LOG_WARNING,
"FFmpeg cannot edit existing files in-place.\n");
1080 AVIOContext *out = NULL;
1081 AVDictionaryEntry *e;
1083 if (!st->codecpar->extradata_size) {
1084 av_log(NULL, AV_LOG_WARNING,
"No extradata to dump in stream #%d:%d.\n",
1088 if (!*filename && (e = av_dict_get(st->metadata,
"filename", NULL, 0)))
1089 filename = e->value;
1091 av_log(NULL, AV_LOG_FATAL,
"No filename specified and no 'filename' tag"
1098 if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &
int_cb, NULL)) < 0) {
1099 av_log(NULL, AV_LOG_FATAL,
"Could not open file %s for writing.\n",
1104 avio_write(out, st->codecpar->extradata, st->codecpar->extradata_size);
1112 AVFormatContext *ic;
1113 const AVInputFormat *file_iformat = NULL;
1116 AVDictionary *unused_opts = NULL;
1117 AVDictionaryEntry *e = NULL;
1118 char * video_codec_name = NULL;
1119 char * audio_codec_name = NULL;
1120 char *subtitle_codec_name = NULL;
1121 char * data_codec_name = NULL;
1122 int scan_all_pmts_set = 0;
1126 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
1132 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
1140 if (!(file_iformat = av_find_input_format(o->
format))) {
1141 av_log(NULL, AV_LOG_FATAL,
"Unknown input format: '%s'\n", o->
format);
1146 if (!strcmp(filename,
"-"))
1150 strcmp(filename,
"/dev/stdin");
1153 ic = avformat_alloc_context();
1162 const AVClass *priv_class;
1166 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1167 av_opt_find(&priv_class,
"channels", NULL, 0,
1168 AV_OPT_SEARCH_FAKE_OBJ)) {
1173 const AVClass *priv_class;
1176 if (file_iformat && (priv_class = file_iformat->priv_class) &&
1177 av_opt_find(&priv_class,
"framerate", NULL, 0,
1178 AV_OPT_SEARCH_FAKE_OBJ)) {
1194 if (video_codec_name)
1196 if (audio_codec_name)
1198 if (subtitle_codec_name)
1199 ic->subtitle_codec =
find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 0);
1200 if (data_codec_name)
1203 ic->video_codec_id = video_codec_name ? ic->video_codec->id : AV_CODEC_ID_NONE;
1204 ic->audio_codec_id = audio_codec_name ? ic->audio_codec->id : AV_CODEC_ID_NONE;
1205 ic->subtitle_codec_id = subtitle_codec_name ? ic->subtitle_codec->id : AV_CODEC_ID_NONE;
1206 ic->data_codec_id = data_codec_name ? ic->data_codec->id : AV_CODEC_ID_NONE;
1208 ic->flags |= AVFMT_FLAG_NONBLOCK;
1210 ic->flags |= AVFMT_FLAG_BITEXACT;
1211 ic->interrupt_callback =
int_cb;
1213 if (!av_dict_get(o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
1214 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts",
"1", AV_DICT_DONT_OVERWRITE);
1215 scan_all_pmts_set = 1;
1218 err = avformat_open_input(&ic, filename, file_iformat, &o->
g->
format_opts);
1221 if (err == AVERROR_PROTOCOL_NOT_FOUND)
1222 av_log(NULL, AV_LOG_ERROR,
"Did you mean file:%s?\n", filename);
1225 if (scan_all_pmts_set)
1226 av_dict_set(&o->
g->
format_opts,
"scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
1231 for (i = 0; i < ic->nb_streams; i++)
1236 int orig_nb_streams = ic->nb_streams;
1240 ret = avformat_find_stream_info(ic, opts);
1242 for (i = 0; i < orig_nb_streams; i++)
1243 av_dict_free(&opts[i]);
1247 av_log(NULL, AV_LOG_FATAL,
"%s: could not find codec parameters\n", filename);
1248 if (ic->nb_streams == 0) {
1249 avformat_close_input(&ic);
1256 av_log(NULL, AV_LOG_WARNING,
"Cannot use -ss and -sseof both, using -ss for %s\n", filename);
1262 av_log(NULL, AV_LOG_ERROR,
"-sseof value must be negative; aborting\n");
1265 if (ic->duration > 0) {
1268 av_log(NULL, AV_LOG_WARNING,
"-sseof value seeks to before start of file %s; ignored\n", filename);
1272 av_log(NULL, AV_LOG_WARNING,
"Cannot use -sseof, duration of %s not known\n", filename);
1277 timestamp += ic->start_time;
1281 int64_t seek_timestamp = timestamp;
1283 if (!(ic->iformat->flags & AVFMT_SEEK_TO_PTS)) {
1284 int dts_heuristic = 0;
1285 for (i=0; i<ic->nb_streams; i++) {
1286 const AVCodecParameters *par = ic->streams[i]->codecpar;
1287 if (par->video_delay) {
1292 if (dts_heuristic) {
1293 seek_timestamp -= 3*AV_TIME_BASE / 23;
1296 ret = avformat_seek_file(ic, -1, INT64_MIN, seek_timestamp, seek_timestamp, 0);
1298 av_log(NULL, AV_LOG_WARNING,
"%s: could not seek to position %0.3f\n",
1299 filename, (
double)timestamp / AV_TIME_BASE);
1310 f = av_mallocz(
sizeof(*f));
1330 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);
1334 av_log(NULL, AV_LOG_WARNING,
"Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n",
nb_input_files, f->
readrate);
1338 f->
pkt = av_packet_alloc();
1349 while ((e = av_dict_get(
input_streams[i]->decoder_opts,
"", e,
1350 AV_DICT_IGNORE_SUFFIX)))
1351 av_dict_set(&unused_opts, e->key, NULL, 0);
1355 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
1356 const AVClass *
class = avcodec_get_class();
1357 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
1358 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1359 const AVClass *fclass = avformat_get_class();
1360 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
1361 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
1362 if (!option || foption)
1366 if (!(option->flags & AV_OPT_FLAG_DECODING_PARAM)) {
1367 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
1368 "input file #%d (%s) is not a decoding option.\n", e->key,
1374 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
1375 "input file #%d (%s) has not been used for any stream. The most "
1376 "likely reason is either wrong type (e.g. a video option with "
1377 "no video streams) or that it is a private option of some decoder "
1378 "which was not actually used for any stream.\n", e->key,
1381 av_dict_free(&unused_opts);
1386 for (j = 0; j < ic->nb_streams; j++) {
1387 AVStream *st = ic->streams[j];
1405 if (avio_open_dyn_buf(&line) < 0) {
1406 av_log(NULL, AV_LOG_FATAL,
"Could not alloc buffer for reading preset.\n");
1410 while ((c = avio_r8(s)) && c !=
'\n')
1413 avio_close_dyn_buf(line, &buf);
1421 char filename[1000];
1422 const char *base[3] = { getenv(
"AVCONV_DATADIR"),
1427 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
1431 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
1432 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
1433 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1436 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
1437 i != 1 ?
"" :
"/.avconv", preset_name);
1438 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
1446 enum AVMediaType type =
ost->
st->codecpar->codec_type;
1447 char *codec_name = NULL;
1449 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
1452 ost->
st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
1453 NULL,
ost->
st->codecpar->codec_type);
1454 ost->
enc = avcodec_find_encoder(
ost->
st->codecpar->codec_id);
1456 av_log(NULL, AV_LOG_FATAL,
"Automatic encoder selection failed for "
1457 "output stream #%d:%d. Default encoder for format %s (codec %s) is "
1458 "probably disabled. Please choose an encoder manually.\n",
1460 avcodec_get_name(
ost->
st->codecpar->codec_id));
1461 return AVERROR_ENCODER_NOT_FOUND;
1463 }
else if (!strcmp(codec_name,
"copy"))
1482 AVStream *st = avformat_new_stream(oc, NULL);
1483 int idx = oc->nb_streams - 1, ret = 0;
1484 const char *bsfs = NULL, *time_base = NULL;
1485 char *next, *codec_tag = NULL;
1490 av_log(NULL, AV_LOG_FATAL,
"Could not alloc stream.\n");
1498 if (!(
ost = av_mallocz(
sizeof(*
ost))))
1506 st->codecpar->codec_type = type;
1510 av_log(NULL, AV_LOG_FATAL,
"Error selecting an encoder for stream "
1517 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding context.\n");
1524 av_log(NULL, AV_LOG_ERROR,
"Error allocating the encoding parameters.\n");
1529 AVIOContext *s = NULL;
1530 char *buf = NULL, *arg = NULL, *preset = NULL;
1540 if (!buf[0] || buf[0] ==
'#') {
1544 if (!(arg = strchr(buf,
'='))) {
1545 av_log(NULL, AV_LOG_FATAL,
"Invalid line found in the preset file.\n");
1551 }
while (!s->eof_reached);
1555 av_log(NULL, AV_LOG_FATAL,
1556 "Preset %s specified for stream %d:%d, but could not be opened.\n",
1566 ost->
enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
1571 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1572 q.num <= 0 || q.den <= 0) {
1573 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1582 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
1584 av_log(NULL, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
1594 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
1595 av_log(NULL, AV_LOG_WARNING,
"Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
1604 if (bsfs && *bsfs) {
1605 ret = av_bsf_list_parse_str(bsfs, &
ost->
bsf_ctx);
1607 av_log(NULL, AV_LOG_ERROR,
"Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
1614 uint32_t tag = strtol(codec_tag, &next, 0);
1616 tag = AV_RL32(codec_tag);
1617 ost->
st->codecpar->codec_tag =
1624 ost->
enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
1639 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
1640 ost->
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
1645 if (
ost->
enc && av_get_exact_bits_per_sample(
ost->
enc->id) == 24)
1646 av_dict_set(&
ost->
swr_opts,
"output_sample_bits",
"24", 0);
1651 if (source_index >= 0) {
1668 const char *p = str;
1675 av_log(NULL, AV_LOG_FATAL,
"Syntax error in matrix \"%s\" at coeff %d\n", str, i);
1685 AVIOContext *pb = NULL;
1686 AVIOContext *dyn_buf = NULL;
1687 int ret = avio_open(&pb, filename, AVIO_FLAG_READ);
1688 uint8_t buf[1024], *str;
1691 av_log(NULL, AV_LOG_ERROR,
"Error opening file %s.\n", filename);
1695 ret = avio_open_dyn_buf(&dyn_buf);
1700 while ((ret = avio_read(pb, buf,
sizeof(buf))) > 0)
1701 avio_write(dyn_buf, buf, ret);
1702 avio_w8(dyn_buf, 0);
1705 ret = avio_close_dyn_buf(dyn_buf, &str);
1714 AVStream *st =
ost->
st;
1717 av_log(NULL, AV_LOG_ERROR,
"Both -filter and -filter_script set for "
1727 return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
1735 av_log(NULL, AV_LOG_ERROR,
1736 "%s '%s' was defined for %s output stream %d:%d but codec copy was selected.\n"
1737 "Filtering and streamcopy cannot be used together.\n",
1738 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
1749 AVCodecContext *video_enc;
1750 char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
1757 if (frame_rate && av_parse_video_rate(&
ost->
frame_rate, frame_rate) < 0) {
1758 av_log(NULL, AV_LOG_FATAL,
"Invalid framerate value: %s\n", frame_rate);
1763 if (max_frame_rate && av_parse_video_rate(&
ost->
max_frame_rate, max_frame_rate) < 0) {
1764 av_log(NULL, AV_LOG_FATAL,
"Invalid maximum framerate value: %s\n", max_frame_rate);
1768 if (frame_rate && max_frame_rate) {
1769 av_log(NULL, AV_LOG_ERROR,
"Only one of -fpsmax and -r can be set for a stream.\n");
1773 if ((frame_rate || max_frame_rate) &&
1775 av_log(NULL, AV_LOG_ERROR,
"Using -vsync 0 and -r/-fpsmax can produce invalid output files\n");
1778 if (frame_aspect_ratio) {
1780 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
1781 q.num <= 0 || q.den <= 0) {
1782 av_log(NULL, AV_LOG_FATAL,
"Invalid aspect ratio: %s\n", frame_aspect_ratio);
1792 const char *p = NULL;
1793 char *frame_size = NULL;
1794 char *frame_pix_fmt = NULL;
1795 char *intra_matrix = NULL, *inter_matrix = NULL;
1796 char *chroma_intra_matrix = NULL;
1801 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
1802 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
1808 if (frame_pix_fmt && *frame_pix_fmt ==
'+') {
1810 if (!*++frame_pix_fmt)
1811 frame_pix_fmt = NULL;
1813 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
1814 av_log(NULL, AV_LOG_FATAL,
"Unknown pixel format requested: %s.\n", frame_pix_fmt);
1817 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
1820 video_enc->gop_size = 0;
1823 if (!(video_enc->intra_matrix = av_mallocz(
sizeof(*video_enc->intra_matrix) * 64))) {
1824 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1830 if (chroma_intra_matrix) {
1831 uint16_t *p = av_mallocz(
sizeof(*video_enc->chroma_intra_matrix) * 64);
1833 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for intra matrix.\n");
1836 video_enc->chroma_intra_matrix = p;
1841 if (!(video_enc->inter_matrix = av_mallocz(
sizeof(*video_enc->inter_matrix) * 64))) {
1842 av_log(NULL, AV_LOG_FATAL,
"Could not allocate memory for inter matrix.\n");
1849 for (i = 0; p; i++) {
1851 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
1853 av_log(NULL, AV_LOG_FATAL,
"error parsing rc_override\n");
1856 video_enc->rc_override =
1857 av_realloc_array(video_enc->rc_override,
1858 i + 1,
sizeof(RcOverride));
1859 if (!video_enc->rc_override) {
1860 av_log(NULL, AV_LOG_FATAL,
"Could not (re)allocate memory for rc_override.\n");
1863 video_enc->rc_override[i].start_frame = start;
1864 video_enc->rc_override[i].end_frame = end;
1866 video_enc->rc_override[i].qscale = q;
1867 video_enc->rc_override[i].quality_factor = 1.0;
1870 video_enc->rc_override[i].qscale = 0;
1871 video_enc->rc_override[i].quality_factor = -q/100.0;
1876 video_enc->rc_override_count = i;
1879 video_enc->flags|= AV_CODEC_FLAG_PSNR;
1885 video_enc->flags |= AV_CODEC_FLAG_PASS1;
1889 video_enc->flags |= AV_CODEC_FLAG_PASS2;
1900 char logfilename[1024];
1903 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
1907 if (!strcmp(
ost->
enc->name,
"libx264")) {
1908 av_dict_set(&
ost->
encoder_opts,
"stats", logfilename, AV_DICT_DONT_OVERWRITE);
1910 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
1914 av_log(NULL, AV_LOG_FATAL,
"Error reading log file '%s' for pass-2 encoding\n",
1918 video_enc->stats_in = logbuffer;
1920 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
1921 f = av_fopen_utf8(logfilename,
"wb");
1923 av_log(NULL, AV_LOG_FATAL,
1924 "Cannot write log file '%s' for pass-1 encoding: %s\n",
1925 logfilename, strerror(errno));
1961 AVCodecContext *audio_enc;
1967 audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
1973 char *sample_fmt = NULL;
1979 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
1980 av_log(NULL, AV_LOG_FATAL,
"Invalid sample format '%s'\n", sample_fmt);
2003 av_log(NULL, AV_LOG_FATAL,
"Cannot determine input stream for channel mapping %d.%d\n",
2035 av_log(NULL, AV_LOG_FATAL,
"Data stream encoding not supported yet (only streamcopy)\n");
2048 av_log(NULL, AV_LOG_FATAL,
"Unknown stream encoding not supported yet (only streamcopy)\n");
2067 AVCodecContext *subtitle_enc;
2073 subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2078 char *frame_size = NULL;
2081 if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
2082 av_log(NULL, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
2098 av_strlcpy(idx_str, arg,
sizeof(idx_str));
2099 p = strchr(idx_str,
':');
2101 av_log(NULL, AV_LOG_FATAL,
2102 "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
2115 AVFormatContext *is = ifile->
ctx;
2116 AVFormatContext *os = ofile->
ctx;
2120 tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters,
sizeof(*os->chapters));
2122 return AVERROR(ENOMEM);
2125 for (i = 0; i < is->nb_chapters; i++) {
2126 AVChapter *in_ch = is->chapters[i], *out_ch;
2128 int64_t ts_off = av_rescale_q(start_time - ifile->
ts_offset,
2129 AV_TIME_BASE_Q, in_ch->time_base);
2131 av_rescale_q(ofile->
recording_time, AV_TIME_BASE_Q, in_ch->time_base);
2134 if (in_ch->end < ts_off)
2136 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
2139 out_ch = av_mallocz(
sizeof(AVChapter));
2141 return AVERROR(ENOMEM);
2143 out_ch->id = in_ch->id;
2144 out_ch->time_base = in_ch->time_base;
2145 out_ch->start = FFMAX(0, in_ch->start - ts_off);
2146 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
2149 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
2151 os->chapters[os->nb_chapters++] = out_ch;
2157 AVFormatContext *oc)
2161 switch (ofilter->
type) {
2165 av_log(NULL, AV_LOG_FATAL,
"Only video and audio filters are supported "
2177 av_log(NULL, AV_LOG_ERROR,
"Streamcopy requested for output stream %d:%d, "
2178 "which is fed from a complex filtergraph. Filtering and streamcopy "
2184 const char *opt =
ost->
filters ?
"-vf/-af/-filter" :
"-filter_script";
2185 av_log(NULL, AV_LOG_ERROR,
2186 "%s '%s' was specified through the %s option "
2187 "for output stream %d:%d, which is fed from a complex filtergraph.\n"
2188 "%s and -filter_complex cannot be used together for the same stream.\n",
2189 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
2195 avfilter_inout_free(&ofilter->
out_tmp);
2212 AVFormatContext *oc;
2217 AVDictionary *unused_opts = NULL;
2218 AVDictionaryEntry *e = NULL;
2219 int format_flags = 0;
2223 av_log(NULL, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
2229 av_log(NULL, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
2237 of = av_mallocz(
sizeof(*of));
2249 if (!strcmp(filename,
"-"))
2252 err = avformat_alloc_output_context2(&oc, NULL, o->
format, filename);
2262 oc->interrupt_callback =
int_cb;
2266 const AVOption *o = av_opt_find(oc,
"fflags", NULL, 0, 0);
2267 av_opt_eval_flags(oc, o, e->value, &format_flags);
2270 format_flags |= AVFMT_FLAG_BITEXACT;
2271 oc->flags |= AVFMT_FLAG_BITEXACT;
2283 switch (ofilter->
type) {
2293 char *subtitle_codec_name = NULL;
2297 if (!o->
video_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_VIDEO) != AV_CODEC_ID_NONE) {
2298 int best_score = 0, idx = -1;
2299 int qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
2302 int file_best_score = 0, file_best_idx = -1;
2306 score =
ist->st->codecpar->width *
ist->st->codecpar->height
2307 + 100000000 * !!(
ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2308 + 5000000*!!(
ist->st->disposition & AV_DISPOSITION_DEFAULT);
2309 if (
ist->user_set_discard == AVDISCARD_ALL)
2311 if((qcr!=MKTAG(
'A',
'P',
'I',
'C')) && (
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2313 if (
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2314 score > file_best_score) {
2315 if((qcr==MKTAG(
'A',
'P',
'I',
'C')) && !(
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2317 file_best_score = score;
2321 if (file_best_idx >= 0) {
2322 if((qcr == MKTAG(
'A',
'P',
'I',
'C')) || !(
ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC))
2323 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->st->disposition & AV_DISPOSITION_DEFAULT);
2324 if (file_best_score > best_score) {
2325 best_score = file_best_score;
2326 idx = file_best_idx;
2335 if (!o->
audio_disable && av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_AUDIO) != AV_CODEC_ID_NONE) {
2336 int best_score = 0, idx = -1;
2339 int file_best_score = 0, file_best_idx = -1;
2343 score =
ist->st->codecpar->channels
2344 + 100000000 * !!(
ist->st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
2345 + 5000000*!!(
ist->st->disposition & AV_DISPOSITION_DEFAULT);
2346 if (
ist->user_set_discard == AVDISCARD_ALL)
2348 if (
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2349 score > file_best_score) {
2350 file_best_score = score;
2354 if (file_best_idx >= 0) {
2355 file_best_score -= 5000000*!!(
input_streams[file_best_idx]->
st->disposition & AV_DISPOSITION_DEFAULT);
2356 if (file_best_score > best_score) {
2357 best_score = file_best_score;
2358 idx = file_best_idx;
2368 if (!o->
subtitle_disable && (avcodec_find_encoder(oc->oformat->subtitle_codec) || subtitle_codec_name)) {
2370 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2371 AVCodecDescriptor
const *input_descriptor =
2372 avcodec_descriptor_get(
input_streams[i]->st->codecpar->codec_id);
2373 AVCodecDescriptor
const *output_descriptor = NULL;
2374 AVCodec
const *output_codec =
2375 avcodec_find_encoder(oc->oformat->subtitle_codec);
2376 int input_props = 0, output_props = 0;
2380 output_descriptor = avcodec_descriptor_get(output_codec->id);
2381 if (input_descriptor)
2382 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2383 if (output_descriptor)
2384 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
2385 if (subtitle_codec_name ||
2386 input_props & output_props ||
2388 (input_descriptor && output_descriptor &&
2389 (!input_descriptor->props ||
2390 !output_descriptor->props))) {
2398 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, filename, NULL, AVMEDIA_TYPE_DATA);
2402 if (
input_streams[i]->st->codecpar->codec_type == AVMEDIA_TYPE_DATA
2423 if (out && !strcmp(out->name, map->
linklabel)) {
2431 av_log(NULL, AV_LOG_FATAL,
"Output with label '%s' does not exist "
2432 "in any defined filter graph, or was already used elsewhere.\n", map->
linklabel);
2440 if (
ist->user_set_discard == AVDISCARD_ALL) {
2441 av_log(NULL, AV_LOG_FATAL,
"Stream #%d:%d is disabled and cannot be mapped.\n",
2447 if(o-> audio_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
2449 if(o-> video_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
2451 if(o-> data_disable &&
ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
2455 switch (
ist->st->codecpar->codec_type) {
2461 case AVMEDIA_TYPE_UNKNOWN:
2468 "Cannot map stream #%d:%d - unsupported type.\n",
2471 av_log(NULL, AV_LOG_FATAL,
2472 "If you want unsupported types ignored instead "
2473 "of failing, please use the -ignore_unknown option\n"
2474 "If you want them copied, please use -copy_unknown\n");
2488 uint8_t *attachment;
2492 if ((err = avio_open2(&pb, o->
attachments[i], AVIO_FLAG_READ, &
int_cb, NULL)) < 0) {
2493 av_log(NULL, AV_LOG_FATAL,
"Could not open attachment file %s.\n",
2497 if ((len = avio_size(pb)) <= 0) {
2498 av_log(NULL, AV_LOG_FATAL,
"Could not get size of the attachment %s.\n",
2502 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
2503 !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
2504 av_log(NULL, AV_LOG_FATAL,
"Attachment %s too large.\n",
2508 avio_read(pb, attachment, len);
2509 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2514 ost->
st->codecpar->extradata = attachment;
2515 ost->
st->codecpar->extradata_size = len;
2518 av_dict_set(&
ost->
st->metadata,
"filename", (p && *p) ? p + 1 : o->
attachments[i], AV_DICT_DONT_OVERWRITE);
2522 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2524 av_log(NULL, AV_LOG_ERROR,
"Output file #%d does not contain any stream\n",
nb_output_files - 1);
2533 AV_DICT_IGNORE_SUFFIX)))
2534 av_dict_set(&unused_opts, e->key, NULL, 0);
2538 while ((e = av_dict_get(unused_opts,
"", e, AV_DICT_IGNORE_SUFFIX))) {
2539 const AVClass *
class = avcodec_get_class();
2540 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
2541 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2542 const AVClass *fclass = avformat_get_class();
2543 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
2544 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2545 if (!option || foption)
2549 if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
2550 av_log(NULL, AV_LOG_ERROR,
"Codec AVOption %s (%s) specified for "
2551 "output file #%d (%s) is not an encoding option.\n", e->key,
2558 if (!strcmp(e->key,
"gop_timecode"))
2561 av_log(NULL, AV_LOG_WARNING,
"Codec AVOption %s (%s) specified for "
2562 "output file #%d (%s) has not been used for any stream. The most "
2563 "likely reason is either wrong type (e.g. a video option with "
2564 "no video streams) or that it is a private option of some encoder "
2565 "which was not actually used for any stream.\n", e->key,
2568 av_dict_free(&unused_opts);
2578 if (
ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2579 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2582 av_log(NULL, AV_LOG_ERROR,
2583 "Error initializing a simple filtergraph between streams "
2596 case AVMEDIA_TYPE_VIDEO:
2600 if (
ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
2602 }
else if (
ost->
enc->pix_fmts) {
2604 while (
ost->
enc->pix_fmts[count] != AV_PIX_FMT_NONE)
2612 case AVMEDIA_TYPE_AUDIO:
2613 if (
ost->
enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
2615 }
else if (
ost->
enc->sample_fmts) {
2617 while (
ost->
enc->sample_fmts[count] != AV_SAMPLE_FMT_NONE)
2626 }
else if (
ost->
enc->supported_samplerates) {
2628 while (
ost->
enc->supported_samplerates[count])
2638 }
else if (
ost->
enc->channel_layouts) {
2640 while (
ost->
enc->channel_layouts[count])
2654 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2655 if (!av_filename_number_test(oc->url)) {
2662 av_log(NULL, AV_LOG_ERROR,
2663 "No input streams but output needs an input stream\n");
2667 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2672 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
2673 &oc->interrupt_callback,
2678 }
else if (strcmp(oc->oformat->name,
"image2")==0 && !av_filename_number_test(filename))
2682 av_dict_set_int(&of->
opts,
"preload", o->
mux_preload*AV_TIME_BASE, 0);
2692 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d while processing metadata maps\n", in_file_index);
2696 in_file_index >= 0 ?
2711 av_log(NULL, AV_LOG_FATAL,
"Invalid input file index %d in chapter mapping.\n",
2723 AV_DICT_DONT_OVERWRITE);
2725 av_dict_set(&oc->metadata,
"duration", NULL, 0);
2726 av_dict_set(&oc->metadata,
"creation_time", NULL, 0);
2727 av_dict_set(&oc->metadata,
"company_name", NULL, 0);
2728 av_dict_set(&oc->metadata,
"product_name", NULL, 0);
2729 av_dict_set(&oc->metadata,
"product_version", NULL, 0);
2737 av_dict_copy(&
output_streams[i]->st->metadata,
ist->st->metadata, AV_DICT_DONT_OVERWRITE);
2739 av_dict_set(&
output_streams[i]->st->metadata,
"encoder", NULL, 0);
2750 const char *p2 = av_get_token(&p,
":");
2751 const char *to_dealloc = p2;
2758 key = av_get_token(&p2,
"=");
2760 av_freep(&to_dealloc);
2766 if (!strcmp(key,
"program_num"))
2767 progid = strtol(p2, NULL, 0);
2768 av_freep(&to_dealloc);
2772 program = av_new_program(oc, progid);
2776 const char *p2 = av_get_token(&p,
":");
2777 const char *to_dealloc = p2;
2783 key = av_get_token(&p2,
"=");
2785 av_log(NULL, AV_LOG_FATAL,
2786 "No '=' character in program string %s.\n",
2794 if (!strcmp(key,
"title")) {
2795 av_dict_set(&program->metadata,
"title", p2, 0);
2796 }
else if (!strcmp(key,
"program_num")) {
2797 }
else if (!strcmp(key,
"st")) {
2798 int st_num = strtol(p2, NULL, 0);
2799 av_program_add_stream_index(oc, progid, st_num);
2801 av_log(NULL, AV_LOG_FATAL,
"Unknown program key %s.\n", key);
2804 av_freep(&to_dealloc);
2813 const char *stream_spec;
2814 int index = 0, j, ret = 0;
2818 av_log(NULL, AV_LOG_FATAL,
"No '=' character in metadata string %s.\n",
2826 for (j = 0; j < oc->nb_streams; j++) {
2831 double theta = av_strtod(val, &tail);
2837 av_dict_set(&oc->streams[j]->metadata, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
2849 if (index < 0 || index >= oc->nb_chapters) {
2850 av_log(NULL, AV_LOG_FATAL,
"Invalid chapter index %d in metadata specifier.\n",
index);
2853 m = &oc->chapters[
index]->metadata;
2856 if (index < 0 || index >= oc->nb_programs) {
2857 av_log(NULL, AV_LOG_FATAL,
"Invalid program index %d in metadata specifier.\n",
index);
2860 m = &oc->programs[
index]->metadata;
2863 av_log(NULL, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", o->
metadata[i].
specifier);
2866 av_dict_set(m, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
2877 enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;
2878 const char *
const frame_rates[] = {
"25",
"30000/1001",
"24000/1001" };
2880 if (!strncmp(arg,
"pal-", 4)) {
2883 }
else if (!strncmp(arg,
"ntsc-", 5)) {
2886 }
else if (!strncmp(arg,
"film-", 5)) {
2897 if (st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
2899 fr = st->time_base.den * 1000LL / st->time_base.num;
2903 }
else if ((fr == 29970) || (fr == 23976)) {
2908 if (norm != UNKNOWN)
2912 if (norm != UNKNOWN)
2913 av_log(NULL, AV_LOG_INFO,
"Assuming %s for target.\n", norm == PAL ?
"PAL" :
"NTSC");
2916 if (norm == UNKNOWN) {
2917 av_log(NULL, AV_LOG_FATAL,
"Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
2918 av_log(NULL, AV_LOG_FATAL,
"Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
2919 av_log(NULL, AV_LOG_FATAL,
"or set a framerate with \"-r xxx\".\n");
2923 if (!strcmp(arg,
"vcd")) {
2928 parse_option(o,
"s", norm == PAL ?
"352x288" :
"352x240", options);
2930 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2950 }
else if (!strcmp(arg,
"svcd")) {
2956 parse_option(o,
"s", norm == PAL ?
"480x576" :
"480x480", options);
2959 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2972 }
else if (!strcmp(arg,
"dvd")) {
2978 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
2981 opt_default(NULL,
"g", norm == PAL ?
"15" :
"18");
2994 }
else if (!strncmp(arg,
"dv", 2)) {
2998 parse_option(o,
"s", norm == PAL ?
"720x576" :
"720x480", options);
2999 parse_option(o,
"pix_fmt", !strncmp(arg,
"dv50", 4) ?
"yuv422p" :
3000 norm == PAL ?
"yuv420p" :
"yuv411p", options);
3007 av_log(NULL, AV_LOG_ERROR,
"Unknown target: %s\n", arg);
3008 return AVERROR(EINVAL);
3027 time_t today2 = time(NULL);
3028 struct tm *today = localtime(&today2);
3031 av_log(NULL, AV_LOG_FATAL,
"Unable to get current time: %s\n", strerror(errno));
3035 snprintf(filename,
sizeof(filename),
"vstats_%02d%02d%02d.log", today->tm_hour, today->tm_min,
3082 char filename[1000], line[1000], tmp_line[1000];
3083 const char *codec_name = NULL;
3089 if (!(f =
get_preset_file(filename,
sizeof(filename), arg, *opt ==
'f', codec_name))) {
3090 if(!strncmp(arg,
"libx264-lossless", strlen(
"libx264-lossless"))){
3091 av_log(NULL, AV_LOG_FATAL,
"Please use -preset <speed> -qp 0\n");
3093 av_log(NULL, AV_LOG_FATAL,
"File for preset '%s' not found\n", arg);
3097 while (fgets(line,
sizeof(line), f)) {
3098 char *key = tmp_line, *value, *endptr;
3100 if (strcspn(line,
"#\n\r") == 0)
3102 av_strlcpy(tmp_line, line,
sizeof(tmp_line));
3103 if (!av_strtok(key,
"=", &value) ||
3104 !av_strtok(value,
"\r\n", &endptr)) {
3105 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid syntax: '%s'\n", filename, line);
3108 av_log(NULL, AV_LOG_DEBUG,
"ffpreset[%s]: set '%s' = '%s'\n", filename, key, value);
3115 av_log(NULL, AV_LOG_FATAL,
"%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
3116 filename, line, key, value);
3130 char *s = av_asprintf(
"%s:%c", opt + 1, *opt);
3132 return AVERROR(ENOMEM);
3142 if(!strcmp(opt,
"ab")){
3145 }
else if(!strcmp(opt,
"b")){
3146 av_log(NULL, AV_LOG_WARNING,
"Please use -b:a or -b:v, -b is ambiguous\n");
3160 if(!strcmp(opt,
"qscale")){
3161 av_log(NULL, AV_LOG_WARNING,
"Please use -q:a or -q:v, -qscale is ambiguous\n");
3164 s = av_asprintf(
"q%s", opt + 6);
3166 return AVERROR(ENOMEM);
3175 if(!strcmp(opt,
"profile")){
3176 av_log(NULL, AV_LOG_WARNING,
"Please use -profile:a or -profile:v, -profile is ambiguous\n");
3177 av_dict_set(&o->
g->
codec_opts,
"profile:v", arg, 0);
3196int opt_vsync(
void *optctx,
const char *opt,
const char *arg)
3212 char *tcr = av_asprintf(
"timecode=%s", arg);
3214 return AVERROR(ENOMEM);
3217 ret = av_dict_set(&o->
g->
codec_opts,
"gop_timecode", arg, 0);
3225 char layout_str[32];
3228 int ret, channels, ac_str_size;
3231 layout = av_get_channel_layout(arg);
3233 av_log(NULL, AV_LOG_ERROR,
"Unknown channel layout: %s\n", arg);
3234 return AVERROR(EINVAL);
3236 snprintf(layout_str,
sizeof(layout_str),
"%"PRIu64, layout);
3242 channels = av_get_channel_layout_nb_channels(layout);
3243 snprintf(layout_str,
sizeof(layout_str),
"%d", channels);
3244 stream_str = strchr(opt,
':');
3245 ac_str_size = 3 + (stream_str ? strlen(stream_str) : 0);
3246 ac_str = av_mallocz(ac_str_size);
3248 return AVERROR(ENOMEM);
3249 av_strlcpy(ac_str,
"ac", 3);
3251 av_strlcat(ac_str, stream_str, ac_str_size);
3268 return AVERROR(ENOMEM);
3272 return AVERROR(ENOMEM);
3283 return AVERROR(EINVAL);
3287 return AVERROR(ENOMEM);
3301 int show_advanced = 0, show_avoptions = 0;
3304 if (!strcmp(opt,
"long"))
3306 else if (!strcmp(opt,
"full"))
3307 show_advanced = show_avoptions = 1;
3309 av_log(NULL, AV_LOG_ERROR,
"Unknown help option '%s'.\n", opt);
3315 " -h -- print basic options\n"
3316 " -h long -- print more options\n"
3317 " -h full -- print all options (including all format and codec specific options, very long)\n"
3318 " -h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol\n"
3319 " See man %s for detailed description of the options.\n"
3326 "instead of just one file):",
3354 if (show_avoptions) {
3355 int flags = AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_ENCODING_PARAM;
3361#if CONFIG_SWRESAMPLE
3364 show_help_children(avfilter_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM);
3365 show_help_children(av_bsf_get_class(), AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_BSF_PARAM);
3371 av_log(NULL, AV_LOG_INFO,
"Hyper fast Audio and Video encoder\n");
3372 av_log(NULL, AV_LOG_INFO,
"usage: %s [options] [[infile options] -i infile]... {[outfile options] outfile}...\n",
program_name);
3373 av_log(NULL, AV_LOG_INFO,
"\n");
3382 [GROUP_OUTFILE] = {
"output url", NULL,
OPT_OUTPUT },
3383 [GROUP_INFILE] = {
"input url",
"i",
OPT_INPUT },
3400 av_log(NULL, AV_LOG_ERROR,
"Error parsing options for %s file "
3401 "%s.\n", inout, g->
arg);
3406 av_log(NULL, AV_LOG_DEBUG,
"Opening an %s file: %s.\n", inout, g->
arg);
3407 ret = open_file(&o, g->
arg);
3410 av_log(NULL, AV_LOG_ERROR,
"Error opening %s file %s.\n",
3414 av_log(NULL, AV_LOG_DEBUG,
"Successfully opened the file.\n");
3426 memset(&octx, 0,
sizeof(octx));
3430 FF_ARRAY_ELEMS(groups));
3432 av_log(NULL, AV_LOG_FATAL,
"Error splitting the argument list: ");
3439 av_log(NULL, AV_LOG_FATAL,
"Error parsing global options: ");
3449 av_log(NULL, AV_LOG_FATAL,
"Error opening input files: ");
3456 av_log(NULL, AV_LOG_FATAL,
"Error initializing complex filters.\n");
3463 av_log(NULL, AV_LOG_FATAL,
"Error opening output files: ");
3472 av_strerror(ret, error,
sizeof(error));
3473 av_log(NULL, AV_LOG_FATAL,
"%s\n", error);
3478int opt_progress(
void *optctx,
const char *opt,
const char *arg)
3480 AVIOContext *avio = NULL;
3483 if (!strcmp(arg,
"-"))
3485 ret = avio_open2(&avio, arg, AVIO_FLAG_WRITE, &
int_cb, NULL);
3487 av_log(NULL, AV_LOG_ERROR,
"Failed to open progress URL \"%s\": %s\n",
3488 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