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
73 printf(
"pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
102 fprintf(stderr,
"Could not find encoder for '%s'\n",
109 fprintf(stderr,
"Could not allocate stream\n");
115 switch ((*codec)->type) {
121 if ((*codec)->supported_samplerates) {
122 c->
sample_rate = (*codec)->supported_samplerates[0];
123 for (i = 0; (*codec)->supported_samplerates[i]; i++) {
124 if ((*codec)->supported_samplerates[i] == 44100)
130 if ((*codec)->channel_layouts) {
132 for (i = 0; (*codec)->channel_layouts[i]; i++) {
182 uint64_t channel_layout,
189 fprintf(stderr,
"Error allocating an audio frame\n");
193 frame->
format = sample_fmt;
201 fprintf(stderr,
"Error allocating an audio buffer\n");
223 fprintf(stderr,
"Could not open audio codec: %s\n",
av_err2str(ret));
246 fprintf(stderr,
"Could not allocate resampler context\n");
260 fprintf(stderr,
"Failed to initialize the resampling context\n");
271 int16_t *q = (int16_t*)frame->
data[0];
279 v = (int)(sin(ost->t) * 10000);
280 for (i = 0; i < ost->st->codec->channels; i++)
282 ost->t += ost->tincr;
283 ost->tincr += ost->tincr2;
330 fprintf(stderr,
"Error while converting\n");
341 fprintf(stderr,
"Error encoding audio frame: %s\n",
av_err2str(
ret));
348 fprintf(stderr,
"Error while writing audio frame: %s\n",
354 return (
frame || got_packet) ? 0 : 1;
376 fprintf(stderr,
"Could not allocate frame data.\n");
395 fprintf(stderr,
"Could not open video codec: %s\n",
av_err2str(ret));
402 fprintf(stderr,
"Could not allocate video frame\n");
413 fprintf(stderr,
"Could not allocate temporary picture\n");
436 for (y = 0; y <
height; y++)
437 for (x = 0; x <
width; x++)
438 pict->
data[0][y * pict->
linesize[0] + x] = x + y + i * 3;
441 for (y = 0; y < height / 2; y++) {
442 for (x = 0; x < width / 2; x++) {
443 pict->
data[1][y * pict->
linesize[1] + x] = 128 + y + i * 2;
444 pict->
data[2][y * pict->
linesize[2] + x] = 64 + x + i * 5;
469 "Could not initialize the conversion context\n");
475 (
const uint8_t *
const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
476 0,
c->height, ost->frame->data, ost->frame->linesize);
481 ost->frame->pts = ost->next_pts++;
525 fprintf(stderr,
"Error encoding video frame: %s\n",
av_err2str(ret));
537 fprintf(stderr,
"Error while writing video frame: %s\n",
av_err2str(ret));
541 return (frame || got_packet) ? 0 : 1;
556 int main(
int argc,
char **argv)
559 const char *filename;
562 AVCodec *audio_codec, *video_codec;
564 int have_video = 0, have_audio = 0;
565 int encode_video = 0, encode_audio = 0;
572 printf(
"usage: %s output_file\n"
573 "API example program to output a media file with libavformat.\n"
574 "This program generates a synthetic audio and video stream, encodes and\n"
575 "muxes them into a file named output_file.\n"
576 "The output format is automatically guessed according to the file extension.\n"
577 "Raw images can also be output by using '%%d' in the filename.\n"
583 if (argc > 3 && !strcmp(argv[2],
"-flags")) {
590 printf(
"Could not deduce output format from file extension: using MPEG.\n");
625 fprintf(stderr,
"Could not open '%s': %s\n", filename,
634 fprintf(stderr,
"Error occurred when opening output file: %s\n",
639 while (encode_video || encode_audio) {
643 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {