You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

690 lines
21KB

  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file
  24. * libavformat API example.
  25. *
  26. * @example output.c
  27. * Output a media file in any supported libavformat format. The default
  28. * codecs are used.
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include "libavutil/channel_layout.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. #include "libavformat/avformat.h"
  38. #include "libavresample/avresample.h"
  39. #include "libswscale/swscale.h"
  40. /* 5 seconds stream duration */
  41. #define STREAM_DURATION 5.0
  42. #define STREAM_FRAME_RATE 25 /* 25 images/s */
  43. #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
  44. #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
  45. #define SCALE_FLAGS SWS_BICUBIC
  46. // a wrapper around a single output AVStream
  47. typedef struct OutputStream {
  48. AVStream *st;
  49. AVCodecContext *enc;
  50. /* pts of the next frame that will be generated */
  51. int64_t next_pts;
  52. AVFrame *frame;
  53. AVFrame *tmp_frame;
  54. float t, tincr, tincr2;
  55. struct SwsContext *sws_ctx;
  56. AVAudioResampleContext *avr;
  57. } OutputStream;
  58. /**************************************************************/
  59. /* audio output */
  60. /*
  61. * add an audio output stream
  62. */
  63. static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
  64. enum AVCodecID codec_id)
  65. {
  66. AVCodecContext *c;
  67. AVCodec *codec;
  68. int ret;
  69. /* find the audio encoder */
  70. codec = avcodec_find_encoder(codec_id);
  71. if (!codec) {
  72. fprintf(stderr, "codec not found\n");
  73. exit(1);
  74. }
  75. ost->st = avformat_new_stream(oc, NULL);
  76. if (!ost->st) {
  77. fprintf(stderr, "Could not alloc stream\n");
  78. exit(1);
  79. }
  80. c = avcodec_alloc_context3(codec);
  81. if (!c) {
  82. fprintf(stderr, "Could not alloc an encoding context\n");
  83. exit(1);
  84. }
  85. ost->enc = c;
  86. /* put sample parameters */
  87. c->sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
  88. c->sample_rate = codec->supported_samplerates ? codec->supported_samplerates[0] : 44100;
  89. c->channel_layout = codec->channel_layouts ? codec->channel_layouts[0] : AV_CH_LAYOUT_STEREO;
  90. c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
  91. c->bit_rate = 64000;
  92. ost->st->time_base = (AVRational){ 1, c->sample_rate };
  93. // some formats want stream headers to be separate
  94. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  95. c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  96. /* initialize sample format conversion;
  97. * to simplify the code, we always pass the data through lavr, even
  98. * if the encoder supports the generated format directly -- the price is
  99. * some extra data copying;
  100. */
  101. ost->avr = avresample_alloc_context();
  102. if (!ost->avr) {
  103. fprintf(stderr, "Error allocating the resampling context\n");
  104. exit(1);
  105. }
  106. av_opt_set_int(ost->avr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  107. av_opt_set_int(ost->avr, "in_sample_rate", 44100, 0);
  108. av_opt_set_int(ost->avr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  109. av_opt_set_int(ost->avr, "out_sample_fmt", c->sample_fmt, 0);
  110. av_opt_set_int(ost->avr, "out_sample_rate", c->sample_rate, 0);
  111. av_opt_set_int(ost->avr, "out_channel_layout", c->channel_layout, 0);
  112. ret = avresample_open(ost->avr);
  113. if (ret < 0) {
  114. fprintf(stderr, "Error opening the resampling context\n");
  115. exit(1);
  116. }
  117. }
  118. static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
  119. uint64_t channel_layout,
  120. int sample_rate, int nb_samples)
  121. {
  122. AVFrame *frame = av_frame_alloc();
  123. int ret;
  124. if (!frame) {
  125. fprintf(stderr, "Error allocating an audio frame\n");
  126. exit(1);
  127. }
  128. frame->format = sample_fmt;
  129. frame->channel_layout = channel_layout;
  130. frame->sample_rate = sample_rate;
  131. frame->nb_samples = nb_samples;
  132. if (nb_samples) {
  133. ret = av_frame_get_buffer(frame, 0);
  134. if (ret < 0) {
  135. fprintf(stderr, "Error allocating an audio buffer\n");
  136. exit(1);
  137. }
  138. }
  139. return frame;
  140. }
  141. static void open_audio(AVFormatContext *oc, OutputStream *ost)
  142. {
  143. AVCodecContext *c;
  144. int nb_samples, ret;
  145. c = ost->enc;
  146. /* open it */
  147. if (avcodec_open2(c, NULL, NULL) < 0) {
  148. fprintf(stderr, "could not open codec\n");
  149. exit(1);
  150. }
  151. /* init signal generator */
  152. ost->t = 0;
  153. ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
  154. /* increment frequency by 110 Hz per second */
  155. ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
  156. if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
  157. nb_samples = 10000;
  158. else
  159. nb_samples = c->frame_size;
  160. ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
  161. c->sample_rate, nb_samples);
  162. ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, AV_CH_LAYOUT_STEREO,
  163. 44100, nb_samples);
  164. /* copy the stream parameters to the muxer */
  165. ret = avcodec_parameters_from_context(ost->st->codecpar, c);
  166. if (ret < 0) {
  167. fprintf(stderr, "Could not copy the stream parameters\n");
  168. exit(1);
  169. }
  170. }
  171. /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
  172. * 'nb_channels' channels. */
  173. static AVFrame *get_audio_frame(OutputStream *ost)
  174. {
  175. AVFrame *frame = ost->tmp_frame;
  176. int j, i, v;
  177. int16_t *q = (int16_t*)frame->data[0];
  178. /* check if we want to generate more frames */
  179. if (av_compare_ts(ost->next_pts, ost->enc->time_base,
  180. STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
  181. return NULL;
  182. for (j = 0; j < frame->nb_samples; j++) {
  183. v = (int)(sin(ost->t) * 10000);
  184. for (i = 0; i < ost->enc->channels; i++)
  185. *q++ = v;
  186. ost->t += ost->tincr;
  187. ost->tincr += ost->tincr2;
  188. }
  189. return frame;
  190. }
  191. /* if a frame is provided, send it to the encoder, otherwise flush the encoder;
  192. * return 1 when encoding is finished, 0 otherwise
  193. */
  194. static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
  195. AVFrame *frame)
  196. {
  197. int ret;
  198. ret = avcodec_send_frame(ost->enc, frame);
  199. if (ret < 0) {
  200. fprintf(stderr, "Error submitting a frame for encoding\n");
  201. exit(1);
  202. }
  203. while (ret >= 0) {
  204. AVPacket pkt = { 0 }; // data and size must be 0;
  205. av_init_packet(&pkt);
  206. ret = avcodec_receive_packet(ost->enc, &pkt);
  207. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
  208. fprintf(stderr, "Error encoding a video frame\n");
  209. exit(1);
  210. } else if (ret >= 0) {
  211. av_packet_rescale_ts(&pkt, ost->enc->time_base, ost->st->time_base);
  212. pkt.stream_index = ost->st->index;
  213. /* Write the compressed frame to the media file. */
  214. ret = av_interleaved_write_frame(oc, &pkt);
  215. if (ret < 0) {
  216. fprintf(stderr, "Error while writing video frame\n");
  217. exit(1);
  218. }
  219. }
  220. }
  221. return ret == AVERROR_EOF;
  222. }
  223. /*
  224. * encode one audio frame and send it to the muxer
  225. * return 1 when encoding is finished, 0 otherwise
  226. */
  227. static int process_audio_stream(AVFormatContext *oc, OutputStream *ost)
  228. {
  229. AVFrame *frame;
  230. int got_output = 0;
  231. int ret;
  232. frame = get_audio_frame(ost);
  233. got_output |= !!frame;
  234. /* feed the data to lavr */
  235. if (frame) {
  236. ret = avresample_convert(ost->avr, NULL, 0, 0,
  237. frame->extended_data, frame->linesize[0],
  238. frame->nb_samples);
  239. if (ret < 0) {
  240. fprintf(stderr, "Error feeding audio data to the resampler\n");
  241. exit(1);
  242. }
  243. }
  244. while ((frame && avresample_available(ost->avr) >= ost->frame->nb_samples) ||
  245. (!frame && avresample_get_out_samples(ost->avr, 0))) {
  246. /* when we pass a frame to the encoder, it may keep a reference to it
  247. * internally;
  248. * make sure we do not overwrite it here
  249. */
  250. ret = av_frame_make_writable(ost->frame);
  251. if (ret < 0)
  252. exit(1);
  253. /* the difference between the two avresample calls here is that the
  254. * first one just reads the already converted data that is buffered in
  255. * the lavr output buffer, while the second one also flushes the
  256. * resampler */
  257. if (frame) {
  258. ret = avresample_read(ost->avr, ost->frame->extended_data,
  259. ost->frame->nb_samples);
  260. } else {
  261. ret = avresample_convert(ost->avr, ost->frame->extended_data,
  262. ost->frame->linesize[0], ost->frame->nb_samples,
  263. NULL, 0, 0);
  264. }
  265. if (ret < 0) {
  266. fprintf(stderr, "Error while resampling\n");
  267. exit(1);
  268. } else if (frame && ret != ost->frame->nb_samples) {
  269. fprintf(stderr, "Too few samples returned from lavr\n");
  270. exit(1);
  271. }
  272. ost->frame->nb_samples = ret;
  273. ost->frame->pts = ost->next_pts;
  274. ost->next_pts += ost->frame->nb_samples;
  275. got_output |= encode_audio_frame(oc, ost, ret ? ost->frame : NULL);
  276. }
  277. return !got_output;
  278. }
  279. /**************************************************************/
  280. /* video output */
  281. /* Add a video output stream. */
  282. static void add_video_stream(OutputStream *ost, AVFormatContext *oc,
  283. enum AVCodecID codec_id)
  284. {
  285. AVCodecContext *c;
  286. AVCodec *codec;
  287. /* find the video encoder */
  288. codec = avcodec_find_encoder(codec_id);
  289. if (!codec) {
  290. fprintf(stderr, "codec not found\n");
  291. exit(1);
  292. }
  293. ost->st = avformat_new_stream(oc, NULL);
  294. if (!ost->st) {
  295. fprintf(stderr, "Could not alloc stream\n");
  296. exit(1);
  297. }
  298. c = avcodec_alloc_context3(codec);
  299. if (!c) {
  300. fprintf(stderr, "Could not alloc an encoding context\n");
  301. exit(1);
  302. }
  303. ost->enc = c;
  304. /* Put sample parameters. */
  305. c->bit_rate = 400000;
  306. /* Resolution must be a multiple of two. */
  307. c->width = 352;
  308. c->height = 288;
  309. /* timebase: This is the fundamental unit of time (in seconds) in terms
  310. * of which frame timestamps are represented. For fixed-fps content,
  311. * timebase should be 1/framerate and timestamp increments should be
  312. * identical to 1. */
  313. ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
  314. c->time_base = ost->st->time_base;
  315. c->gop_size = 12; /* emit one intra frame every twelve frames at most */
  316. c->pix_fmt = STREAM_PIX_FMT;
  317. if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  318. /* just for testing, we also add B-frames */
  319. c->max_b_frames = 2;
  320. }
  321. if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  322. /* Needed to avoid using macroblocks in which some coeffs overflow.
  323. * This does not happen with normal video, it just happens here as
  324. * the motion of the chroma plane does not match the luma plane. */
  325. c->mb_decision = 2;
  326. }
  327. /* Some formats want stream headers to be separate. */
  328. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  329. c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
  330. }
  331. static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
  332. {
  333. AVFrame *picture;
  334. int ret;
  335. picture = av_frame_alloc();
  336. if (!picture)
  337. return NULL;
  338. picture->format = pix_fmt;
  339. picture->width = width;
  340. picture->height = height;
  341. /* allocate the buffers for the frame data */
  342. ret = av_frame_get_buffer(picture, 32);
  343. if (ret < 0) {
  344. fprintf(stderr, "Could not allocate frame data.\n");
  345. exit(1);
  346. }
  347. return picture;
  348. }
  349. static void open_video(AVFormatContext *oc, OutputStream *ost)
  350. {
  351. AVCodecContext *c;
  352. int ret;
  353. c = ost->enc;
  354. /* open the codec */
  355. if (avcodec_open2(c, NULL, NULL) < 0) {
  356. fprintf(stderr, "could not open codec\n");
  357. exit(1);
  358. }
  359. /* Allocate the encoded raw picture. */
  360. ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
  361. if (!ost->frame) {
  362. fprintf(stderr, "Could not allocate picture\n");
  363. exit(1);
  364. }
  365. /* If the output format is not YUV420P, then a temporary YUV420P
  366. * picture is needed too. It is then converted to the required
  367. * output format. */
  368. ost->tmp_frame = NULL;
  369. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  370. ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
  371. if (!ost->tmp_frame) {
  372. fprintf(stderr, "Could not allocate temporary picture\n");
  373. exit(1);
  374. }
  375. }
  376. /* copy the stream parameters to the muxer */
  377. ret = avcodec_parameters_from_context(ost->st->codecpar, c);
  378. if (ret < 0) {
  379. fprintf(stderr, "Could not copy the stream parameters\n");
  380. exit(1);
  381. }
  382. }
  383. /* Prepare a dummy image. */
  384. static void fill_yuv_image(AVFrame *pict, int frame_index,
  385. int width, int height)
  386. {
  387. int x, y, i, ret;
  388. /* when we pass a frame to the encoder, it may keep a reference to it
  389. * internally;
  390. * make sure we do not overwrite it here
  391. */
  392. ret = av_frame_make_writable(pict);
  393. if (ret < 0)
  394. exit(1);
  395. i = frame_index;
  396. /* Y */
  397. for (y = 0; y < height; y++)
  398. for (x = 0; x < width; x++)
  399. pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
  400. /* Cb and Cr */
  401. for (y = 0; y < height / 2; y++) {
  402. for (x = 0; x < width / 2; x++) {
  403. pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
  404. pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
  405. }
  406. }
  407. }
  408. static AVFrame *get_video_frame(OutputStream *ost)
  409. {
  410. AVCodecContext *c = ost->enc;
  411. /* check if we want to generate more frames */
  412. if (av_compare_ts(ost->next_pts, c->time_base,
  413. STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
  414. return NULL;
  415. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  416. /* as we only generate a YUV420P picture, we must convert it
  417. * to the codec pixel format if needed */
  418. if (!ost->sws_ctx) {
  419. ost->sws_ctx = sws_getContext(c->width, c->height,
  420. AV_PIX_FMT_YUV420P,
  421. c->width, c->height,
  422. c->pix_fmt,
  423. SCALE_FLAGS, NULL, NULL, NULL);
  424. if (!ost->sws_ctx) {
  425. fprintf(stderr,
  426. "Cannot initialize the conversion context\n");
  427. exit(1);
  428. }
  429. }
  430. fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
  431. sws_scale(ost->sws_ctx, (const uint8_t * const *) ost->tmp_frame->data,
  432. ost->tmp_frame->linesize, 0, c->height, ost->frame->data,
  433. ost->frame->linesize);
  434. } else {
  435. fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
  436. }
  437. ost->frame->pts = ost->next_pts++;
  438. return ost->frame;
  439. }
  440. /*
  441. * encode one video frame and send it to the muxer
  442. * return 1 when encoding is finished, 0 otherwise
  443. */
  444. static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
  445. {
  446. int ret;
  447. AVCodecContext *c;
  448. AVFrame *frame;
  449. c = ost->enc;
  450. frame = get_video_frame(ost);
  451. /* encode the image */
  452. ret = avcodec_send_frame(c, frame);
  453. if (ret < 0) {
  454. fprintf(stderr, "Error submitting a frame for encoding\n");
  455. exit(1);
  456. }
  457. while (ret >= 0) {
  458. AVPacket pkt = { 0 };
  459. av_init_packet(&pkt);
  460. ret = avcodec_receive_packet(c, &pkt);
  461. if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
  462. fprintf(stderr, "Error encoding a video frame\n");
  463. exit(1);
  464. } else if (ret >= 0) {
  465. av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
  466. pkt.stream_index = ost->st->index;
  467. /* Write the compressed frame to the media file. */
  468. ret = av_interleaved_write_frame(oc, &pkt);
  469. if (ret < 0) {
  470. fprintf(stderr, "Error while writing video frame\n");
  471. exit(1);
  472. }
  473. }
  474. }
  475. return ret == AVERROR_EOF;
  476. }
  477. static void close_stream(AVFormatContext *oc, OutputStream *ost)
  478. {
  479. avcodec_free_context(&ost->enc);
  480. av_frame_free(&ost->frame);
  481. av_frame_free(&ost->tmp_frame);
  482. sws_freeContext(ost->sws_ctx);
  483. avresample_free(&ost->avr);
  484. }
  485. /**************************************************************/
  486. /* media file output */
  487. int main(int argc, char **argv)
  488. {
  489. OutputStream video_st = { 0 }, audio_st = { 0 };
  490. const char *filename;
  491. AVOutputFormat *fmt;
  492. AVFormatContext *oc;
  493. int have_video = 0, have_audio = 0;
  494. int encode_video = 0, encode_audio = 0;
  495. /* Initialize libavcodec, and register all codecs and formats. */
  496. av_register_all();
  497. if (argc != 2) {
  498. printf("usage: %s output_file\n"
  499. "API example program to output a media file with libavformat.\n"
  500. "The output format is automatically guessed according to the file extension.\n"
  501. "Raw images can also be output by using '%%d' in the filename\n"
  502. "\n", argv[0]);
  503. return 1;
  504. }
  505. filename = argv[1];
  506. /* Autodetect the output format from the name. default is MPEG. */
  507. fmt = av_guess_format(NULL, filename, NULL);
  508. if (!fmt) {
  509. printf("Could not deduce output format from file extension: using MPEG.\n");
  510. fmt = av_guess_format("mpeg", NULL, NULL);
  511. }
  512. if (!fmt) {
  513. fprintf(stderr, "Could not find suitable output format\n");
  514. return 1;
  515. }
  516. /* Allocate the output media context. */
  517. oc = avformat_alloc_context();
  518. if (!oc) {
  519. fprintf(stderr, "Memory error\n");
  520. return 1;
  521. }
  522. oc->oformat = fmt;
  523. snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
  524. /* Add the audio and video streams using the default format codecs
  525. * and initialize the codecs. */
  526. if (fmt->video_codec != AV_CODEC_ID_NONE) {
  527. add_video_stream(&video_st, oc, fmt->video_codec);
  528. have_video = 1;
  529. encode_video = 1;
  530. }
  531. if (fmt->audio_codec != AV_CODEC_ID_NONE) {
  532. add_audio_stream(&audio_st, oc, fmt->audio_codec);
  533. have_audio = 1;
  534. encode_audio = 1;
  535. }
  536. /* Now that all the parameters are set, we can open the audio and
  537. * video codecs and allocate the necessary encode buffers. */
  538. if (have_video)
  539. open_video(oc, &video_st);
  540. if (have_audio)
  541. open_audio(oc, &audio_st);
  542. av_dump_format(oc, 0, filename, 1);
  543. /* open the output file, if needed */
  544. if (!(fmt->flags & AVFMT_NOFILE)) {
  545. if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
  546. fprintf(stderr, "Could not open '%s'\n", filename);
  547. return 1;
  548. }
  549. }
  550. /* Write the stream header, if any. */
  551. avformat_write_header(oc, NULL);
  552. while (encode_video || encode_audio) {
  553. /* select the stream to encode */
  554. if (encode_video &&
  555. (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,
  556. audio_st.next_pts, audio_st.enc->time_base) <= 0)) {
  557. encode_video = !write_video_frame(oc, &video_st);
  558. } else {
  559. encode_audio = !process_audio_stream(oc, &audio_st);
  560. }
  561. }
  562. /* Write the trailer, if any. The trailer must be written before you
  563. * close the CodecContexts open when you wrote the header; otherwise
  564. * av_write_trailer() may try to use memory that was freed on
  565. * av_codec_close(). */
  566. av_write_trailer(oc);
  567. /* Close each codec. */
  568. if (have_video)
  569. close_stream(oc, &video_st);
  570. if (have_audio)
  571. close_stream(oc, &audio_st);
  572. if (!(fmt->flags & AVFMT_NOFILE))
  573. /* Close the output file. */
  574. avio_close(oc->pb);
  575. /* free the stream */
  576. avformat_free_context(oc);
  577. return 0;
  578. }