Statistics
| Branch: | Revision:

ffmpeg / output_example.c @ 42225a30

History | View | Annotate | Download (15 KB)

1 8de65d4f Fabrice Bellard
/*
2
 * Libavformat API example: Output a media file in any supported
3
 * libavformat format. The default codecs are used.
4 115329f1 Diego Biurrun
 *
5 8de65d4f Fabrice Bellard
 * Copyright (c) 2003 Fabrice Bellard
6 115329f1 Diego Biurrun
 *
7 8de65d4f Fabrice Bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13 115329f1 Diego Biurrun
 *
14 8de65d4f Fabrice Bellard
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16 115329f1 Diego Biurrun
 *
17 8de65d4f Fabrice Bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 115329f1 Diego Biurrun
 * THE SOFTWARE.
24 8de65d4f Fabrice Bellard
 */
25
#include <stdlib.h>
26
#include <stdio.h>
27 af547ce5 Michael Niedermayer
#include <string.h>
28 8de65d4f Fabrice Bellard
#include <math.h>
29
30 264cb2b8 Michael Niedermayer
#ifndef M_PI
31
#define M_PI 3.1415926535897931
32
#endif
33
34 8de65d4f Fabrice Bellard
#include "avformat.h"
35
36
/* 5 seconds stream duration */
37 cb0c448a Fabrice Bellard
#define STREAM_DURATION   5.0
38
#define STREAM_FRAME_RATE 25 /* 25 images/s */
39
#define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
40 002c01a6 Andreas Öman
#define STREAM_PIX_FMT PIX_FMT_YUV420P /* default pix_fmt */
41 8de65d4f Fabrice Bellard
42
/**************************************************************/
43
/* audio output */
44
45 e70fcf07 Fabrice Bellard
float t, tincr, tincr2;
46 8de65d4f Fabrice Bellard
int16_t *samples;
47
uint8_t *audio_outbuf;
48
int audio_outbuf_size;
49
int audio_input_frame_size;
50
51 115329f1 Diego Biurrun
/*
52 8de65d4f Fabrice Bellard
 * add an audio output stream
53
 */
54 ceaf1909 Dieter
static AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
55 8de65d4f Fabrice Bellard
{
56
    AVCodecContext *c;
57
    AVStream *st;
58
59
    st = av_new_stream(oc, 1);
60
    if (!st) {
61
        fprintf(stderr, "Could not alloc stream\n");
62
        exit(1);
63
    }
64
65 01f4895c Michael Niedermayer
    c = st->codec;
66 e70fcf07 Fabrice Bellard
    c->codec_id = codec_id;
67 8de65d4f Fabrice Bellard
    c->codec_type = CODEC_TYPE_AUDIO;
68
69
    /* put sample parameters */
70
    c->bit_rate = 64000;
71
    c->sample_rate = 44100;
72
    c->channels = 2;
73 e70fcf07 Fabrice Bellard
    return st;
74
}
75
76 ceaf1909 Dieter
static void open_audio(AVFormatContext *oc, AVStream *st)
77 e70fcf07 Fabrice Bellard
{
78
    AVCodecContext *c;
79
    AVCodec *codec;
80
81 01f4895c Michael Niedermayer
    c = st->codec;
82 e70fcf07 Fabrice Bellard
83
    /* find the audio encoder */
84
    codec = avcodec_find_encoder(c->codec_id);
85
    if (!codec) {
86
        fprintf(stderr, "codec not found\n");
87
        exit(1);
88
    }
89 8de65d4f Fabrice Bellard
90
    /* open it */
91
    if (avcodec_open(c, codec) < 0) {
92
        fprintf(stderr, "could not open codec\n");
93
        exit(1);
94
    }
95
96
    /* init signal generator */
97
    t = 0;
98 e70fcf07 Fabrice Bellard
    tincr = 2 * M_PI * 110.0 / c->sample_rate;
99
    /* increment frequency by 110 Hz per second */
100
    tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
101 8de65d4f Fabrice Bellard
102
    audio_outbuf_size = 10000;
103
    audio_outbuf = malloc(audio_outbuf_size);
104
105
    /* ugly hack for PCM codecs (will be removed ASAP with new PCM
106
       support to compute the input frame size in samples */
107
    if (c->frame_size <= 1) {
108
        audio_input_frame_size = audio_outbuf_size / c->channels;
109 01f4895c Michael Niedermayer
        switch(st->codec->codec_id) {
110 8de65d4f Fabrice Bellard
        case CODEC_ID_PCM_S16LE:
111
        case CODEC_ID_PCM_S16BE:
112
        case CODEC_ID_PCM_U16LE:
113
        case CODEC_ID_PCM_U16BE:
114
            audio_input_frame_size >>= 1;
115
            break;
116
        default:
117
            break;
118
        }
119
    } else {
120
        audio_input_frame_size = c->frame_size;
121
    }
122
    samples = malloc(audio_input_frame_size * 2 * c->channels);
123
}
124
125 cb750e33 Fabrice Bellard
/* prepare a 16 bit dummy audio frame of 'frame_size' samples and
126
   'nb_channels' channels */
127 ceaf1909 Dieter
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
128 cb750e33 Fabrice Bellard
{
129
    int j, i, v;
130
    int16_t *q;
131
132
    q = samples;
133
    for(j=0;j<frame_size;j++) {
134
        v = (int)(sin(t) * 10000);
135
        for(i = 0; i < nb_channels; i++)
136
            *q++ = v;
137
        t += tincr;
138
        tincr += tincr2;
139
    }
140
}
141
142 ceaf1909 Dieter
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
143 8de65d4f Fabrice Bellard
{
144
    AVCodecContext *c;
145 e928649b Michael Niedermayer
    AVPacket pkt;
146
    av_init_packet(&pkt);
147 115329f1 Diego Biurrun
148 01f4895c Michael Niedermayer
    c = st->codec;
149 8de65d4f Fabrice Bellard
150 cb750e33 Fabrice Bellard
    get_audio_frame(samples, audio_input_frame_size, c->channels);
151
152 e928649b Michael Niedermayer
    pkt.size= avcodec_encode_audio(c, audio_outbuf, audio_outbuf_size, samples);
153
154 002c01a6 Andreas Öman
    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
155 e928649b Michael Niedermayer
    pkt.flags |= PKT_FLAG_KEY;
156
    pkt.stream_index= st->index;
157
    pkt.data= audio_outbuf;
158 8de65d4f Fabrice Bellard
159
    /* write the compressed frame in the media file */
160 e928649b Michael Niedermayer
    if (av_write_frame(oc, &pkt) != 0) {
161 8de65d4f Fabrice Bellard
        fprintf(stderr, "Error while writing audio frame\n");
162
        exit(1);
163
    }
164
}
165
166 ceaf1909 Dieter
static void close_audio(AVFormatContext *oc, AVStream *st)
167 e70fcf07 Fabrice Bellard
{
168 01f4895c Michael Niedermayer
    avcodec_close(st->codec);
169 115329f1 Diego Biurrun
170 e70fcf07 Fabrice Bellard
    av_free(samples);
171
    av_free(audio_outbuf);
172
}
173
174 8de65d4f Fabrice Bellard
/**************************************************************/
175
/* video output */
176
177 e70fcf07 Fabrice Bellard
AVFrame *picture, *tmp_picture;
178 8de65d4f Fabrice Bellard
uint8_t *video_outbuf;
179
int frame_count, video_outbuf_size;
180
181
/* add a video output stream */
182 ceaf1909 Dieter
static AVStream *add_video_stream(AVFormatContext *oc, int codec_id)
183 8de65d4f Fabrice Bellard
{
184
    AVCodecContext *c;
185
    AVStream *st;
186
187
    st = av_new_stream(oc, 0);
188
    if (!st) {
189
        fprintf(stderr, "Could not alloc stream\n");
190
        exit(1);
191
    }
192 115329f1 Diego Biurrun
193 01f4895c Michael Niedermayer
    c = st->codec;
194 e70fcf07 Fabrice Bellard
    c->codec_id = codec_id;
195 8de65d4f Fabrice Bellard
    c->codec_type = CODEC_TYPE_VIDEO;
196
197
    /* put sample parameters */
198
    c->bit_rate = 400000;
199
    /* resolution must be a multiple of two */
200 115329f1 Diego Biurrun
    c->width = 352;
201 8de65d4f Fabrice Bellard
    c->height = 288;
202 5b28c8c3 Michael Niedermayer
    /* time base: this is the fundamental unit of time (in seconds) in terms
203
       of which frame timestamps are represented. for fixed-fps content,
204
       timebase should be 1/framerate and timestamp increments should be
205
       identically 1. */
206 115329f1 Diego Biurrun
    c->time_base.den = STREAM_FRAME_RATE;
207 c0df9d75 Michael Niedermayer
    c->time_base.num = 1;
208 cb0c448a Fabrice Bellard
    c->gop_size = 12; /* emit one intra frame every twelve frames at most */
209 002c01a6 Andreas Öman
    c->pix_fmt = STREAM_PIX_FMT;
210 85e33747 Michael Niedermayer
    if (c->codec_id == CODEC_ID_MPEG2VIDEO) {
211 cb0c448a Fabrice Bellard
        /* just for testing, we also add B frames */
212
        c->max_b_frames = 2;
213
    }
214 85e33747 Michael Niedermayer
    if (c->codec_id == CODEC_ID_MPEG1VIDEO){
215 115329f1 Diego Biurrun
        /* needed to avoid using macroblocks in which some coeffs overflow
216
           this doesnt happen with normal video, it just happens here as the
217 85e33747 Michael Niedermayer
           motion of the chroma plane doesnt match the luma plane */
218
        c->mb_decision=2;
219
    }
220 af547ce5 Michael Niedermayer
    // some formats want stream headers to be seperate
221
    if(!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))
222
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
223 115329f1 Diego Biurrun
224 e70fcf07 Fabrice Bellard
    return st;
225
}
226
227 ceaf1909 Dieter
static AVFrame *alloc_picture(int pix_fmt, int width, int height)
228 e70fcf07 Fabrice Bellard
{
229
    AVFrame *picture;
230
    uint8_t *picture_buf;
231
    int size;
232 115329f1 Diego Biurrun
233 e70fcf07 Fabrice Bellard
    picture = avcodec_alloc_frame();
234
    if (!picture)
235
        return NULL;
236
    size = avpicture_get_size(pix_fmt, width, height);
237
    picture_buf = malloc(size);
238
    if (!picture_buf) {
239
        av_free(picture);
240
        return NULL;
241
    }
242 115329f1 Diego Biurrun
    avpicture_fill((AVPicture *)picture, picture_buf,
243 e70fcf07 Fabrice Bellard
                   pix_fmt, width, height);
244
    return picture;
245
}
246 115329f1 Diego Biurrun
247 ceaf1909 Dieter
static void open_video(AVFormatContext *oc, AVStream *st)
248 e70fcf07 Fabrice Bellard
{
249
    AVCodec *codec;
250
    AVCodecContext *c;
251
252 01f4895c Michael Niedermayer
    c = st->codec;
253 e70fcf07 Fabrice Bellard
254
    /* find the video encoder */
255
    codec = avcodec_find_encoder(c->codec_id);
256
    if (!codec) {
257
        fprintf(stderr, "codec not found\n");
258
        exit(1);
259
    }
260
261
    /* open the codec */
262 8de65d4f Fabrice Bellard
    if (avcodec_open(c, codec) < 0) {
263
        fprintf(stderr, "could not open codec\n");
264
        exit(1);
265
    }
266
267 e70fcf07 Fabrice Bellard
    video_outbuf = NULL;
268
    if (!(oc->oformat->flags & AVFMT_RAWPICTURE)) {
269
        /* allocate output buffer */
270
        /* XXX: API change will be done */
271
        video_outbuf_size = 200000;
272
        video_outbuf = malloc(video_outbuf_size);
273
    }
274 8de65d4f Fabrice Bellard
275 e70fcf07 Fabrice Bellard
    /* allocate the encoded raw picture */
276
    picture = alloc_picture(c->pix_fmt, c->width, c->height);
277
    if (!picture) {
278
        fprintf(stderr, "Could not allocate picture\n");
279
        exit(1);
280
    }
281 8de65d4f Fabrice Bellard
282 e70fcf07 Fabrice Bellard
    /* if the output format is not YUV420P, then a temporary YUV420P
283
       picture is needed too. It is then converted to the required
284
       output format */
285
    tmp_picture = NULL;
286
    if (c->pix_fmt != PIX_FMT_YUV420P) {
287
        tmp_picture = alloc_picture(PIX_FMT_YUV420P, c->width, c->height);
288
        if (!tmp_picture) {
289
            fprintf(stderr, "Could not allocate temporary picture\n");
290
            exit(1);
291
        }
292
    }
293
}
294 8de65d4f Fabrice Bellard
295 e70fcf07 Fabrice Bellard
/* prepare a dummy image */
296 ceaf1909 Dieter
static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)
297 8de65d4f Fabrice Bellard
{
298 e70fcf07 Fabrice Bellard
    int x, y, i;
299
300
    i = frame_index;
301 8de65d4f Fabrice Bellard
302
    /* Y */
303 e70fcf07 Fabrice Bellard
    for(y=0;y<height;y++) {
304
        for(x=0;x<width;x++) {
305
            pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
306 8de65d4f Fabrice Bellard
        }
307
    }
308 115329f1 Diego Biurrun
309 8de65d4f Fabrice Bellard
    /* Cb and Cr */
310 e70fcf07 Fabrice Bellard
    for(y=0;y<height/2;y++) {
311
        for(x=0;x<width/2;x++) {
312
            pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
313
            pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
314 8de65d4f Fabrice Bellard
        }
315
    }
316 e70fcf07 Fabrice Bellard
}
317 8de65d4f Fabrice Bellard
318 ceaf1909 Dieter
static void write_video_frame(AVFormatContext *oc, AVStream *st)
319 e70fcf07 Fabrice Bellard
{
320
    int out_size, ret;
321
    AVCodecContext *c;
322 115329f1 Diego Biurrun
323 01f4895c Michael Niedermayer
    c = st->codec;
324 115329f1 Diego Biurrun
325 cb0c448a Fabrice Bellard
    if (frame_count >= STREAM_NB_FRAMES) {
326
        /* no more frame to compress. The codec has a latency of a few
327
           frames if using B frames, so we get the last frames by
328 002c01a6 Andreas Öman
           passing the same picture again */
329 e70fcf07 Fabrice Bellard
    } else {
330 cb0c448a Fabrice Bellard
        if (c->pix_fmt != PIX_FMT_YUV420P) {
331
            /* as we only generate a YUV420P picture, we must convert it
332
               to the codec pixel format if needed */
333
            fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
334 115329f1 Diego Biurrun
            img_convert((AVPicture *)picture, c->pix_fmt,
335 cb0c448a Fabrice Bellard
                        (AVPicture *)tmp_picture, PIX_FMT_YUV420P,
336
                        c->width, c->height);
337
        } else {
338
            fill_yuv_image(picture, frame_count, c->width, c->height);
339
        }
340 e70fcf07 Fabrice Bellard
    }
341
342 115329f1 Diego Biurrun
343 e70fcf07 Fabrice Bellard
    if (oc->oformat->flags & AVFMT_RAWPICTURE) {
344
        /* raw video case. The API will change slightly in the near
345
           futur for that */
346 e928649b Michael Niedermayer
        AVPacket pkt;
347
        av_init_packet(&pkt);
348 115329f1 Diego Biurrun
349 e928649b Michael Niedermayer
        pkt.flags |= PKT_FLAG_KEY;
350
        pkt.stream_index= st->index;
351 002c01a6 Andreas Öman
        pkt.data= (uint8_t *)picture;
352 e928649b Michael Niedermayer
        pkt.size= sizeof(AVPicture);
353 115329f1 Diego Biurrun
354 e928649b Michael Niedermayer
        ret = av_write_frame(oc, &pkt);
355 e70fcf07 Fabrice Bellard
    } else {
356
        /* encode the image */
357 002c01a6 Andreas Öman
        out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, picture);
358 cb0c448a Fabrice Bellard
        /* if zero size, it means the image was buffered */
359 b5bc8591 Michael Niedermayer
        if (out_size > 0) {
360 e928649b Michael Niedermayer
            AVPacket pkt;
361
            av_init_packet(&pkt);
362 115329f1 Diego Biurrun
363 002c01a6 Andreas Öman
            pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
364 e928649b Michael Niedermayer
            if(c->coded_frame->key_frame)
365
                pkt.flags |= PKT_FLAG_KEY;
366
            pkt.stream_index= st->index;
367
            pkt.data= video_outbuf;
368
            pkt.size= out_size;
369 115329f1 Diego Biurrun
370 cb0c448a Fabrice Bellard
            /* write the compressed frame in the media file */
371 e928649b Michael Niedermayer
            ret = av_write_frame(oc, &pkt);
372 cb0c448a Fabrice Bellard
        } else {
373
            ret = 0;
374
        }
375 e70fcf07 Fabrice Bellard
    }
376
    if (ret != 0) {
377 8de65d4f Fabrice Bellard
        fprintf(stderr, "Error while writing video frame\n");
378
        exit(1);
379
    }
380 e70fcf07 Fabrice Bellard
    frame_count++;
381
}
382
383 ceaf1909 Dieter
static void close_video(AVFormatContext *oc, AVStream *st)
384 e70fcf07 Fabrice Bellard
{
385 01f4895c Michael Niedermayer
    avcodec_close(st->codec);
386 e70fcf07 Fabrice Bellard
    av_free(picture->data[0]);
387
    av_free(picture);
388
    if (tmp_picture) {
389
        av_free(tmp_picture->data[0]);
390
        av_free(tmp_picture);
391
    }
392
    av_free(video_outbuf);
393 8de65d4f Fabrice Bellard
}
394
395
/**************************************************************/
396
/* media file output */
397
398
int main(int argc, char **argv)
399
{
400
    const char *filename;
401
    AVOutputFormat *fmt;
402
    AVFormatContext *oc;
403 e70fcf07 Fabrice Bellard
    AVStream *audio_st, *video_st;
404 8de65d4f Fabrice Bellard
    double audio_pts, video_pts;
405 e70fcf07 Fabrice Bellard
    int i;
406
407 8de65d4f Fabrice Bellard
    /* initialize libavcodec, and register all codecs and formats */
408
    av_register_all();
409 115329f1 Diego Biurrun
410 8de65d4f Fabrice Bellard
    if (argc != 2) {
411
        printf("usage: %s output_file\n"
412 e70fcf07 Fabrice Bellard
               "API example program to output a media file with libavformat.\n"
413
               "The output format is automatically guessed according to the file extension.\n"
414
               "Raw images can also be output by using '%%d' in the filename\n"
415 8de65d4f Fabrice Bellard
               "\n", argv[0]);
416
        exit(1);
417
    }
418 115329f1 Diego Biurrun
419 8de65d4f Fabrice Bellard
    filename = argv[1];
420
421
    /* auto detect the output format from the name. default is
422
       mpeg. */
423
    fmt = guess_format(NULL, filename, NULL);
424
    if (!fmt) {
425
        printf("Could not deduce output format from file extension: using MPEG.\n");
426
        fmt = guess_format("mpeg", NULL, NULL);
427
    }
428
    if (!fmt) {
429
        fprintf(stderr, "Could not find suitable output format\n");
430
        exit(1);
431
    }
432 115329f1 Diego Biurrun
433 8de65d4f Fabrice Bellard
    /* allocate the output media context */
434 bc874dae Michel Bardiaux
    oc = av_alloc_format_context();
435 8de65d4f Fabrice Bellard
    if (!oc) {
436
        fprintf(stderr, "Memory error\n");
437
        exit(1);
438
    }
439
    oc->oformat = fmt;
440 e70fcf07 Fabrice Bellard
    snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
441 8de65d4f Fabrice Bellard
442
    /* add the audio and video streams using the default format codecs
443
       and initialize the codecs */
444
    video_st = NULL;
445
    audio_st = NULL;
446
    if (fmt->video_codec != CODEC_ID_NONE) {
447
        video_st = add_video_stream(oc, fmt->video_codec);
448
    }
449
    if (fmt->audio_codec != CODEC_ID_NONE) {
450
        audio_st = add_audio_stream(oc, fmt->audio_codec);
451
    }
452
453 e70fcf07 Fabrice Bellard
    /* set the output parameters (must be done even if no
454
       parameters). */
455
    if (av_set_parameters(oc, NULL) < 0) {
456
        fprintf(stderr, "Invalid output format parameters\n");
457
        exit(1);
458
    }
459
460 8de65d4f Fabrice Bellard
    dump_format(oc, 0, filename, 1);
461
462 e70fcf07 Fabrice Bellard
    /* now that all the parameters are set, we can open the audio and
463
       video codecs and allocate the necessary encode buffers */
464
    if (video_st)
465
        open_video(oc, video_st);
466
    if (audio_st)
467
        open_audio(oc, audio_st);
468
469 8de65d4f Fabrice Bellard
    /* open the output file, if needed */
470
    if (!(fmt->flags & AVFMT_NOFILE)) {
471
        if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
472
            fprintf(stderr, "Could not open '%s'\n", filename);
473
            exit(1);
474
        }
475
    }
476 115329f1 Diego Biurrun
477 8de65d4f Fabrice Bellard
    /* write the stream header, if any */
478
    av_write_header(oc);
479 115329f1 Diego Biurrun
480 8de65d4f Fabrice Bellard
    for(;;) {
481
        /* compute current audio and video time */
482
        if (audio_st)
483 e06d3d55 Michael Niedermayer
            audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
484 8de65d4f Fabrice Bellard
        else
485
            audio_pts = 0.0;
486 115329f1 Diego Biurrun
487 8de65d4f Fabrice Bellard
        if (video_st)
488 e06d3d55 Michael Niedermayer
            video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
489 8de65d4f Fabrice Bellard
        else
490
            video_pts = 0.0;
491
492 115329f1 Diego Biurrun
        if ((!audio_st || audio_pts >= STREAM_DURATION) &&
493 8de65d4f Fabrice Bellard
            (!video_st || video_pts >= STREAM_DURATION))
494
            break;
495 115329f1 Diego Biurrun
496 8de65d4f Fabrice Bellard
        /* write interleaved audio and video frames */
497 e70fcf07 Fabrice Bellard
        if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
498 8de65d4f Fabrice Bellard
            write_audio_frame(oc, audio_st);
499
        } else {
500
            write_video_frame(oc, video_st);
501
        }
502
    }
503
504
    /* close each codec */
505 e70fcf07 Fabrice Bellard
    if (video_st)
506
        close_video(oc, video_st);
507
    if (audio_st)
508
        close_audio(oc, audio_st);
509 8de65d4f Fabrice Bellard
510
    /* write the trailer, if any */
511
    av_write_trailer(oc);
512 115329f1 Diego Biurrun
513 e70fcf07 Fabrice Bellard
    /* free the streams */
514
    for(i = 0; i < oc->nb_streams; i++) {
515 8720de5b Michael Niedermayer
        av_freep(&oc->streams[i]->codec);
516 e70fcf07 Fabrice Bellard
        av_freep(&oc->streams[i]);
517
    }
518
519 8de65d4f Fabrice Bellard
    if (!(fmt->flags & AVFMT_NOFILE)) {
520
        /* close the output file */
521
        url_fclose(&oc->pb);
522
    }
523
524
    /* free the stream */
525
    av_free(oc);
526
527
    return 0;
528
}