Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2305867
  • 博文数量: 141
  • 博客积分: 3552
  • 博客等级: 中校
  • 技术积分: 4148
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-15 14:39
个人简介

熟悉Linux下程序设计及各种应用程序 熟悉C Language 熟悉Glusterfs、FFmpeg、CDN 系统设计,计算机图形系统设计、分布式程序设计 目前主要研究方向:流媒体

文章分类

全部博文(141)

分类: LINUX

2014-08-12 15:04:49

在有些场景下,其实只需要转封装,不需要转码,
大概步骤如下:
1. 打开输入的formatcontext
2. 打开输出文件
3. 打开输出的formatcontext
4. 写文件头
5. 复制codec信息
6. 读取输入frame
7. 写输出frame
8. 写文件尾
9. 关闭输出文件

代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libavformat/avformat.h>
  4. #include <libavutil/avutil.h>

  5. int main(int argc, char *argv[])
  6. {
  7.     AVPacket pkt;
  8.     AVFormatContext *input_fmtctx = NULL;
  9.     AVFormatContext *output_fmtctx = NULL;
  10.     AVCodecContext *enc_ctx = NULL;
  11.     AVCodecContext *dec_ctx = NULL;
  12.     AVCodec *encoder = NULL;
  13.     int ret = 0;
  14.     int i = 0;

  15.     av_register_all();

  16.     if (avformat_open_input(&input_fmtctx, "./a.avi", NULL, NULL) < 0) {
  17.         av_log(NULL, AV_LOG_ERROR, "Cannot open the file %s\n", "./a.mp4");
  18.         return -ENOENT;
  19.     }

  20.     if (avformat_find_stream_info(input_fmtctx,0) < 0) {
  21.         av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 Failed to retrieve input stream information\n");
  22.         return -EINVAL;
  23.     }

  24.     av_dump_format(input_fmtctx, NULL, "./a.avi", 0);

  25.     if (avformat_alloc_output_context2(&output_fmtctx, NULL, NULL, "./fuck.mp4") < 0) {
  26.         av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 Cannot open the file %s\n", "./fuck.mp4");
  27.         return -ENOENT;
  28.     }

  29.     for (i = 0; i < input_fmtctx->nb_streams; i++) {
  30.         AVStream *out_stream = NULL;
  31.         AVStream *in_stream = NULL;
  32.         in_stream = input_fmtctx->streams;
  33.         out_stream = avformat_new_stream(output_fmtctx, NULL);
  34.         if (out_stream < 0) {
  35.             av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 Alloc new Stream error\n");
  36.             return -EINVAL;
  37.         }

  38.         avcodec_copy_context(output_fmtctx->streams->codec, input_fmtctx->streams->codec);
  39.         out_stream->codec->codec_tag = 0;
  40.         if (output_fmtctx->oformat->flags & AVFMT_GLOBALHEADER) {
  41.             out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
  42.         }
  43.     }
  44.     av_dump_format(output_fmtctx, NULL, "./fuck.mp4", 1);
  45.     av_init_packet(&pkt);
  46.     pkt.data = NULL;
  47.     pkt.size = 0;

  48.     if (avio_open(&output_fmtctx->pb, "./fuck.mp4", AVIO_FLAG_WRITE) < 0) {
  49.         av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 cannot open the output file '%s'\n", "./fuck.mp4");
  50.         return -ENOENT;
  51.     }

  52.     if ((ret = avformat_write_header(output_fmtctx, NULL)) < 0) {
  53.         av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 Cannot write the header for the file '%s' ret = %d\n", "fuck.mp4", ret);
  54.         return -ENOENT;
  55.     }


  56.     while (1) {
  57.         AVStream *in_stream = NULL;
  58.         AVStream *out_stream = NULL;

  59.         ret = av_read_frame(input_fmtctx, &pkt);
  60.         if (ret < 0) {
  61.             av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 read frame error %d\n", ret);
  62.             break;
  63.         }

  64.         in_stream = input_fmtctx->streams[pkt.stream_index];
  65.         out_stream = output_fmtctx->streams[pkt.stream_index];
  66.         pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  67.         pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  68.         pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
  69.         pkt.pos = -1;
  70.         ret = av_write_frame(output_fmtctx, &pkt);

  71.         if (ret < 0) {
  72.             av_log(NULL, AV_LOG_ERROR, "bbs.chinaffmpeg.org 孙悟空 Muxing Error\n");
  73.             break;
  74.         }
  75.         av_free_packet(&pkt);
  76.     }

  77.     av_write_trailer(output_fmtctx);
  78.     avformat_close_input(&input_fmtctx);
  79.     avio_close(output_fmtctx->pb);
  80.     avformat_free_context(output_fmtctx);

  81.         return 0;

  82. }



阅读(21558) | 评论(8) | 转发(1) |
给主人留下些什么吧!~~

Mitchb0117072014-09-01 11:05:02

我的ffmpeg版本是2.3.3,修改Demo程序后把H264+aac的ts文件转为mp4文件,但是总是提示dts错误信息,请问pts和dts设置怎么调整?
[h264 @ 0x95f78e0] non-existing PPS 0 referenced
[h264 @ 0x95f78e0] decode_slice_header error
[h264 @ 0x95f78e0] no frame!

Ignoring attempt to set invalid timebase 1/0 for st:0
[mp4 @ 0x95f7d40] Using AVStream.codec.time_base as a timebase hint to the muxer is deprecated. Set AVStream.time_base inst

T-Bagwell2014-08-29 22:09:19

Mitchb011707:请问该Demo程序运行过吗?46行好像错了

运行过以后发出来的

回复 | 举报

Mitchb0117072014-08-28 11:27:14

请问该Demo程序运行过吗?46行好像错了