45#include "libavformat/avformat.h"
46#include "libavformat/avio.h"
48#include "libavcodec/avcodec.h"
50#include "libavfilter/avfilter.h"
52#include "libavutil/avassert.h"
53#include "libavutil/avstring.h"
54#include "libavutil/avutil.h"
55#include "libavutil/bprint.h"
56#include "libavutil/dict.h"
57#include "libavutil/getenv_utf8.h"
58#include "libavutil/intreadwrite.h"
59#include "libavutil/log.h"
60#include "libavutil/mem.h"
61#include "libavutil/opt.h"
62#include "libavutil/parseutils.h"
63#include "libavutil/pixdesc.h"
65#define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
111 const char *opt_name,
int flag)
113 const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0);
116 const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0);
120 av_opt_eval_flags(ctx, o, e->value, &val);
121 return !!(val & flag);
129 enum AVMediaType type = ost->
st->codecpar->codec_type;
130 char *codec_name = NULL;
134 if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) {
137 ost->
st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url,
138 NULL, ost->
st->codecpar->codec_type);
139 *enc = avcodec_find_encoder(ost->
st->codecpar->codec_id);
141 av_log(ost, AV_LOG_FATAL,
"Automatic encoder selection failed "
142 "Default encoder for format %s (codec %s) is "
143 "probably disabled. Please choose an encoder manually.\n",
144 s->oformat->name, avcodec_get_name(ost->
st->codecpar->codec_id));
145 return AVERROR_ENCODER_NOT_FOUND;
147 }
else if (strcmp(codec_name,
"copy")) {
149 ost->
st->codecpar->codec_id = (*enc)->id;
156static char *
get_line(AVIOContext *s, AVBPrint *bprint)
160 while ((c = avio_r8(s)) && c !=
'\n')
161 av_bprint_chars(bprint, c, 1);
163 if (!av_bprint_is_complete(bprint))
172 char *env_avconv_datadir = getenv_utf8(
"AVCONV_DATADIR");
173 char *env_home = getenv_utf8(
"HOME");
174 const char *base[3] = { env_avconv_datadir,
179 for (i = 0; i < FF_ARRAY_ELEMS(base) && ret < 0; i++) {
183 snprintf(filename,
sizeof(filename),
"%s%s/%s-%s.avpreset", base[i],
184 i != 1 ?
"" :
"/.avconv", codec_name, preset_name);
185 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
188 snprintf(filename,
sizeof(filename),
"%s%s/%s.avpreset", base[i],
189 i != 1 ?
"" :
"/.avconv", preset_name);
190 ret = avio_open2(s, filename, AVIO_FLAG_READ, &
int_cb, NULL);
193 freeenv_utf8(env_home);
194 freeenv_utf8(env_avconv_datadir);
216 ret = avio_open2(&esf->
io, path, AVIO_FLAG_WRITE, &
int_cb, NULL);
218 av_log(NULL, AV_LOG_ERROR,
"Error opening stats file '%s': %s\n",
219 path, av_err2str(ret));
223 esf->
path = av_strdup(path);
225 return AVERROR(ENOMEM);
243 const char **pstr,
char delim)
245 const char *str = *pstr;
255 dst = av_malloc(len + 1);
257 return AVERROR(ENOMEM);
259 for (idx = 0; *str; idx++, str++) {
260 if (str[0] ==
'\\' && str[1])
262 else if (*str == delim)
282 const char *path,
const char *fmt_spec)
284 static const struct {
289 int need_input_data:1;
309 const char *next = fmt_spec;
319 ret =
unescape(&val, &val_len, &next,
'{');
337 ret =
unescape(&val, &val_len, &next,
'}');
342 av_log(NULL, AV_LOG_ERROR,
343 "Empty formatting directive in: %s\n", fmt_spec);
344 return AVERROR(EINVAL);
348 av_log(NULL, AV_LOG_ERROR,
349 "Missing closing brace in: %s\n", fmt_spec);
350 ret = AVERROR(EINVAL);
358 for (
size_t i = 0; i < FF_ARRAY_ELEMS(fmt_specs); i++) {
359 if (!strcmp(val, fmt_specs[i].str)) {
360 if ((pre && fmt_specs[i].post_only) || (!pre && fmt_specs[i].pre_only)) {
361 av_log(NULL, AV_LOG_ERROR,
362 "Format directive '%s' may only be used %s-encoding\n",
363 val, pre ?
"post" :
"pre");
364 ret = AVERROR(EINVAL);
368 c->
type = fmt_specs[i].type;
370 if (fmt_specs[i].need_input_data) {
374 av_log(ost, AV_LOG_WARNING,
375 "Format directive '%s' is unavailable, because "
376 "this output stream has no associated input stream\n",
386 av_log(NULL, AV_LOG_ERROR,
"Invalid format directive: %s\n", val);
387 ret = AVERROR(EINVAL);
412 .class_name =
"OutputStream",
413 .version = LIBAVUTIL_VERSION_INT,
415 .category = AV_CLASS_CATEGORY_MUXER,
420 const char *type_str = av_get_media_type_string(type);
438 AVFormatContext *oc = mux->
fc;
442 AVStream *st = avformat_new_stream(oc, NULL);
444 const char *bsfs = NULL, *time_base = NULL;
445 char *next, *codec_tag = NULL;
458 ms->
muxing_queue = av_fifo_alloc2(8,
sizeof(AVPacket*), 0);
466 st->codecpar->codec_type = type;
470 av_log(ost, AV_LOG_FATAL,
"Error selecting an encoder\n");
475 ost->
enc_ctx = avcodec_alloc_context3(enc);
489 ost->
pkt = av_packet_alloc();
494 AVCodecContext *enc = ost->
enc_ctx;
495 AVIOContext *s = NULL;
496 char *buf = NULL, *arg = NULL, *preset = NULL;
497 const char *enc_stats_pre = NULL, *enc_stats_post = NULL, *mux_stats = NULL;
507 av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
509 av_bprint_clear(&bprint);
511 if (!buf[0] || buf[0] ==
'#')
513 if (!(arg = strchr(buf,
'='))) {
514 av_log(ost, AV_LOG_FATAL,
"Invalid line found in the preset file.\n");
518 av_dict_set(&ost->
encoder_opts, buf, arg, AV_DICT_DONT_OVERWRITE);
519 }
while (!s->eof_reached);
520 av_bprint_finalize(&bprint, NULL);
524 av_log(ost, AV_LOG_FATAL,
525 "Preset %s specified, but could not be opened.\n", preset);
531 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
532 const char *format =
"{fidx} {sidx} {n} {t}";
542 if (enc_stats_post &&
543 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
544 const char *format =
"{fidx} {sidx} {n} {t}";
555 (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO)) {
556 const char *format =
"{fidx} {sidx} {n} {t}";
573 AV_CODEC_FLAG_BITEXACT);
579 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
580 q.num <= 0 || q.den <= 0) {
581 av_log(ost, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
590 if (av_parse_ratio(&q, time_base, INT_MAX, 0, NULL) < 0 ||
592 av_log(ost, AV_LOG_FATAL,
"Invalid time base: %s\n", time_base);
602 if (!*p && type != AVMEDIA_TYPE_VIDEO) {
603 av_log(ost, AV_LOG_WARNING,
"Applying unspecific -frames to non video streams, maybe you meant -vframes ?\n");
613 ret = av_bsf_list_parse_str(bsfs, &ms->
bsf_ctx);
615 av_log(ost, AV_LOG_ERROR,
"Error parsing bitstream filter sequence '%s': %s\n", bsfs, av_err2str(ret));
622 uint32_t tag = strtol(codec_tag, &next, 0);
624 tag = AV_RL32(codec_tag);
625 ost->
st->codecpar->codec_tag = tag;
631 if (ost->
enc_ctx && qscale >= 0) {
632 ost->
enc_ctx->flags |= AV_CODEC_FLAG_QSCALE;
633 ost->
enc_ctx->global_quality = FF_QP2LAMBDA * qscale;
648 if (oc->oformat->flags & AVFMT_GLOBALHEADER && ost->
enc_ctx)
649 ost->
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
654 if (ost->
enc_ctx && av_get_exact_bits_per_sample(ost->
enc_ctx->codec_id) == 24)
655 av_dict_set(&ost->
swr_opts,
"output_sample_bits",
"24", 0);
673 AVStream *st = ost->
st;
676 av_log(ost, AV_LOG_ERROR,
"Both -filter and -filter_script set\n");
683 return av_strdup(ost->
filters);
685 return av_strdup(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ?
693 av_log(ost, AV_LOG_ERROR,
694 "%s '%s' was defined, but codec copy was selected.\n"
695 "Filtering and streamcopy cannot be used together.\n",
696 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
712 av_log(logctx, AV_LOG_FATAL,
713 "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
722 AVFormatContext *oc = mux->
fc;
725 char *frame_rate = NULL, *max_frame_rate = NULL, *frame_aspect_ratio = NULL;
731 if (frame_rate && av_parse_video_rate(&ost->
frame_rate, frame_rate) < 0) {
732 av_log(ost, AV_LOG_FATAL,
"Invalid framerate value: %s\n", frame_rate);
737 if (max_frame_rate && av_parse_video_rate(&ost->
max_frame_rate, max_frame_rate) < 0) {
738 av_log(ost, AV_LOG_FATAL,
"Invalid maximum framerate value: %s\n", max_frame_rate);
742 if (frame_rate && max_frame_rate) {
743 av_log(ost, AV_LOG_ERROR,
"Only one of -fpsmax and -r can be set for a stream.\n");
748 if (frame_aspect_ratio) {
750 if (av_parse_ratio(&q, frame_aspect_ratio, 255, 0, NULL) < 0 ||
751 q.num <= 0 || q.den <= 0) {
752 av_log(ost, AV_LOG_FATAL,
"Invalid aspect ratio: %s\n", frame_aspect_ratio);
762 AVCodecContext *video_enc = ost->
enc_ctx;
763 const char *p = NULL, *fps_mode = NULL;
764 char *frame_size = NULL;
765 char *frame_pix_fmt = NULL;
766 char *intra_matrix = NULL, *inter_matrix = NULL;
767 char *chroma_intra_matrix = NULL;
772 if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
773 av_log(ost, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
778 if (frame_pix_fmt && *frame_pix_fmt ==
'+') {
780 if (!*++frame_pix_fmt)
781 frame_pix_fmt = NULL;
783 if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == AV_PIX_FMT_NONE) {
784 av_log(ost, AV_LOG_FATAL,
"Unknown pixel format requested: %s.\n", frame_pix_fmt);
787 st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
791 if (!(video_enc->intra_matrix = av_mallocz(
sizeof(*video_enc->intra_matrix) * 64)))
796 if (chroma_intra_matrix) {
797 uint16_t *p = av_mallocz(
sizeof(*video_enc->chroma_intra_matrix) * 64);
800 video_enc->chroma_intra_matrix = p;
805 if (!(video_enc->inter_matrix = av_mallocz(
sizeof(*video_enc->inter_matrix) * 64)))
811 for (i = 0; p; i++) {
813 int e = sscanf(p,
"%d,%d,%d", &start, &end, &q);
815 av_log(ost, AV_LOG_FATAL,
"error parsing rc_override\n");
818 video_enc->rc_override =
819 av_realloc_array(video_enc->rc_override,
820 i + 1,
sizeof(RcOverride));
821 if (!video_enc->rc_override) {
822 av_log(ost, AV_LOG_FATAL,
"Could not (re)allocate memory for rc_override.\n");
825 video_enc->rc_override[i].start_frame = start;
826 video_enc->rc_override[i].end_frame = end;
828 video_enc->rc_override[i].qscale = q;
829 video_enc->rc_override[i].quality_factor = 1.0;
832 video_enc->rc_override[i].qscale = 0;
833 video_enc->rc_override[i].quality_factor = -q/100.0;
838 video_enc->rc_override_count = i;
842 av_log(ost, AV_LOG_WARNING,
"The -psnr option is deprecated, use -flags +psnr\n");
843 video_enc->flags|= AV_CODEC_FLAG_PSNR;
851 video_enc->flags |= AV_CODEC_FLAG_PASS1;
852 av_dict_set(&ost->
encoder_opts,
"flags",
"+pass1", AV_DICT_APPEND);
855 video_enc->flags |= AV_CODEC_FLAG_PASS2;
856 av_dict_set(&ost->
encoder_opts,
"flags",
"+pass2", AV_DICT_APPEND);
867 char logfilename[1024];
874 snprintf(logfilename,
sizeof(logfilename),
"%s-%d.log",
878 if (!strcmp(ost->
enc_ctx->codec->name,
"libx264")) {
879 av_dict_set(&ost->
encoder_opts,
"stats", logfilename, AV_DICT_DONT_OVERWRITE);
881 if (video_enc->flags & AV_CODEC_FLAG_PASS2) {
882 char *logbuffer =
file_read(logfilename);
885 av_log(ost, AV_LOG_FATAL,
"Error reading log file '%s' for pass-2 encoding\n",
889 video_enc->stats_in = logbuffer;
891 if (video_enc->flags & AV_CODEC_FLAG_PASS1) {
894 av_log(ost, AV_LOG_FATAL,
895 "Cannot write log file '%s' for pass-1 encoding: %s\n",
896 logfilename, strerror(errno));
917 av_log(ost, AV_LOG_FATAL,
"One of -r/-fpsmax was specified "
918 "together a non-CFR -vsync/-fps_mode. This is contradictory.\n");
925 }
else if (!strcmp(oc->oformat->name,
"avi")) {
928 ost->
vsync_method = (oc->oformat->flags & AVFMT_VARIABLE_FPS) ?
929 ((oc->oformat->flags & AVFMT_NOTIMESTAMPS) ?
962 AVFormatContext *oc = mux->
fc;
974 AVCodecContext *audio_enc = ost->
enc_ctx;
977 char *sample_fmt = NULL;
981 audio_enc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
982 audio_enc->ch_layout.nb_channels = channels;
987 if (av_channel_layout_from_string(&audio_enc->ch_layout, layout) < 0) {
988#if FF_API_OLD_CHANNEL_LAYOUT
990 AV_NOWARN_DEPRECATED({
991 mask = av_get_channel_layout(layout);
995 av_log(ost, AV_LOG_FATAL,
"Unknown channel layout: %s\n", layout);
997#if FF_API_OLD_CHANNEL_LAYOUT
999 av_log(ost, AV_LOG_WARNING,
"Channel layout '%s' uses a deprecated syntax.\n",
1001 av_channel_layout_from_mask(&audio_enc->ch_layout, mask);
1008 (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
1009 av_log(ost, AV_LOG_FATAL,
"Invalid sample format '%s'\n", sample_fmt);
1022#if FFMPEG_OPT_MAP_CHANNEL
1032 }
else if (!ost->
ist) {
1033 av_log(ost, AV_LOG_FATAL,
"Cannot determine input stream for channel mapping %d.%d\n",
1064 av_log(ost, AV_LOG_FATAL,
"Data stream encoding not supported yet (only streamcopy)\n");
1077 av_log(ost, AV_LOG_FATAL,
"Unknown stream encoding not supported yet (only streamcopy)\n");
1100 AVCodecContext *subtitle_enc = ost->
enc_ctx;
1101 char *frame_size = NULL;
1104 if (frame_size && av_parse_video_size(&subtitle_enc->width, &subtitle_enc->height, frame_size) < 0) {
1105 av_log(ost, AV_LOG_FATAL,
"Invalid frame size: %s.\n", frame_size);
1118 switch (ofilter->
type) {
1122 av_log(mux, AV_LOG_FATAL,
"Only video and audio filters are supported "
1133 av_log(ost, AV_LOG_ERROR,
"Streamcopy requested for output stream fed "
1134 "from a complex filtergraph. Filtering and streamcopy "
1135 "cannot be used together.\n");
1140 const char *opt = ost->
filters ?
"-vf/-af/-filter" :
"-filter_script";
1141 av_log(ost, AV_LOG_ERROR,
1142 "%s '%s' was specified through the %s option "
1143 "for output stream %d:%d, which is fed from a complex filtergraph.\n"
1144 "%s and -filter_complex cannot be used together for the same stream.\n",
1145 ost->
filters ?
"Filtergraph" :
"Filtergraph script",
1151 avfilter_inout_free(&ofilter->
out_tmp);
1156 AVFormatContext *oc = mux->
fc;
1162 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_VIDEO) == AV_CODEC_ID_NONE)
1165 qcr = avformat_query_codec(oc->oformat, oc->oformat->video_codec, 0);
1169 int file_best_score = 0;
1170 for (
int i = 0; i < ifile->
nb_streams; i++) {
1175 ist->
st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
1178 score = ist->
st->codecpar->width * ist->
st->codecpar->height
1179 + 100000000 * !!(ist->
st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1180 + 5000000*!!(ist->
st->disposition & AV_DISPOSITION_DEFAULT);
1181 if((qcr!=MKTAG(
'A',
'P',
'I',
'C')) && (ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1184 if (score > file_best_score) {
1185 if((qcr==MKTAG(
'A',
'P',
'I',
'C')) && !(ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1187 file_best_score = score;
1188 file_best_ist = ist;
1191 if (file_best_ist) {
1192 if((qcr == MKTAG(
'A',
'P',
'I',
'C')) ||
1193 !(file_best_ist->
st->disposition & AV_DISPOSITION_ATTACHED_PIC))
1194 file_best_score -= 5000000*!!(file_best_ist->
st->disposition & AV_DISPOSITION_DEFAULT);
1195 if (file_best_score > best_score) {
1196 best_score = file_best_score;
1197 best_ist = file_best_ist;
1207 AVFormatContext *oc = mux->
fc;
1212 if (av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_AUDIO) == AV_CODEC_ID_NONE)
1218 int file_best_score = 0;
1219 for (
int i = 0; i < ifile->
nb_streams; i++) {
1224 ist->
st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
1227 score = ist->
st->codecpar->ch_layout.nb_channels
1228 + 100000000 * !!(ist->
st->event_flags & AVSTREAM_EVENT_FLAG_NEW_PACKETS)
1229 + 5000000*!!(ist->
st->disposition & AV_DISPOSITION_DEFAULT);
1230 if (score > file_best_score) {
1231 file_best_score = score;
1232 file_best_ist = ist;
1235 if (file_best_ist) {
1236 file_best_score -= 5000000*!!(file_best_ist->
st->disposition & AV_DISPOSITION_DEFAULT);
1237 if (file_best_score > best_score) {
1238 best_score = file_best_score;
1239 best_ist = file_best_ist;
1249 AVFormatContext *oc = mux->
fc;
1250 char *subtitle_codec_name = NULL;
1254 if (!avcodec_find_encoder(oc->oformat->subtitle_codec) && !subtitle_codec_name)
1258 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1259 AVCodecDescriptor
const *input_descriptor =
1260 avcodec_descriptor_get(ist->st->codecpar->codec_id);
1261 AVCodecDescriptor
const *output_descriptor = NULL;
1262 AVCodec
const *output_codec =
1263 avcodec_find_encoder(oc->oformat->subtitle_codec);
1264 int input_props = 0, output_props = 0;
1265 if (ist->user_set_discard == AVDISCARD_ALL)
1268 output_descriptor = avcodec_descriptor_get(output_codec->id);
1269 if (input_descriptor)
1270 input_props = input_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1271 if (output_descriptor)
1272 output_props = output_descriptor->props & (AV_CODEC_PROP_TEXT_SUB | AV_CODEC_PROP_BITMAP_SUB);
1273 if (subtitle_codec_name ||
1274 input_props & output_props ||
1276 (input_descriptor && output_descriptor &&
1277 (!input_descriptor->props ||
1278 !output_descriptor->props))) {
1287 AVFormatContext *oc = mux->
fc;
1289 enum AVCodecID codec_id = av_guess_codec(oc->oformat, NULL, oc->url, NULL, AVMEDIA_TYPE_DATA);
1291 if (codec_id == AV_CODEC_ID_NONE)
1295 if (ist->user_set_discard == AVDISCARD_ALL)
1297 if (ist->st->codecpar->codec_type == AVMEDIA_TYPE_DATA &&
1298 ist->st->codecpar->codec_id == codec_id )
1319 if (out && !strcmp(out->name, map->
linklabel)) {
1327 av_log(mux, AV_LOG_FATAL,
"Output with label '%s' does not exist "
1328 "in any defined filter graph, or was already used elsewhere.\n", map->
linklabel);
1335 av_log(mux, AV_LOG_FATAL,
"Stream #%d:%d is disabled and cannot be mapped.\n",
1341 if(o-> audio_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
1343 if(o-> video_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1345 if(o-> data_disable && ist->
st->codecpar->codec_type == AVMEDIA_TYPE_DATA)
1348 switch (ist->
st->codecpar->codec_type) {
1354 case AVMEDIA_TYPE_UNKNOWN:
1361 "Cannot map stream #%d:%d - unsupported type.\n",
1364 av_log(mux, AV_LOG_FATAL,
1365 "If you want unsupported types ignored instead "
1366 "of failing, please use the -ignore_unknown option\n"
1367 "If you want them copied, please use -copy_unknown\n");
1381 uint8_t *attachment;
1385 if ((err = avio_open2(&pb, o->
attachments[i], AVIO_FLAG_READ, &
int_cb, NULL)) < 0) {
1386 av_log(mux, AV_LOG_FATAL,
"Could not open attachment file %s.\n",
1390 if ((len = avio_size(pb)) <= 0) {
1391 av_log(mux, AV_LOG_FATAL,
"Could not get size of the attachment %s.\n",
1395 if (len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
1396 !(attachment = av_malloc(len + AV_INPUT_BUFFER_PADDING_SIZE))) {
1397 av_log(mux, AV_LOG_FATAL,
"Attachment %s too large.\n",
1401 avio_read(pb, attachment, len);
1402 memset(attachment + len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1406 ost->
st->codecpar->extradata = attachment;
1407 ost->
st->codecpar->extradata_size = len;
1410 av_dict_set(&ost->
st->metadata,
"filename", (p && *p) ? p + 1 : o->
attachments[i], AV_DICT_DONT_OVERWRITE);
1417 AVFormatContext *oc = mux->
fc;
1432 switch (ofilter->
type) {
1433 case AVMEDIA_TYPE_VIDEO: auto_disable_v = 1;
break;
1434 case AVMEDIA_TYPE_AUDIO: auto_disable_a = 1;
break;
1435 case AVMEDIA_TYPE_SUBTITLE: auto_disable_s = 1;
break;
1443 if (!auto_disable_v)
1445 if (!auto_disable_a)
1447 if (!auto_disable_s)
1449 if (!auto_disable_d)
1458 if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
1460 av_log(mux, AV_LOG_ERROR,
"Output file does not contain any stream\n");
1468 int nb_av_enc = 0, nb_interleaved = 0;
1469 int limit_frames = 0, limit_frames_av_enc = 0;
1471#define IS_AV_ENC(ost, type) \
1472 (ost->enc_ctx && (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO))
1473#define IS_INTERLEAVED(type) (type != AVMEDIA_TYPE_ATTACHMENT)
1475 for (
int i = 0; i < oc->nb_streams; i++) {
1478 enum AVMediaType type = ost->
st->codecpar->codec_type;
1490 if (!((nb_interleaved > 1 && of->
shortest) ||
1491 (nb_interleaved > 0 && limit_frames)))
1497 if ((of->
shortest && nb_av_enc > 1) || limit_frames_av_enc) {
1500 return AVERROR(ENOMEM);
1502 for (
int i = 0; i < oc->nb_streams; i++) {
1505 enum AVMediaType type = ost->
st->codecpar->codec_type;
1517 return AVERROR(ENOMEM);
1526 if (nb_interleaved > nb_av_enc) {
1529 return AVERROR(ENOMEM);
1531 mux->
sq_pkt = av_packet_alloc();
1533 return AVERROR(ENOMEM);
1535 for (
int i = 0; i < oc->nb_streams; i++) {
1538 enum AVMediaType type = ost->
st->codecpar->codec_type;
1554#undef IS_INTERLEAVED
1561 AVFormatContext *oc = mux->
fc;
1569 const char *p2 = av_get_token(&p,
":");
1570 const char *to_dealloc = p2;
1577 key = av_get_token(&p2,
"=");
1579 av_freep(&to_dealloc);
1585 if (!strcmp(key,
"program_num"))
1586 progid = strtol(p2, NULL, 0);
1587 av_freep(&to_dealloc);
1591 program = av_new_program(oc, progid);
1597 const char *p2 = av_get_token(&p,
":");
1598 const char *to_dealloc = p2;
1604 key = av_get_token(&p2,
"=");
1606 av_log(mux, AV_LOG_FATAL,
1607 "No '=' character in program string %s.\n",
1615 if (!strcmp(key,
"title")) {
1616 av_dict_set(&program->metadata,
"title", p2, 0);
1617 }
else if (!strcmp(key,
"program_num")) {
1618 }
else if (!strcmp(key,
"st")) {
1619 int st_num = strtol(p2, NULL, 0);
1620 av_program_add_stream_index(oc, progid, st_num);
1622 av_log(mux, AV_LOG_FATAL,
"Unknown program key %s.\n", key);
1625 av_freep(&to_dealloc);
1639 char *type,
int *index,
const char **stream_spec)
1647 if (*(++arg) && *arg !=
':') {
1648 av_log(logctx, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", arg);
1651 *stream_spec = *arg ==
':' ? arg + 1 :
"";
1655 if (*(++arg) ==
':')
1656 *index = strtol(++arg, NULL, 0);
1659 av_log(logctx, AV_LOG_FATAL,
"Invalid metadata type %c.\n", *arg);
1672 const char *stream_spec;
1673 int index = 0, ret = 0;
1677 av_log(of, AV_LOG_FATAL,
"No '=' character in metadata string %s.\n",
1685 for (
int j = 0; j < oc->nb_streams; j++) {
1688#if FFMPEG_ROTATION_METADATA
1691 double theta = av_strtod(val, &tail);
1697 av_log(ost, AV_LOG_WARNING,
1698 "Conversion of a 'rotate' metadata key to a "
1699 "proper display matrix rotation is deprecated. "
1700 "See -display_rotation for setting rotation "
1704 av_dict_set(&oc->streams[j]->metadata, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
1705#if FFMPEG_ROTATION_METADATA
1717 if (index < 0 || index >= oc->nb_chapters) {
1718 av_log(of, AV_LOG_FATAL,
"Invalid chapter index %d in metadata specifier.\n", index);
1721 m = &oc->chapters[index]->metadata;
1724 if (index < 0 || index >= oc->nb_programs) {
1725 av_log(of, AV_LOG_FATAL,
"Invalid program index %d in metadata specifier.\n", index);
1728 m = &oc->programs[index]->metadata;
1731 av_log(of, AV_LOG_FATAL,
"Invalid metadata specifier %s.\n", o->
metadata[i].
specifier);
1734 av_dict_set(m, o->
metadata[i].
u.
str, *val ? val : NULL, 0);
1741 const AVCodec *c = ost->
enc_ctx->codec;
1744 if (ost->
enc_ctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
1753 if (!c->ch_layouts) {
1756 av_channel_layout_default(&f->
ch_layout, ost->
enc_ctx->ch_layout.nb_channels);
1761 for (i = 0; c->ch_layouts[i].nb_channels; i++) {
1762 if (c->ch_layouts[i].nb_channels == ost->
enc_ctx->ch_layout.nb_channels)
1765 if (c->ch_layouts[i].nb_channels) {
1767 err = av_channel_layout_copy(&f->
ch_layout, &c->ch_layouts[i]);
1774 av_channel_layout_default(&f->
ch_layout, ost->
enc_ctx->ch_layout.nb_channels);
1780 AVFormatContext *is = ifile->
ctx;
1784 tmp = av_realloc_f(os->chapters, is->nb_chapters + os->nb_chapters,
sizeof(*os->chapters));
1786 return AVERROR(ENOMEM);
1789 for (i = 0; i < is->nb_chapters; i++) {
1790 AVChapter *in_ch = is->chapters[i], *out_ch;
1792 int64_t ts_off = av_rescale_q(start_time - ifile->
ts_offset,
1793 AV_TIME_BASE_Q, in_ch->time_base);
1795 av_rescale_q(ofile->
recording_time, AV_TIME_BASE_Q, in_ch->time_base);
1798 if (in_ch->end < ts_off)
1800 if (rt != INT64_MAX && in_ch->start > rt + ts_off)
1803 out_ch = av_mallocz(
sizeof(AVChapter));
1805 return AVERROR(ENOMEM);
1807 out_ch->id = in_ch->id;
1808 out_ch->time_base = in_ch->time_base;
1809 out_ch->start = FFMAX(0, in_ch->start - ts_off);
1810 out_ch->end = FFMIN(rt, in_ch->end - ts_off);
1813 av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
1815 os->chapters[os->nb_chapters++] = out_ch;
1821 const char *outspec,
const char *inspec,
1822 int *metadata_global_manual,
int *metadata_streams_manual,
1825 AVFormatContext *oc = mux->
fc;
1826 AVDictionary **meta_in = NULL;
1827 AVDictionary **meta_out = NULL;
1829 char type_in, type_out;
1830 const char *istream_spec = NULL, *ostream_spec = NULL;
1831 int idx_in = 0, idx_out = 0;
1836 if (type_in ==
'g' || type_out ==
'g')
1837 *metadata_global_manual = 1;
1838 if (type_in ==
's' || type_out ==
's')
1839 *metadata_streams_manual = 1;
1840 if (type_in ==
'c' || type_out ==
'c')
1841 *metadata_chapters_manual = 1;
1847#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
1848 if ((index) < 0 || (index) >= (nb_elems)) {\
1849 av_log(mux, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
1854#define SET_DICT(type, meta, context, index)\
1857 meta = &context->metadata;\
1860 METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
1861 meta = &context->chapters[index]->metadata;\
1864 METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
1865 meta = &context->programs[index]->metadata;\
1869 default: av_assert0(0);\
1872 SET_DICT(type_in, meta_in, ic, idx_in);
1873 SET_DICT(type_out, meta_out, oc, idx_out);
1876 if (type_in ==
's') {
1877 for (i = 0; i < ic->nb_streams; i++) {
1879 meta_in = &ic->streams[i]->metadata;
1885 av_log(mux, AV_LOG_FATAL,
"Stream specifier %s does not match any streams.\n", istream_spec);
1890 if (type_out ==
's') {
1891 for (i = 0; i < oc->nb_streams; i++) {
1893 meta_out = &oc->streams[i]->metadata;
1894 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
1899 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
1907 AVFormatContext *oc = mux->
fc;
1909 int metadata_global_manual = 0;
1910 int metadata_streams_manual = 0;
1911 int metadata_chapters_manual = 0;
1919 av_log(mux, AV_LOG_FATAL,
"Invalid input file index %d while "
1920 "processing metadata maps\n", in_file_index);
1924 in_file_index >= 0 ?
input_files[in_file_index]->ctx : NULL,
1926 &metadata_global_manual, &metadata_streams_manual,
1927 &metadata_chapters_manual, o);
1932 if (chapters_input_file == INT_MAX) {
1934 chapters_input_file = -1;
1937 chapters_input_file = i;
1941 av_log(mux, AV_LOG_FATAL,
"Invalid input file index %d in chapter mapping.\n",
1942 chapters_input_file);
1946 if (chapters_input_file >= 0)
1948 !metadata_chapters_manual);
1953 AV_DICT_DONT_OVERWRITE);
1955 av_dict_set(&oc->metadata,
"duration", NULL, 0);
1956 av_dict_set(&oc->metadata,
"creation_time", NULL, 0);
1957 av_dict_set(&oc->metadata,
"company_name", NULL, 0);
1958 av_dict_set(&oc->metadata,
"product_name", NULL, 0);
1959 av_dict_set(&oc->metadata,
"product_version", NULL, 0);
1961 if (!metadata_streams_manual)
1967 av_dict_copy(&ost->
st->metadata, ost->
ist->
st->metadata, AV_DICT_DONT_OVERWRITE);
1969 av_dict_set(&ost->
st->metadata,
"encoder", NULL, 0);
1977 AVFormatContext *ctx = mux->
fc;
1980 int have_default[AVMEDIA_TYPE_NB] = { 0 };
1981 int have_manual = 0;
1984 const char **dispositions;
1986 dispositions = av_calloc(ctx->nb_streams,
sizeof(*dispositions));
1988 return AVERROR(ENOMEM);
1991 for (
int i = 0; i < ctx->nb_streams; i++) {
1998 have_manual |= !!dispositions[i];
2001 ost->
st->disposition = ost->
ist->
st->disposition;
2003 if (ost->
st->disposition & AV_DISPOSITION_DEFAULT)
2004 have_default[ost->
st->codecpar->codec_type] = 1;
2010 for (
int i = 0; i < ctx->nb_streams; i++) {
2012 const char *disp = dispositions[i];
2017 ret = av_opt_set(ost->
st,
"disposition", disp, 0);
2025 for (
int i = 0; i < ctx->nb_streams; i++) {
2027 enum AVMediaType type = ost->
st->codecpar->codec_type;
2029 if (
nb_streams[type] < 2 || have_default[type] ||
2030 ost->
st->disposition & AV_DISPOSITION_ATTACHED_PIC)
2033 ost->
st->disposition |= AV_DISPOSITION_DEFAULT;
2034 have_default[type] = 1;
2039 av_freep(&dispositions);
2055 return FFDIFFSIGN(*(
const int64_t *)a, *(
const int64_t *)b);
2062 int n = 1, i, size, index = 0;
2065 for (p = spec; *p; p++)
2069 pts = av_malloc_array(size,
sizeof(*pts));
2074 for (i = 0; i < n; i++) {
2075 char *next = strchr(p,
',');
2080 if (!memcmp(p,
"chapters", 8)) {
2081 AVChapter *
const *ch = mux->
fc->chapters;
2082 unsigned int nb_ch = mux->
fc->nb_chapters;
2085 if (nb_ch > INT_MAX - size ||
2086 !(pts = av_realloc_f(pts, size += nb_ch - 1,
2091 for (j = 0; j < nb_ch; j++) {
2092 const AVChapter *c = ch[j];
2093 av_assert1(index < size);
2094 pts[index++] = av_rescale_q(c->start, c->time_base,
2095 AV_TIME_BASE_Q) + t;
2099 av_assert1(index < size);
2106 av_assert0(index == size);
2116 const char *forced_keyframes = NULL;
2120 if (!(ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
2121 ost->
enc_ctx && forced_keyframes))
2124 if (!strncmp(forced_keyframes,
"expr:", 5)) {
2125 int ret = av_expr_parse(&ost->
kf.
pexpr, forced_keyframes + 5,
2128 av_log(ost, AV_LOG_ERROR,
2129 "Invalid force_key_frames expression '%s'\n", forced_keyframes + 5);
2139 }
else if (!strcmp(forced_keyframes,
"source")) {
2141 }
else if (!strcmp(forced_keyframes,
"source_no_drop")) {
2153 const AVClass *
class = avcodec_get_class();
2154 const AVClass *fclass = avformat_get_class();
2157 AVDictionary *unused_opts;
2158 const AVDictionaryEntry *e;
2164 av_dict_set(&unused_opts, e->key, NULL, 0);
2168 while ((e = av_dict_iterate(unused_opts, e))) {
2169 const AVOption *option = av_opt_find(&
class, e->key, NULL, 0,
2170 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2171 const AVOption *foption = av_opt_find(&fclass, e->key, NULL, 0,
2172 AV_OPT_SEARCH_CHILDREN | AV_OPT_SEARCH_FAKE_OBJ);
2173 if (!option || foption)
2176 if (!(option->flags & AV_OPT_FLAG_ENCODING_PARAM)) {
2177 av_log(mux, AV_LOG_ERROR,
"Codec AVOption %s (%s) is not an "
2178 "encoding option.\n", e->key, option->help ? option->help :
"");
2183 if (!strcmp(e->key,
"gop_timecode"))
2186 av_log(mux, AV_LOG_WARNING,
"Codec AVOption %s (%s) has not been used "
2187 "for any stream. The most likely reason is either wrong type "
2188 "(e.g. a video option with no video streams) or that it is a "
2189 "private option of some encoder which was not actually used for "
2190 "any stream.\n", e->key, option->help ? option->help :
"");
2192 av_dict_free(&unused_opts);
2197 const Muxer *mux = obj;
2203 .class_name =
"OutputFile",
2204 .version = LIBAVUTIL_VERSION_INT,
2206 .category = AV_CLASS_CATEGORY_MUXER,
2224 AVFormatContext *oc;
2234 if (stop_time != INT64_MAX && recording_time != INT64_MAX) {
2235 stop_time = INT64_MAX;
2236 av_log(mux, AV_LOG_WARNING,
"-t and -to cannot be used together; using -t.\n");
2239 if (stop_time != INT64_MAX && recording_time == INT64_MAX) {
2241 if (stop_time <= start_time) {
2242 av_log(mux, AV_LOG_ERROR,
"-to value smaller than -ss; aborting.\n");
2245 recording_time = stop_time - start_time;
2257 if (!strcmp(filename,
"-"))
2260 err = avformat_alloc_output_context2(&oc, NULL, o->
format, filename);
2270 if (strcmp(oc->oformat->name,
"rtp"))
2273 of->
format = oc->oformat;
2274 if (recording_time != INT64_MAX)
2275 oc->duration = recording_time;
2277 oc->interrupt_callback =
int_cb;
2280 oc->flags |= AVFMT_FLAG_BITEXACT;
2284 AVFMT_FLAG_BITEXACT);
2302 if (ost->
st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
2303 ost->
st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
2306 av_log(ost, AV_LOG_ERROR,
2307 "Error initializing a simple filtergraph\n");
2311 }
else if (ost->
ist) {
2317 const AVCodec *c = ost->
enc_ctx->codec;
2319 switch (ost->
enc_ctx->codec_type) {
2320 case AVMEDIA_TYPE_VIDEO:
2324 if (ost->
enc_ctx->pix_fmt != AV_PIX_FMT_NONE) {
2330 case AVMEDIA_TYPE_AUDIO:
2331 if (ost->
enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) {
2336 if (ost->
enc_ctx->sample_rate) {
2341 if (ost->
enc_ctx->ch_layout.nb_channels) {
2343 }
else if (c->ch_layouts) {
2352 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2353 if (!av_filename_number_test(oc->url)) {
2359 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2364 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
2365 &oc->interrupt_callback,
2370 }
else if (strcmp(oc->oformat->name,
"image2")==0 && !av_filename_number_test(filename))
2374 av_dict_set_int(&mux->
opts,
"preload", o->
mux_preload*AV_TIME_BASE, 0);
2386 av_log(mux, AV_LOG_FATAL,
"Error setting output stream dispositions\n");
2394 av_log(mux, AV_LOG_FATAL,
"Error processing forced keyframes\n");
2400 av_log(mux, AV_LOG_FATAL,
"Error setting up output sync queues\n");
2407 if (of->
format->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
EncStatsComponent * components
double expr_const_values[FKF_NB]
int max_muxing_queue_size
size_t muxing_queue_data_threshold
AVDictionary * codec_opts
AVDictionary * format_opts
SpecifierOpt * metadata_map
int nb_audio_channel_maps
SpecifierOpt * max_frames
float shortest_buf_duration
const char ** attachments
AudioChannelMap * audio_channel_maps
const AVOutputFormat * format
int64_t start_time
start time in microseconds == AV_TIME_BASE units
int64_t recording_time
desired length of the resulting file in microseconds == AV_TIME_BASE units
struct OutputStream * ost
const AVChannelLayout * ch_layouts
AVChannelLayout ch_layout
unsigned int fix_sub_duration_heartbeat
int copy_initial_nonkeyframes
AVRational frame_aspect_ratio
double rotate_override_value
int audio_channels_mapped
enum VideoSyncMethod vsync_method
AVRational max_frame_rate
const char * attachment_filename
AVDictionary * encoder_opts
char * filters
filtergraph associated to the -filter option
char * filters_script
filtergraph script associated to the -filter_script option