美文网首页
二、mp4转wmv项目讲解

二、mp4转wmv项目讲解

作者: 循环不计次 | 来源:发表于2019-05-23 10:09 被阅读0次

本文用到的函数:

序号 函数名 参数 注释
1 av_register_all void av_register_all(void) 注册所有的封装器和解封器
2 avformat_open_input int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options) 创建一个输入的上下文环境
3 avformat_alloc_output_context2 int avformat_alloc_output_context2(AVFormatContext **ctx, AVOutputFormat *oformat,const char *format_name, const char *filename); 创建一个输出的上下文环境
4 avformat_new_stream AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c) 在指定上下文中插入一个Stream成员
5 avcodec_parameters_copy int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src) copyAVCodec的上下文
6 avio_open int avio_open(AVIOContext **s, const char *url, int flags) 打开FFmpeg的输入输出文件
7 avformat_write_header int avformat_write_header(AVFormatContext *s, AVDictionary **options) 写视频文件头
8 av_read_frame int av_read_frame(AVFormatContext *s, AVPacket *pkt) 读入指定上下文的一帧
9 av_rescale_q_rnd int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq,enum AVRounding rnd) 换算比例
10 av_write_frame int av_write_frame(AVFormatContext *s, AVPacket *pkt) 向指定上下文写入一帧
11 av_packet_unref void av_packet_unref(AVPacket *pkt) 释放AVPacket内存
12 av_write_trailer int av_write_trailer(AVFormatContext *s) 写视频文件尾
13 avio_close int avio_close(AVIOContext *s) 关闭FFmpeg的输入输出文件
0001.jpg
#include <iostream>
extern "C" {
#include<libavformat/avformat.h>
#include<libavcodec/avcodec.h>
}
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avutil.lib")
using namespace std;
int main()
{
    char infile[] = "2.mp4";
    char outfile[] = "output.mov";
    av_register_all();//注册所有的封装器和解封器
    AVFormatContext* inputFormatCtx = NULL;
    avformat_open_input(&inputFormatCtx, infile, NULL, NULL);
    if (!inputFormatCtx) {
        printf("open file fail!\n");
        return -1;
    }
    
    AVFormatContext* outputFormatCtx = NULL;
    avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, outfile);
    if (!outputFormatCtx){
        printf("create OutputCtx fail!\n");
        return -2;
    }
    AVStream *videoStream = avformat_new_stream(outputFormatCtx, NULL);
    AVStream *audioStream= avformat_new_stream(outputFormatCtx, NULL);
    avcodec_parameters_copy(videoStream->codecpar, inputFormatCtx->streams[0]->codecpar);
    avcodec_parameters_copy(audioStream->codecpar, inputFormatCtx->streams[0]->codecpar);
    videoStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    audioStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
    av_dump_format(inputFormatCtx, 0, infile, 0);
    printf("============================================");
    av_dump_format(outputFormatCtx, 0, outfile, 1);
    
    int ret=avio_open(&outputFormatCtx->pb, outfile, AVIO_FLAG_WRITE);
    if (ret < 0) {
        printf("avio open failed!\n");
        return -3;
    }
    avformat_write_header(outputFormatCtx, NULL);
    if (ret < 0)
    {
        printf("write header failed!\n");
        
    }
    AVPacket pkt;
    int i = 0;
    for (;;) 
    {

        ret=av_read_frame(inputFormatCtx, &pkt);
        if (ret < 0) {
            break;
        }
        pkt.pts=av_rescale_q_rnd(pkt.pts,
            inputFormatCtx->streams[pkt.stream_index]->time_base,
            outputFormatCtx->streams[pkt.stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
        pkt.dts=av_rescale_q_rnd(pkt.dts,
            inputFormatCtx->streams[pkt.stream_index]->time_base,
            outputFormatCtx->streams[pkt.stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.pos = -1;
        pkt.duration= av_rescale_q_rnd(pkt.duration,
            inputFormatCtx->streams[pkt.stream_index]->time_base,
            outputFormatCtx->streams[pkt.stream_index]->time_base,
            (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        av_write_frame(outputFormatCtx, &pkt);
        av_packet_unref(&pkt);
        cout <<".";
    }
    cout << "\n=========================" << endl;
    av_write_trailer(outputFormatCtx);
    avio_close(outputFormatCtx->pb);

}

相关文章

网友评论

      本文标题:二、mp4转wmv项目讲解

      本文链接:https://www.haomeiwen.com/subject/jgzmzqtx.html