您的位置:首页 > 其它

mp4封装灵感收集

2012-06-08 21:09 162 查看
1.使用ffmpeg

ffmpeg写container都是用avformat里的接口来做的,每帧数据都通过***Stream来写的,但你上面的代码很明显就不是如此,建议你直接在网上搜一下ffmpeg写文件, 这样的源码多如牛毛。

FFMpeg SDK 3.2可以编码h264

用ffmpeg把H.264码流封装成mp4
2009-07-23 18:25



代码参考

https://trac.handbrake.fr/browser/trunk/libhb/muxmp4.c

注意

st->codec->extradata里面一定要放入sps、pps两个NAL,否则转码出来的文件QuickTime无法播放

例如,00 00 00 01 67 42 d0 0b 92 54 0a 0f 90 80 00 01 f4 00 00 13 88 47 8a 15 50 00 00 00 01 68 ce 3c 80





2.编码h264

ffmpeg0.7不支持,不知道以前的版本支持吗?

ffmpeg标准版本并没有AAC,H264的编码支持(解码是支持的),若要加上那些支持的话,你需要将相应的faac,x264等库编译好,然后在编译ffmpeg的时候将这些库的支持加上去(在ffmpeg编译的configurate命令里)。

C/C++ code
av_register_all();
 fmt = guess_format(NULL, outfile, NULL);
 oc = av_alloc_format_context();
 oc->oformat = fmt;
    
 snprintf(oc->filename, sizeof(oc->filename), "%s", outfile);

 video_st = NULL;
 audio_st = NULL;
 
 if (fmt->video_codec != CODEC_ID_NONE) {
        video_st = add_video_stream(oc, fmt->video_codec);
  }

 if (av_set_parameters(oc, NULL) < 0) {
        fprintf(stderr, "Invalid output format parameters\n");
        exit(1);
 }
 
 if (video_st){
        open_video(oc, video_st);
 }

 if (!(fmt->flags & ***FMT_NOFILE)) {
        if (url_fopen(&oc->pb,outfile, URL_WRONLY) < 0) {
            fprintf(stderr, "Could not open \'%s\'\n", outfile);
            exit(1);
        }
 }

 if(av_write_header(oc) < 0){
        PRINTF("av_write_header failed\n");    
        return -1;
 }

 for(;;) {
        /* compute current audio and video time */
        
        if (audio_st)
            audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
        else
            audio_pts = 0.0;

        if (video_st)
            video_pts = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
        else
            video_pts = 0.0;

        /* write interleaved audio and video frames */
        if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
           PRINTF("write_audio_frame\n"

3.

我如果用***I文件格式来输出的话,就能播放视频画面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: