46 #define STREAM_DURATION 10.0
47 #define STREAM_FRAME_RATE 25
48 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
56 printf(
"pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
86 fprintf(stderr,
"Could not find encoder for '%s'\n",
93 fprintf(stderr,
"Could not allocate stream\n");
99 switch ((*codec)->type) {
174 fprintf(stderr,
"Could not allocate audio frame\n");
181 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
197 fprintf(stderr,
"Could not allocate source samples\n");
210 fprintf(stderr,
"Could not allocate resampler context\n");
223 if ((ret =
swr_init(swr_ctx)) < 0) {
224 fprintf(stderr,
"Failed to initialize the resampling context\n");
231 fprintf(stderr,
"Could not allocate destination samples\n");
250 v = (int)(sin(
t) * 10000);
262 int got_packet,
ret, dst_nb_samples;
291 fprintf(stderr,
"Error while converting\n");
307 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(ret));
319 fprintf(stderr,
"Error while writing audio frame: %s\n",
352 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
359 fprintf(stderr,
"Could not allocate video frame\n");
369 fprintf(stderr,
"Could not allocate picture: %s\n",
av_err2str(ret));
379 fprintf(stderr,
"Could not allocate temporary picture: %s\n",
398 for (y = 0; y <
height; y++)
399 for (x = 0; x <
width; x++)
400 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
403 for (y = 0; y < height / 2; y++) {
404 for (x = 0; x < width / 2; x++) {
405 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
406 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
427 "Could not initialize the conversion context\n");
460 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
475 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
492 int main(
int argc,
char **argv)
494 const char *filename;
498 AVCodec *audio_codec, *video_codec;
499 double audio_time, video_time;
506 printf(
"usage: %s output_file\n"
507 "API example program to output a media file with libavformat.\n"
508 "This program generates a synthetic audio and video stream, encodes and\n"
509 "muxes them into a file named output_file.\n"
510 "The output format is automatically guessed according to the file extension.\n"
511 "Raw images can also be output by using '%%d' in the filename.\n"
521 printf(
"Could not deduce output format from file extension: using MPEG.\n");
552 fprintf(stderr,
"Could not open '%s': %s\n", filename,
561 fprintf(stderr,
"Error occurred when opening output file: %s\n",
579 if (audio_st && !
audio_is_eof && audio_time <= video_time) {
581 }
else if (video_st && !
video_is_eof && video_time < audio_time) {