46 #define STREAM_DURATION 10.0
47 #define STREAM_FRAME_RATE 25
48 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P
50 #define SCALE_FLAGS SWS_BICUBIC
71 printf(
"pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
99 fprintf(stderr,
"Could not find encoder for '%s'\n",
106 fprintf(stderr,
"Could not allocate stream\n");
112 switch ((*codec)->type) {
175 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
211 fprintf(stderr,
"Could not allocate resampler context\n");
224 if ((ret =
swr_init(swr_ctx)) < 0) {
225 fprintf(stderr,
"Failed to initialize the resampling context\n");
232 fprintf(stderr,
"Could not allocate an audio frame.\n");
237 fprintf(stderr,
"Could not allocate an audio frame.\n");
247 int16_t *q = (int16_t*)ost->
frame->
data[0];
262 for (j = 0; j < ost->frame->nb_samples; j++) {
263 v = (int)(sin(ost->t) * 10000);
264 for (i = 0; i < ost->st->codec->channels; i++)
266 ost->t += ost->tincr;
267 ost->tincr += ost->tincr2;
270 ost->frame->pts = ost->next_pts;
271 ost->next_pts += ost->frame->nb_samples;
307 fprintf(stderr,
"Error while converting\n");
321 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(ret));
328 fprintf(stderr,
"Error while writing audio frame: %s\n",
334 return (frame || got_packet) ? 0 : 1;
356 fprintf(stderr,
"Could not allocate frame data.\n");
371 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
378 fprintf(stderr,
"Could not allocate video frame\n");
389 fprintf(stderr,
"Could not allocate temporary picture\n");
412 for (y = 0; y <
height; y++)
413 for (x = 0; x <
width; x++)
414 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
417 for (y = 0; y < height / 2; y++) {
418 for (x = 0; x < width / 2; x++) {
419 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
420 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
445 "Could not initialize the conversion context\n");
451 (
const uint8_t *
const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
452 0,
c->height, ost->frame->data, ost->frame->linesize);
457 ost->frame->pts = ost->next_pts++;
501 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
513 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
517 return (frame || got_packet) ? 0 : 1;
531 int main(
int argc,
char **argv)
534 const char *filename;
537 AVCodec *audio_codec, *video_codec;
539 int have_video = 0, have_audio = 0;
540 int encode_video = 0, encode_audio = 0;
546 printf(
"usage: %s output_file\n"
547 "API example program to output a media file with libavformat.\n"
548 "This program generates a synthetic audio and video stream, encodes and\n"
549 "muxes them into a file named output_file.\n"
550 "The output format is automatically guessed according to the file extension.\n"
551 "Raw images can also be output by using '%%d' in the filename.\n"
561 printf(
"Could not deduce output format from file extension: using MPEG.\n");
596 fprintf(stderr,
"Could not open '%s': %s\n", filename,
605 fprintf(stderr,
"Error occurred when opening output file: %s\n",
610 while (encode_video || encode_audio) {
614 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {