muxing.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. * Output a media file in any supported libavformat format. The default
  27. * codecs are used.
  28. * @example muxing.c
  29. */
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <math.h>
  34. #include <libavutil/opt.h>
  35. #include <libavutil/mathematics.h>
  36. #include <libavutil/timestamp.h>
  37. #include <libavformat/avformat.h>
  38. #include <libswscale/swscale.h>
  39. #include <libswresample/swresample.h>
  40. static int audio_is_eof, video_is_eof;
  41. #define STREAM_DURATION 10.0
  42. #define STREAM_FRAME_RATE 25 /* 25 images/s */
  43. #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
  44. static int sws_flags = SWS_BICUBIC;
  45. static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
  46. {
  47. AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
  48. printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
  49. av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
  50. av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
  51. av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
  52. pkt->stream_index);
  53. }
  54. static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
  55. {
  56. /* rescale output packet timestamp values from codec to stream timebase */
  57. pkt->pts = av_rescale_q_rnd(pkt->pts, *time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  58. pkt->dts = av_rescale_q_rnd(pkt->dts, *time_base, st->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
  59. pkt->duration = av_rescale_q(pkt->duration, *time_base, st->time_base);
  60. pkt->stream_index = st->index;
  61. /* Write the compressed frame to the media file. */
  62. log_packet(fmt_ctx, pkt);
  63. return av_interleaved_write_frame(fmt_ctx, pkt);
  64. }
  65. /* Add an output stream. */
  66. static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
  67. enum AVCodecID codec_id)
  68. {
  69. AVCodecContext *c;
  70. AVStream *st;
  71. /* find the encoder */
  72. *codec = avcodec_find_encoder(codec_id);
  73. if (!(*codec)) {
  74. fprintf(stderr, "Could not find encoder for '%s'\n",
  75. avcodec_get_name(codec_id));
  76. exit(1);
  77. }
  78. st = avformat_new_stream(oc, *codec);
  79. if (!st) {
  80. fprintf(stderr, "Could not allocate stream\n");
  81. exit(1);
  82. }
  83. st->id = oc->nb_streams-1;
  84. c = st->codec;
  85. switch ((*codec)->type) {
  86. case AVMEDIA_TYPE_AUDIO:
  87. c->sample_fmt = (*codec)->sample_fmts ?
  88. (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  89. c->bit_rate = 64000;
  90. c->sample_rate = 44100;
  91. c->channels = 2;
  92. break;
  93. case AVMEDIA_TYPE_VIDEO:
  94. c->codec_id = codec_id;
  95. c->bit_rate = 400000;
  96. /* Resolution must be a multiple of two. */
  97. c->width = 352;
  98. c->height = 288;
  99. /* timebase: This is the fundamental unit of time (in seconds) in terms
  100. * of which frame timestamps are represented. For fixed-fps content,
  101. * timebase should be 1/framerate and timestamp increments should be
  102. * identical to 1. */
  103. c->time_base.den = STREAM_FRAME_RATE;
  104. c->time_base.num = 1;
  105. c->gop_size = 12; /* emit one intra frame every twelve frames at most */
  106. c->pix_fmt = STREAM_PIX_FMT;
  107. if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  108. /* just for testing, we also add B frames */
  109. c->max_b_frames = 2;
  110. }
  111. if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
  112. /* Needed to avoid using macroblocks in which some coeffs overflow.
  113. * This does not happen with normal video, it just happens here as
  114. * the motion of the chroma plane does not match the luma plane. */
  115. c->mb_decision = 2;
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. /* Some formats want stream headers to be separate. */
  122. if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  123. c->flags |= CODEC_FLAG_GLOBAL_HEADER;
  124. return st;
  125. }
  126. /**************************************************************/
  127. /* audio output */
  128. static float t, tincr, tincr2;
  129. AVFrame *audio_frame;
  130. static uint8_t **src_samples_data;
  131. static int src_samples_linesize;
  132. static int src_nb_samples;
  133. static int max_dst_nb_samples;
  134. uint8_t **dst_samples_data;
  135. int dst_samples_linesize;
  136. int dst_samples_size;
  137. int samples_count;
  138. struct SwrContext *swr_ctx = NULL;
  139. static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
  140. {
  141. AVCodecContext *c;
  142. int ret;
  143. c = st->codec;
  144. /* allocate and init a re-usable frame */
  145. audio_frame = av_frame_alloc();
  146. if (!audio_frame) {
  147. fprintf(stderr, "Could not allocate audio frame\n");
  148. exit(1);
  149. }
  150. /* open it */
  151. ret = avcodec_open2(c, codec, NULL);
  152. if (ret < 0) {
  153. fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
  154. exit(1);
  155. }
  156. /* init signal generator */
  157. t = 0;
  158. tincr = 2 * M_PI * 110.0 / c->sample_rate;
  159. /* increment frequency by 110 Hz per second */
  160. tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
  161. src_nb_samples = c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ?
  162. 10000 : c->frame_size;
  163. ret = av_samples_alloc_array_and_samples(&src_samples_data, &src_samples_linesize, c->channels,
  164. src_nb_samples, AV_SAMPLE_FMT_S16, 0);
  165. if (ret < 0) {
  166. fprintf(stderr, "Could not allocate source samples\n");
  167. exit(1);
  168. }
  169. /* compute the number of converted samples: buffering is avoided
  170. * ensuring that the output buffer will contain at least all the
  171. * converted input samples */
  172. max_dst_nb_samples = src_nb_samples;
  173. /* create resampler context */
  174. if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
  175. swr_ctx = swr_alloc();
  176. if (!swr_ctx) {
  177. fprintf(stderr, "Could not allocate resampler context\n");
  178. exit(1);
  179. }
  180. /* set options */
  181. av_opt_set_int (swr_ctx, "in_channel_count", c->channels, 0);
  182. av_opt_set_int (swr_ctx, "in_sample_rate", c->sample_rate, 0);
  183. av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  184. av_opt_set_int (swr_ctx, "out_channel_count", c->channels, 0);
  185. av_opt_set_int (swr_ctx, "out_sample_rate", c->sample_rate, 0);
  186. av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
  187. /* initialize the resampling context */
  188. if ((ret = swr_init(swr_ctx)) < 0) {
  189. fprintf(stderr, "Failed to initialize the resampling context\n");
  190. exit(1);
  191. }
  192. ret = av_samples_alloc_array_and_samples(&dst_samples_data, &dst_samples_linesize, c->channels,
  193. max_dst_nb_samples, c->sample_fmt, 0);
  194. if (ret < 0) {
  195. fprintf(stderr, "Could not allocate destination samples\n");
  196. exit(1);
  197. }
  198. } else {
  199. dst_samples_data = src_samples_data;
  200. }
  201. dst_samples_size = av_samples_get_buffer_size(NULL, c->channels, max_dst_nb_samples,
  202. c->sample_fmt, 0);
  203. }
  204. /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
  205. * 'nb_channels' channels. */
  206. static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
  207. {
  208. int j, i, v;
  209. int16_t *q;
  210. q = samples;
  211. for (j = 0; j < frame_size; j++) {
  212. v = (int)(sin(t) * 10000);
  213. for (i = 0; i < nb_channels; i++)
  214. *q++ = v;
  215. t += tincr;
  216. tincr += tincr2;
  217. }
  218. }
  219. static void write_audio_frame(AVFormatContext *oc, AVStream *st, int flush)
  220. {
  221. AVCodecContext *c;
  222. AVPacket pkt = { 0 }; // data and size must be 0;
  223. int got_packet, ret, dst_nb_samples;
  224. av_init_packet(&pkt);
  225. c = st->codec;
  226. if (!flush) {
  227. get_audio_frame((int16_t *)src_samples_data[0], src_nb_samples, c->channels);
  228. /* convert samples from native format to destination codec format, using the resampler */
  229. if (swr_ctx) {
  230. /* compute destination number of samples */
  231. dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + src_nb_samples,
  232. c->sample_rate, c->sample_rate, AV_ROUND_UP);
  233. if (dst_nb_samples > max_dst_nb_samples) {
  234. av_free(dst_samples_data[0]);
  235. ret = av_samples_alloc(dst_samples_data, &dst_samples_linesize, c->channels,
  236. dst_nb_samples, c->sample_fmt, 0);
  237. if (ret < 0)
  238. exit(1);
  239. max_dst_nb_samples = dst_nb_samples;
  240. dst_samples_size = av_samples_get_buffer_size(NULL, c->channels, dst_nb_samples,
  241. c->sample_fmt, 0);
  242. }
  243. /* convert to destination format */
  244. ret = swr_convert(swr_ctx,
  245. dst_samples_data, dst_nb_samples,
  246. (const uint8_t **)src_samples_data, src_nb_samples);
  247. if (ret < 0) {
  248. fprintf(stderr, "Error while converting\n");
  249. exit(1);
  250. }
  251. } else {
  252. dst_nb_samples = src_nb_samples;
  253. }
  254. audio_frame->nb_samples = dst_nb_samples;
  255. audio_frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base);
  256. avcodec_fill_audio_frame(audio_frame, c->channels, c->sample_fmt,
  257. dst_samples_data[0], dst_samples_size, 0);
  258. samples_count += dst_nb_samples;
  259. }
  260. ret = avcodec_encode_audio2(c, &pkt, flush ? NULL : audio_frame, &got_packet);
  261. if (ret < 0) {
  262. fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
  263. exit(1);
  264. }
  265. if (!got_packet) {
  266. if (flush)
  267. audio_is_eof = 1;
  268. return;
  269. }
  270. ret = write_frame(oc, &c->time_base, st, &pkt);
  271. if (ret < 0) {
  272. fprintf(stderr, "Error while writing audio frame: %s\n",
  273. av_err2str(ret));
  274. exit(1);
  275. }
  276. }
  277. static void close_audio(AVFormatContext *oc, AVStream *st)
  278. {
  279. avcodec_close(st->codec);
  280. if (dst_samples_data != src_samples_data) {
  281. av_free(dst_samples_data[0]);
  282. av_free(dst_samples_data);
  283. }
  284. av_free(src_samples_data[0]);
  285. av_free(src_samples_data);
  286. av_frame_free(&audio_frame);
  287. }
  288. /**************************************************************/
  289. /* video output */
  290. static AVFrame *frame;
  291. static AVPicture src_picture, dst_picture;
  292. static int frame_count;
  293. static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
  294. {
  295. int ret;
  296. AVCodecContext *c = st->codec;
  297. /* open the codec */
  298. ret = avcodec_open2(c, codec, NULL);
  299. if (ret < 0) {
  300. fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
  301. exit(1);
  302. }
  303. /* allocate and init a re-usable frame */
  304. frame = av_frame_alloc();
  305. if (!frame) {
  306. fprintf(stderr, "Could not allocate video frame\n");
  307. exit(1);
  308. }
  309. frame->format = c->pix_fmt;
  310. frame->width = c->width;
  311. frame->height = c->height;
  312. /* Allocate the encoded raw picture. */
  313. ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
  314. if (ret < 0) {
  315. fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret));
  316. exit(1);
  317. }
  318. /* If the output format is not YUV420P, then a temporary YUV420P
  319. * picture is needed too. It is then converted to the required
  320. * output format. */
  321. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  322. ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
  323. if (ret < 0) {
  324. fprintf(stderr, "Could not allocate temporary picture: %s\n",
  325. av_err2str(ret));
  326. exit(1);
  327. }
  328. }
  329. /* copy data and linesize picture pointers to frame */
  330. *((AVPicture *)frame) = dst_picture;
  331. }
  332. /* Prepare a dummy image. */
  333. static void fill_yuv_image(AVPicture *pict, int frame_index,
  334. int width, int height)
  335. {
  336. int x, y, i;
  337. i = frame_index;
  338. /* Y */
  339. for (y = 0; y < height; y++)
  340. for (x = 0; x < width; x++)
  341. pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
  342. /* Cb and Cr */
  343. for (y = 0; y < height / 2; y++) {
  344. for (x = 0; x < width / 2; x++) {
  345. pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
  346. pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
  347. }
  348. }
  349. }
  350. static void write_video_frame(AVFormatContext *oc, AVStream *st, int flush)
  351. {
  352. int ret;
  353. static struct SwsContext *sws_ctx;
  354. AVCodecContext *c = st->codec;
  355. if (!flush) {
  356. if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
  357. /* as we only generate a YUV420P picture, we must convert it
  358. * to the codec pixel format if needed */
  359. if (!sws_ctx) {
  360. sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P,
  361. c->width, c->height, c->pix_fmt,
  362. sws_flags, NULL, NULL, NULL);
  363. if (!sws_ctx) {
  364. fprintf(stderr,
  365. "Could not initialize the conversion context\n");
  366. exit(1);
  367. }
  368. }
  369. fill_yuv_image(&src_picture, frame_count, c->width, c->height);
  370. sws_scale(sws_ctx,
  371. (const uint8_t * const *)src_picture.data, src_picture.linesize,
  372. 0, c->height, dst_picture.data, dst_picture.linesize);
  373. } else {
  374. fill_yuv_image(&dst_picture, frame_count, c->width, c->height);
  375. }
  376. }
  377. if (oc->oformat->flags & AVFMT_RAWPICTURE && !flush) {
  378. /* Raw video case - directly store the picture in the packet */
  379. AVPacket pkt;
  380. av_init_packet(&pkt);
  381. pkt.flags |= AV_PKT_FLAG_KEY;
  382. pkt.stream_index = st->index;
  383. pkt.data = dst_picture.data[0];
  384. pkt.size = sizeof(AVPicture);
  385. ret = av_interleaved_write_frame(oc, &pkt);
  386. } else {
  387. AVPacket pkt = { 0 };
  388. int got_packet;
  389. av_init_packet(&pkt);
  390. /* encode the image */
  391. frame->pts = frame_count;
  392. ret = avcodec_encode_video2(c, &pkt, flush ? NULL : frame, &got_packet);
  393. if (ret < 0) {
  394. fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
  395. exit(1);
  396. }
  397. /* If size is zero, it means the image was buffered. */
  398. if (got_packet) {
  399. ret = write_frame(oc, &c->time_base, st, &pkt);
  400. } else {
  401. if (flush)
  402. video_is_eof = 1;
  403. ret = 0;
  404. }
  405. }
  406. if (ret < 0) {
  407. fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
  408. exit(1);
  409. }
  410. frame_count++;
  411. }
  412. static void close_video(AVFormatContext *oc, AVStream *st)
  413. {
  414. avcodec_close(st->codec);
  415. av_free(src_picture.data[0]);
  416. av_free(dst_picture.data[0]);
  417. av_frame_free(&frame);
  418. }
  419. /**************************************************************/
  420. /* media file output */
  421. int main(int argc, char **argv)
  422. {
  423. const char *filename;
  424. AVOutputFormat *fmt;
  425. AVFormatContext *oc;
  426. AVStream *audio_st, *video_st;
  427. AVCodec *audio_codec, *video_codec;
  428. double audio_time, video_time;
  429. int flush, ret;
  430. /* Initialize libavcodec, and register all codecs and formats. */
  431. av_register_all();
  432. if (argc != 2) {
  433. printf("usage: %s output_file\n"
  434. "API example program to output a media file with libavformat.\n"
  435. "This program generates a synthetic audio and video stream, encodes and\n"
  436. "muxes them into a file named output_file.\n"
  437. "The output format is automatically guessed according to the file extension.\n"
  438. "Raw images can also be output by using '%%d' in the filename.\n"
  439. "\n", argv[0]);
  440. return 1;
  441. }
  442. filename = argv[1];
  443. /* allocate the output media context */
  444. avformat_alloc_output_context2(&oc, NULL, NULL, filename);
  445. if (!oc) {
  446. printf("Could not deduce output format from file extension: using MPEG.\n");
  447. avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
  448. }
  449. if (!oc)
  450. return 1;
  451. fmt = oc->oformat;
  452. /* Add the audio and video streams using the default format codecs
  453. * and initialize the codecs. */
  454. video_st = NULL;
  455. audio_st = NULL;
  456. if (fmt->video_codec != AV_CODEC_ID_NONE)
  457. video_st = add_stream(oc, &video_codec, fmt->video_codec);
  458. if (fmt->audio_codec != AV_CODEC_ID_NONE)
  459. audio_st = add_stream(oc, &audio_codec, fmt->audio_codec);
  460. /* Now that all the parameters are set, we can open the audio and
  461. * video codecs and allocate the necessary encode buffers. */
  462. if (video_st)
  463. open_video(oc, video_codec, video_st);
  464. if (audio_st)
  465. open_audio(oc, audio_codec, audio_st);
  466. av_dump_format(oc, 0, filename, 1);
  467. /* open the output file, if needed */
  468. if (!(fmt->flags & AVFMT_NOFILE)) {
  469. ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
  470. if (ret < 0) {
  471. fprintf(stderr, "Could not open '%s': %s\n", filename,
  472. av_err2str(ret));
  473. return 1;
  474. }
  475. }
  476. /* Write the stream header, if any. */
  477. ret = avformat_write_header(oc, NULL);
  478. if (ret < 0) {
  479. fprintf(stderr, "Error occurred when opening output file: %s\n",
  480. av_err2str(ret));
  481. return 1;
  482. }
  483. flush = 0;
  484. while ((video_st && !video_is_eof) || (audio_st && !audio_is_eof)) {
  485. /* Compute current audio and video time. */
  486. audio_time = (audio_st && !audio_is_eof) ? audio_st->pts.val * av_q2d(audio_st->time_base) : INFINITY;
  487. video_time = (video_st && !video_is_eof) ? video_st->pts.val * av_q2d(video_st->time_base) : INFINITY;
  488. if (!flush &&
  489. (!audio_st || audio_time >= STREAM_DURATION) &&
  490. (!video_st || video_time >= STREAM_DURATION)) {
  491. flush = 1;
  492. }
  493. /* write interleaved audio and video frames */
  494. if (audio_st && !audio_is_eof && audio_time <= video_time) {
  495. write_audio_frame(oc, audio_st, flush);
  496. } else if (video_st && !video_is_eof && video_time < audio_time) {
  497. write_video_frame(oc, video_st, flush);
  498. }
  499. }
  500. /* Write the trailer, if any. The trailer must be written before you
  501. * close the CodecContexts open when you wrote the header; otherwise
  502. * av_write_trailer() may try to use memory that was freed on
  503. * av_codec_close(). */
  504. av_write_trailer(oc);
  505. /* Close each codec. */
  506. if (video_st)
  507. close_video(oc, video_st);
  508. if (audio_st)
  509. close_audio(oc, audio_st);
  510. if (!(fmt->flags & AVFMT_NOFILE))
  511. /* Close the output file. */
  512. avio_close(oc->pb);
  513. /* free the stream */
  514. avformat_free_context(oc);
  515. return 0;
  516. }