ffmpeg视频分割,视频合成

ffmpeg实现视频切割

http://blog.csdn.net/dancing_night/article/details/45720255

1、概述

本程序实现把一个视频切割为2个视频,不涉及编解码,最难理解的地方在于pts和dts的计算,要好好看看,不够完美的地方在于没有按照关键帧切割,所以会在切割点花屏,以后改善。

*注:只处理一个视频流和一个音频流,若流多了,估计会crash。

简单说下流程:

打开输入---->打开输出---->根据输入来创建流---->拷贝流设置---->循环读帧---->判断时间点是否到达切割点,并做设置---->设置pts和dts---->写入---->善后

2、代码

[cpp]  view plain  copy
  1. /* 
  2. *最简单的视频切割 
  3. *缪国凯 Mickel 
  4. *821486004@qq.com 
  5. *本程序实现把一个视频切割为2个视频,不涉及编解码,最难理解的地方在于pts和dts的计算,要好好看看 
  6. *不够完美的地方在于没有按照关键帧切割,所以会在切割点花屏,以后改善 
  7. *注:只处理一个视频流和一个音频流,若流多了,估计会crash 
  8. *2015-5-14 
  9. */  
  10.   
  11. #include "stdafx.h"  
  12.   
  13. #ifdef __cplusplus  
  14. extern"C"  
  15. {  
  16. #endif  
  17. #include <libavformat/avformat.h>  
  18. #include "libavcodec/avcodec.h"  
  19. #include "libavfilter/avfiltergraph.h"  
  20. #include "libavfilter/buffersink.h"  
  21. #include "libavfilter/buffersrc.h"  
  22. #include "libavutil/avutil.h"  
  23. #include "libavutil/opt.h"  
  24. #include "libavutil/pixdesc.h"  
  25. #include "libswresample\swresample.h"  
  26. #include "libavutil\fifo.h"  
  27. #include "libavutil/audio_fifo.h"  
  28.   
  29.   
  30. #pragma comment(lib, "avcodec.lib")  
  31. #pragma comment(lib, "avformat.lib")  
  32. #pragma comment(lib, "avutil.lib")  
  33. //#pragma comment(lib, "avdevice.lib")  
  34. #pragma comment(lib, "avfilter.lib")  
  35. //#pragma comment(lib, "postproc.lib")  
  36. #pragma comment(lib, "swresample.lib")  
  37. //#pragma comment(lib, "swscale.lib")  
  38. #ifdef __cplusplus  
  39. };  
  40. #endif  
  41.   
  42.   
  43. int _tmain(int argc, _TCHAR* argv[])  
  44. {  
  45.     if(argc < 3)  
  46.     {  
  47.         printf("no input file!\n");  
  48.         return -1;  
  49.     }  
  50.     AVFormatContext *ifmt_ctx = NULL, *ofmt1_ctx = NULL, *ofmt2_ctx = NULL;  
  51.     AVStream *out1_vstream = NULL, *out1_astream = NULL;  
  52.     AVStream *out2_vstream = NULL, *out2_astream = NULL;  
  53.     char str_out1_filename[10];  
  54.     char str_out2_filename[10];  
  55.     sprintf(str_out1_filename, "test1.%s", argv[2]);  
  56.     sprintf(str_out2_filename, "test2.%s", argv[2]);  
  57.   
  58.     int inVideo_StreamIndex = -1,inAudio_StreamIndex = -1;  
  59.     int ret;  
  60.   
  61.     av_register_all();  
  62.   
  63.     if ((ret = avformat_open_input(&ifmt_ctx, argv[1], NULL, NULL)) < 0)  
  64.     {  
  65.         printf("can not open the in put file format context!\n");  
  66.         return -1;  
  67.     }  
  68.     if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0)  
  69.     {  
  70.         printf("can not find the input stream info!\n");  
  71.         goto end;  
  72.     }  
  73.           
  74.     avformat_alloc_output_context2(&ofmt1_ctx, NULL, NULL, str_out1_filename);  
  75.     if (!ofmt1_ctx)  
  76.     {  
  77.         printf( "Could not create output1 context\n");  
  78.         ret = AVERROR_UNKNOWN;  
  79.         goto end;  
  80.     }  
  81.   
  82.     avformat_alloc_output_context2(&ofmt2_ctx, NULL, NULL, str_out2_filename);  
  83.     if (!ofmt2_ctx)  
  84.     {  
  85.         printf( "Could not create output2 context\n");  
  86.         ret = AVERROR_UNKNOWN;  
  87.         goto end;  
  88.     }  
  89.   
  90.     for (int i = 0; i < ifmt_ctx->nb_streams; i++)  
  91.     {  
  92.         if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)  
  93.         {  
  94.             inVideo_StreamIndex = i;  
  95.             out1_vstream = avformat_new_stream(ofmt1_ctx, NULL);  
  96.             out2_vstream = avformat_new_stream(ofmt2_ctx, NULL);  
  97.   
  98.             //open decoder  
  99.             if(0 > avcodec_open2(ifmt_ctx->streams[i]->codec, avcodec_find_decoder(ifmt_ctx->streams[i]->codec->codec_id), NULL))  
  100.             {  
  101.                 printf("can not find or open video decoder!\n");  
  102.                 goto end;  
  103.             }  
  104.   
  105.             if (!out1_vstream)  
  106.             {  
  107.                 printf("Failed allocating output1 video stream\n");  
  108.                 ret = AVERROR_UNKNOWN;  
  109.                 goto end;  
  110.             }  
  111.             else  
  112.             {  
  113.                 //copy the settings of AVCodecContext;  
  114.                 if (avcodec_copy_context(out1_vstream->codec, ifmt_ctx->streams[i]->codec) < 0)  
  115.                 {  
  116.                     printf( "Failed to copy context from input to output stream codec context\n");  
  117.                     goto end;  
  118.                 }  
  119.                 out1_vstream->codec->codec_tag = 0;  
  120.                 if(ofmt1_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  121.                 {  
  122.                     out1_vstream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  123.                 }  
  124.             }  
  125.   
  126.             if (!out2_vstream)  
  127.             {  
  128.                 printf("Failed allocating output1 video stream\n");  
  129.                 ret = AVERROR_UNKNOWN;  
  130.                 goto end;  
  131.             }  
  132.             else  
  133.             {  
  134.                 //copy the settings of AVCodecContext;  
  135.                 if (avcodec_copy_context(out2_vstream->codec, ifmt_ctx->streams[i]->codec) < 0)  
  136.                 {  
  137.                     printf( "Failed to copy context from input to output stream codec context\n");  
  138.                     goto end;  
  139.                 }  
  140.                 out2_vstream->codec->codec_tag = 0;  
  141.                 if(ofmt2_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  142.                 {  
  143.                     out2_vstream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  144.                 }  
  145.             }  
  146.         }  
  147.         else if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)  
  148.         {  
  149.             inAudio_StreamIndex = i;  
  150.             out1_astream = avformat_new_stream(ofmt1_ctx, NULL);  
  151.             out2_astream = avformat_new_stream(ofmt2_ctx, NULL);  
  152.   
  153.             if (!out1_astream)  
  154.             {  
  155.                 printf("Failed allocating output1 video stream\n");  
  156.                 ret = AVERROR_UNKNOWN;  
  157.                 goto end;  
  158.             }  
  159.             else  
  160.             {  
  161.                 //copy the settings of AVCodecContext;  
  162.                 if (avcodec_copy_context(out1_astream->codec, ifmt_ctx->streams[i]->codec) < 0)  
  163.                 {  
  164.                     printf( "Failed to copy context from input to output stream codec context\n");  
  165.                     goto end;  
  166.                 }  
  167.                 out1_astream->codec->codec_tag = 0;  
  168.                 if(ofmt1_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  169.                 {  
  170.                     out1_astream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  171.                 }  
  172.             }  
  173.   
  174.   
  175.             if (!out2_astream)  
  176.             {  
  177.                 printf("Failed allocating output1 video stream\n");  
  178.                 ret = AVERROR_UNKNOWN;  
  179.                 goto end;  
  180.             }  
  181.             else  
  182.             {  
  183.                 //copy the settings of AVCodecContext;  
  184.                 if (avcodec_copy_context(out2_astream->codec, ifmt_ctx->streams[i]->codec) < 0)  
  185.                 {  
  186.                     printf( "Failed to copy context from input to output stream codec context\n");  
  187.                     goto end;  
  188.                 }  
  189.                 out2_astream->codec->codec_tag = 0;  
  190.                 if(ofmt2_ctx->oformat->flags & AVFMT_GLOBALHEADER)  
  191.                 {  
  192.                     out2_astream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  193.                 }  
  194.             }  
  195.         }  
  196.     }  
  197.   
  198.     //Dump Format------------------  
  199.     printf("\n==============Input Video=============\n");  
  200.     av_dump_format(ifmt_ctx, 0, argv[1], 0);  
  201.     printf("\n==============Output1============\n");  
  202.     av_dump_format(ofmt1_ctx, 0, str_out1_filename, 1);  
  203.     printf("\n==============Output2============\n");  
  204.     av_dump_format(ofmt2_ctx, 0, str_out1_filename, 1);  
  205.     printf("\n======================================\n");  
  206.       
  207.     //open output1 file  
  208.     if (!(ofmt1_ctx->oformat->flags & AVFMT_NOFILE))  
  209.     {  
  210.         if (avio_open(&ofmt1_ctx->pb, str_out1_filename, AVIO_FLAG_WRITE) < 0)  
  211.         {  
  212.             printf( "Could not open output file '%s'", str_out1_filename);  
  213.             goto end;  
  214.         }  
  215.     }  
  216.   
  217.     //open output2 file  
  218.     if (!(ofmt2_ctx->oformat->flags & AVFMT_NOFILE))  
  219.     {  
  220.         if (avio_open(&ofmt2_ctx->pb, str_out2_filename, AVIO_FLAG_WRITE) < 0)  
  221.         {  
  222.             printf( "Could not open output file '%s'", str_out2_filename);  
  223.             goto end;  
  224.         }  
  225.     }  
  226.   
  227.     //write out 1 file header  
  228.     if (avformat_write_header(ofmt1_ctx, NULL) < 0)  
  229.     {  
  230.         printf( "Error occurred when opening video output file\n");  
  231.         goto end;  
  232.     }  
  233.   
  234.     //write out 2 file header  
  235.     if (avformat_write_header(ofmt2_ctx, NULL) < 0)  
  236.     {  
  237.         printf( "Error occurred when opening video output file\n");  
  238.         goto end;  
  239.     }  
  240.     int splitPtsV = 0;//the real split video pts  
  241.     int splitDtsV = 0;  
  242.     int splitPtsA = 0;//the real split audio pts  
  243.     int splitDtsA = 0;  
  244.     int videoIndex = 0;//the real video index  
  245.       
  246.     int splitTime = 30;//the split time (sec)  
  247.   
  248.     AVPacket pkt;  
  249.     while(1)  
  250.     {  
  251.         AVFormatContext *ofmt_ctx;  
  252.         AVStream *in_stream, *out_stream;  
  253.         if (av_read_frame(ifmt_ctx, &pkt) < 0)  
  254.         {  
  255.             break;  
  256.         }  
  257.         in_stream = ifmt_ctx->streams[pkt.stream_index];  
  258.           
  259.         if (pkt.stream_index == inVideo_StreamIndex)  
  260.         {  
  261.             videoIndex++;  
  262.             int time = pkt.pts * (((float)in_stream->time_base.num) / ((float)in_stream->time_base.den));  
  263.             if (time <= splitTime)  
  264.             {  
  265.                 splitPtsV = pkt.pts;  
  266.                 splitDtsV = pkt.dts;                  
  267.                 out_stream = ofmt1_ctx->streams[pkt.stream_index];  
  268.                 ofmt_ctx = ofmt1_ctx;  
  269.             }  
  270.             else  
  271.             {  
  272.                 pkt.pts = pkt.pts - splitPtsV;  
  273.                 pkt.dts = pkt.dts - splitDtsV;  
  274.                 out_stream = ofmt2_ctx->streams[pkt.stream_index];  
  275.                 ofmt_ctx = ofmt2_ctx;  
  276.             }  
  277.         }  
  278.         else if (pkt.stream_index == inAudio_StreamIndex)  
  279.         {  
  280.             int time = pkt.pts * (((float)in_stream->time_base.num) / ((float)in_stream->time_base.den));  
  281.             if (time <= splitTime)  
  282.             {  
  283.                 splitPtsA = pkt.pts;  
  284.                 splitDtsA = pkt.dts;  
  285.   
  286.                 out_stream = ofmt1_ctx->streams[pkt.stream_index];  
  287.                 ofmt_ctx = ofmt1_ctx;  
  288.             }  
  289.             else  
  290.             {     
  291.                 pkt.pts = pkt.pts - splitPtsA;  
  292.                 pkt.dts = pkt.dts - splitDtsA;  
  293.   
  294.                 out_stream = ofmt2_ctx->streams[pkt.stream_index];  
  295.                 ofmt_ctx = ofmt2_ctx;  
  296.             }  
  297.         }  
  298.   
  299.         pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  300.         pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  301.         pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);  
  302.         pkt.pos = -1;  
  303.   
  304.         //write into file  
  305.         if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0)  
  306.         {  
  307.             printf( "Error muxing packet\n");  
  308.             break;  
  309.         }  
  310.   
  311.         av_free_packet(&pkt);  
  312.     }  
  313.   
  314.     av_write_trailer(ofmt1_ctx);  
  315.     av_write_trailer(ofmt2_ctx);  
  316.   
  317. end:  
  318.     avformat_close_input(&ifmt_ctx);  
  319.   
  320.     /* close output */  
  321.     if (ofmt1_ctx && !(ofmt1_ctx->oformat->flags & AVFMT_NOFILE))  
  322.         avio_close(ofmt1_ctx->pb);  
  323.   
  324.     avformat_free_context(ofmt1_ctx);  
  325.   
  326.     /* close output */  
  327.     if (ofmt2_ctx && !(ofmt2_ctx->oformat->flags & AVFMT_NOFILE))  
  328.         avio_close(ofmt2_ctx->pb);  
  329.   
  330.     avformat_free_context(ofmt2_ctx);  
  331.   
  332.     return 0;  
  333. }  


3、下载地址

http://download.csdn.net/detail/dancing_night/8699109












ffmpeg实现视频合并

http://blog.csdn.net/dancing_night/article/details/45724215


1、概述

简单说下流程:

打开输入---->打开输出---->根据输入来创建流---->拷贝流设置---->循环读帧---->判断第一个读完,改输入为第二个---->设置pts和dts---->写入---->善后


2、代码

[cpp]  view plain  copy
  1. /* 
  2. *最简单的视频合并 
  3. *缪国凯 Mickel 
  4. *821486004@qq.com 
  5. *本程序实现把2个视频合并为一个视频,不涉及编解码,但是对视频源有要求,必须是相同的参数 
  6. *着重理解第二个视频开始的时候的pts和dts计算 
  7. *注:只处理一个视频流和一个音频流,若流多了,估计会crash 
  8. *2015-5-14 
  9. */  
  10.   
  11. #include "stdafx.h"  
  12.   
  13. #ifdef __cplusplus  
  14. extern"C"  
  15. {  
  16. #endif  
  17. #include <libavformat/avformat.h>  
  18. #include "libavcodec/avcodec.h"  
  19. #include "libavfilter/avfiltergraph.h"  
  20. #include "libavfilter/buffersink.h"  
  21. #include "libavfilter/buffersrc.h"  
  22. #include "libavutil/avutil.h"  
  23. #include "libavutil/opt.h"  
  24. #include "libavutil/pixdesc.h"  
  25. #include "libswresample\swresample.h"  
  26. #include "libavutil\fifo.h"  
  27. #include "libavutil/audio_fifo.h"  
  28.   
  29.   
  30. #pragma comment(lib, "avcodec.lib")  
  31. #pragma comment(lib, "avformat.lib")  
  32. #pragma comment(lib, "avutil.lib")  
  33.     //#pragma comment(lib, "avdevice.lib")  
  34. #pragma comment(lib, "avfilter.lib")  
  35.     //#pragma comment(lib, "postproc.lib")  
  36. #pragma comment(lib, "swresample.lib")  
  37.     //#pragma comment(lib, "swscale.lib")  
  38. #ifdef __cplusplus  
  39. };  
  40. #endif  
  41.   
  42. AVFormatContext *in1_fmtctx = NULL, *in2_fmtctx = NULL, *out_fmtctx = NULL;  
  43. AVStream *out_video_stream = NULL, *out_audio_stream = NULL;  
  44. int video_stream_index = -1, audio_stream_index = -1;  
  45.   
  46.   
  47. int open_input(const char * in1_name, const char * in2_name)  
  48. {  
  49.     int ret = -1;  
  50.     if ((ret = avformat_open_input(&in1_fmtctx, in1_name, NULL, NULL)) < 0)  
  51.     {  
  52.         printf("can not open the first input context!\n");  
  53.         return ret;  
  54.     }  
  55.     if ((ret = avformat_find_stream_info(in1_fmtctx, NULL)) < 0)  
  56.     {  
  57.         printf("can not find the first input stream info!\n");  
  58.         return ret;  
  59.     }  
  60.   
  61.     if ((ret = avformat_open_input(&in2_fmtctx, in2_name, NULL, NULL)) < 0)  
  62.     {  
  63.         printf("can not open the first input context!\n");  
  64.         return ret;  
  65.     }  
  66.     if ((ret = avformat_find_stream_info(in2_fmtctx, NULL)) < 0)  
  67.     {  
  68.         printf("can not find the second input stream info!\n");  
  69.         return ret;  
  70.     }  
  71. }  
  72.   
  73. int open_output(const char * out_name)  
  74. {  
  75.     int ret = -1;  
  76.     if ((ret = avformat_alloc_output_context2(&out_fmtctx, NULL, NULL, out_name)) < 0)  
  77.     {  
  78.         printf("can not alloc context for output!\n");  
  79.         return ret;  
  80.     }  
  81.   
  82.     //new stream for out put  
  83.     for (int i = 0; i < in1_fmtctx->nb_streams; i++)  
  84.     {  
  85.         if (in1_fmtctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)  
  86.         {  
  87.             video_stream_index = i;  
  88.             out_video_stream = avformat_new_stream(out_fmtctx, NULL);  
  89.             if (!out_video_stream)  
  90.             {  
  91.                 printf("Failed allocating output1 video stream\n");  
  92.                 ret = AVERROR_UNKNOWN;  
  93.                 return ret;  
  94.             }  
  95.             if ((ret = avcodec_copy_context(out_video_stream->codec, in1_fmtctx->streams[i]->codec)) < 0)  
  96.             {  
  97.                 printf("can not copy the video codec context!\n");  
  98.                 return ret;  
  99.             }  
  100.             out_video_stream->codec->codec_tag = 0;  
  101.             if(out_fmtctx->oformat->flags & AVFMT_GLOBALHEADER)  
  102.             {  
  103.                 out_video_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  104.             }  
  105.         }  
  106.         else if (in1_fmtctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)  
  107.         {  
  108.             audio_stream_index = i;  
  109.             out_audio_stream = avformat_new_stream(out_fmtctx, NULL);  
  110.   
  111.             if (!out_audio_stream)  
  112.             {  
  113.                 printf("Failed allocating output1 video stream\n");  
  114.                 ret = AVERROR_UNKNOWN;  
  115.                 return ret;  
  116.             }  
  117.             if ((ret = avcodec_copy_context(out_audio_stream->codec, in1_fmtctx->streams[i]->codec)) < 0)  
  118.             {  
  119.                 printf("can not copy the video codec context!\n");  
  120.                 return ret;  
  121.             }  
  122.             out_audio_stream->codec->codec_tag = 0;  
  123.             if(out_fmtctx->oformat->flags & AVFMT_GLOBALHEADER)  
  124.             {  
  125.                 out_audio_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  126.             }  
  127.         }  
  128.     }  
  129.   
  130.     //open output file  
  131.     if (!(out_fmtctx->oformat->flags & AVFMT_NOFILE))  
  132.     {  
  133.         if ((ret = avio_open(&out_fmtctx->pb, out_name, AVIO_FLAG_WRITE)) < 0)  
  134.         {  
  135.             printf("can not open the out put file handle!\n");  
  136.             return ret;  
  137.         }  
  138.     }  
  139.   
  140.     //write out  file header  
  141.     if ((ret = avformat_write_header(out_fmtctx, NULL)) < 0)  
  142.     {  
  143.         printf( "Error occurred when opening video output file\n");  
  144.         return ret;  
  145.     }  
  146. }  
  147.   
  148. int _tmain(int argc, _TCHAR* argv[])  
  149. {  
  150.     if(argc < 4)  
  151.     {  
  152.         printf("no input file!\n");  
  153.         return -1;  
  154.     }  
  155.     char out_name[20];  
  156.     sprintf(out_name, "combine.%s", argv[3]);  
  157.     av_register_all();    
  158.     if (0 > open_input(argv[1], argv[2]))  
  159.     {  
  160.         goto end;  
  161.     }  
  162.       
  163.     if(0 > open_output(out_name))  
  164.     {  
  165.         goto end;  
  166.     }  
  167.   
  168.     AVFormatContext *input_ctx = in1_fmtctx;  
  169.     AVPacket pkt;  
  170.     int pts_v, pts_a, dts_v, dts_a;  
  171.     while(1)  
  172.     {  
  173.         if(0 > av_read_frame(input_ctx, &pkt))  
  174.         {  
  175.             if (input_ctx == in1_fmtctx)  
  176.             {  
  177.                 float vedioDuraTime, audioDuraTime;  
  178.   
  179.                 //calc the first media dura time  
  180.                 vedioDuraTime = ((float)input_ctx->streams[video_stream_index]->time_base.num /   
  181.                     (float)input_ctx->streams[video_stream_index]->time_base.den) * ((float)pts_v);  
  182.                 audioDuraTime = ((float)input_ctx->streams[audio_stream_index]->time_base.num /   
  183.                     (float)input_ctx->streams[audio_stream_index]->time_base.den) * ((float)pts_a);  
  184.   
  185.                 //calc the pts and dts end of the first media  
  186.                 if (audioDuraTime > vedioDuraTime)  
  187.                 {  
  188.                     dts_v = pts_v = audioDuraTime / ((float)input_ctx->streams[video_stream_index]->time_base.num /   
  189.                         (float)input_ctx->streams[video_stream_index]->time_base.den);  
  190.                     dts_a++;  
  191.                     pts_a++;  
  192.                 }  
  193.                 else  
  194.                 {  
  195.                     dts_a = pts_a = vedioDuraTime / ((float)input_ctx->streams[audio_stream_index]->time_base.num /   
  196.                         (float)input_ctx->streams[audio_stream_index]->time_base.den);  
  197.                     dts_v++;  
  198.                     pts_v++;  
  199.                 }  
  200.                 input_ctx = in2_fmtctx;  
  201.                 continue;  
  202.             }  
  203.             break;  
  204.         }  
  205.   
  206.         if (pkt.stream_index == video_stream_index)  
  207.         {  
  208.             if (input_ctx == in2_fmtctx)  
  209.             {  
  210.                 pkt.pts += pts_v;  
  211.                 pkt.dts += dts_v;  
  212.             }  
  213.             else  
  214.             {  
  215.                 pts_v = pkt.pts;  
  216.                 dts_v = pkt.dts;  
  217.             }  
  218.         }  
  219.         else if (pkt.stream_index == audio_stream_index)  
  220.         {  
  221.             if (input_ctx == in2_fmtctx)  
  222.             {  
  223.                 pkt.pts += pts_a;  
  224.                 pkt.dts += dts_a;  
  225.             }  
  226.             else  
  227.             {  
  228.                 pts_a = pkt.pts;  
  229.                 dts_a = pkt.dts;  
  230.             }  
  231.         }  
  232.   
  233.         pkt.pts = av_rescale_q_rnd(pkt.pts, input_ctx->streams[pkt.stream_index]->time_base,   
  234.             out_fmtctx->streams[pkt.stream_index]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  235.         pkt.dts = av_rescale_q_rnd(pkt.dts, input_ctx->streams[pkt.stream_index]->time_base,   
  236.             out_fmtctx->streams[pkt.stream_index]->time_base, (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));  
  237.         pkt.pos = -1;  
  238.   
  239.         if (av_interleaved_write_frame(out_fmtctx, &pkt) < 0)   
  240.         {  
  241.             printf( "Error muxing packet\n");  
  242.             //break;  
  243.         }  
  244.         av_free_packet(&pkt);         
  245.     }  
  246.   
  247.     av_write_trailer(out_fmtctx);  
  248.       
  249. end:  
  250.     avformat_close_input(&in1_fmtctx);  
  251.     avformat_close_input(&in2_fmtctx);  
  252.   
  253.     /* close output */  
  254.     if (out_fmtctx && !(out_fmtctx->oformat->flags & AVFMT_NOFILE))  
  255.         avio_close(out_fmtctx->pb);  
  256.   
  257.     avformat_free_context(out_fmtctx);  
  258.   
  259.     return 0;  
  260. }  


3、下载地址

http://download.csdn.net/detail/dancing_night/8700439




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值