音视频开发——FFmpeg 实现MP4转FLV文件 C语言实现

转换步骤

  1. 初始化FFmpeg库
  2. 打开输入文件
  3. 找到输入文件的流信息
  4. 打开输出文件并设置输出格式
  5. 创建输出文件的流
  6. 初始化解码器和编码器
  7. 读取输入文件的帧并写入输出文件
  8. 释放资源

关键代码

1 初始化FFmpeg库

av_register_all();
  1. 打开输入文件

    if ((ret = avformat_open_input(&input_format_ctx, input_filename, NULL, NULL)) < 0) {
        fprintf(stderr, "Could not open input file '%s'\n", input_filename);
        return ret;
    }
    
  2. 找到输入文件的流信息

    if ((ret = avformat_find_stream_info(input_format_ctx, NULL)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information\n");
        return ret;
    }
    
  3. 打开输出文件并设置输出格式

    avformat_alloc_output_context2(&output_format_ctx, NULL, "flv", output_filename);
    if (!output_format_ctx) {
        fprintf(stderr, "Could not create output context\n");
        return AVERROR_UNKNOWN;
    }
    
  4. 创建输出文件的流

    for (int i = 0; i < input_format_ctx->nb_streams; i++) {
        AVStream *in_stream = input_format_ctx->streams[i];
        AVStream *out_stream = avformat_new_stream(output_format_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed to allocate output stream\n");
            return AVERROR_UNKNOWN;
        }
        // 复制输入流的编解码器参数到输出流
        if ((ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar)) < 0) {
            fprintf(stderr, "Failed to copy codec parameters\n");
            return ret;
        }
        out_stream->codecpar->codec_tag = 0;
    }
    
  5. 打开输出文件

    if (!(output_format_ctx->oformat->flags & AVFMT_NOFILE)) {
        if ((ret = avio_open(&output_format_ctx->pb, output_filename, AVIO_FLAG_WRITE)) < 0) {
            fprintf(stderr, "Could not open output file '%s'\n", output_filename);
            return ret;
        }
    }
    
  6. 写文件头

    if ((ret = avformat_write_header(output_format_ctx, NULL)) < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        return ret;
    }
    
  7. 读取输入文件的帧并写入输出文件

while (1) {
    AVStream *in_stream, *out_stream;

    ret = av_read_frame(input_format_ctx, &packet);
    if (ret < 0)
        break;

    in_stream  = input_format_ctx->streams[packet.stream_index];
    out_stream = output_format_ctx->streams[packet.stream_index];

    log_packet(input_format_ctx, &packet, "in");

    packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
    packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
    packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
    packet.pos = -1;

    log_packet(output_format_ctx, &packet, "out");

    ret = av_interleaved_write_frame(output_format_ctx, &packet);
    if (ret < 0) {
        fprintf(stderr, "Error muxing packet\n");
        break;
    }

    av_packet_unref(&packet);
}

  1. 写文件尾

    av_write_trailer(output_format_ctx);
    
  2. 释放资源

    avformat_close_input(&input_format_ctx);
    
    if (output_format_ctx && !(output_format_ctx->oformat->flags & AVFMT_NOFILE))
        avio_closep(&output_format_ctx->pb);
    
    avformat_free_context(output_format_ctx);
    

通过以上步骤和代码,可以将MP4文件转换为FLV格式。

完整代码

#include <stdio.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
#include <libavutil/timestamp.h>
#include <libswscale/swscale.h>

void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt, const char *tag)
{
    AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;

    printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
           tag,
           av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
           av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
           av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
           pkt->stream_index);
}

int main(int argc, char *argv[])
{
    if (argc < 3) {
        printf("Usage: %s <input file> <output file>\n", argv[0]);
        return -1;
    }

    const char *input_filename = argv[1];
    const char *output_filename = argv[2];

    AVFormatContext *input_format_ctx = NULL, *output_format_ctx = NULL;
    AVPacket packet;
    int ret;

    av_register_all();

    // 打开输入文件
    if ((ret = avformat_open_input(&input_format_ctx, input_filename, NULL, NULL)) < 0) {
        fprintf(stderr, "Could not open input file '%s'\n", input_filename);
        return ret;
    }

    // 找到输入文件的流信息
    if ((ret = avformat_find_stream_info(input_format_ctx, NULL)) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information\n");
        return ret;
    }

    // 打开输出文件并设置输出格式
    avformat_alloc_output_context2(&output_format_ctx, NULL, "flv", output_filename);
    if (!output_format_ctx) {
        fprintf(stderr, "Could not create output context\n");
        return AVERROR_UNKNOWN;
    }

    // 创建输出文件的流
    for (int i = 0; i < input_format_ctx->nb_streams; i++) {
        AVStream *in_stream = input_format_ctx->streams[i];
        AVStream *out_stream = avformat_new_stream(output_format_ctx, NULL);
        if (!out_stream) {
            fprintf(stderr, "Failed to allocate output stream\n");
            return AVERROR_UNKNOWN;
        }

        // 复制输入流的编解码器参数到输出流
        if ((ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar)) < 0) {
            fprintf(stderr, "Failed to copy codec parameters\n");
            return ret;
        }
        out_stream->codecpar->codec_tag = 0;
    }

    // 打开输出文件
    if (!(output_format_ctx->oformat->flags & AVFMT_NOFILE)) {
        if ((ret = avio_open(&output_format_ctx->pb, output_filename, AVIO_FLAG_WRITE)) < 0) {
            fprintf(stderr, "Could not open output file '%s'\n", output_filename);
            return ret;
        }
    }

    // 写文件头
    if ((ret = avformat_write_header(output_format_ctx, NULL)) < 0) {
        fprintf(stderr, "Error occurred when opening output file\n");
        return ret;
    }

    // 读取输入文件的帧并写入输出文件
    while (1) {
        AVStream *in_stream, *out_stream;

        // 获取一个packet
        ret = av_read_frame(input_format_ctx, &packet);
        if (ret < 0)
            break;

        in_stream  = input_format_ctx->streams[packet.stream_index];
        out_stream = output_format_ctx->streams[packet.stream_index];

        // 记录packet的时间戳
        log_packet(input_format_ctx, &packet, "in");

        // 转换PTS/DTS
        packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
        packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
        packet.pos = -1;

        // 记录packet的时间戳
        log_packet(output_format_ctx, &packet, "out");

        // 写packet到输出文件
        ret = av_interleaved_write_frame(output_format_ctx, &packet);
        if (ret < 0) {
            fprintf(stderr, "Error muxing packet\n");
            break;
        }

        av_packet_unref(&packet);
    }

    // 写文件尾
    av_write_trailer(output_format_ctx);

    // 释放资源
    avformat_close_input(&input_format_ctx);

    if (output_format_ctx && !(output_format_ctx->oformat->flags & AVFMT_NOFILE))
        avio_closep(&output_format_ctx->pb);

    avformat_free_context(output_format_ctx);

    return 0;
}

相关推荐

  1. 视频开发——FFmpeg 实现MP4FLV文件 C语言实现

    2024-07-11 21:08:02       20 阅读
  2. ffmpeg视频文件码为MP4格式

    2024-07-11 21:08:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 21:08:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 21:08:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 21:08:02       58 阅读
  4. Python语言-面向对象

    2024-07-11 21:08:02       69 阅读

热门阅读

  1. 【C#】遍历文件夹及其子文件夹指定后缀文件

    2024-07-11 21:08:02       19 阅读
  2. C语言从头学33——内存管理(一)

    2024-07-11 21:08:02       22 阅读
  3. Qt 的Q_PROPERTY关键字

    2024-07-11 21:08:02       19 阅读
  4. C++ 入门08:运算符重载

    2024-07-11 21:08:02       24 阅读
  5. [AI 大模型] 百度 文心一言

    2024-07-11 21:08:02       20 阅读
  6. 架构面试-场景题-单点登录(SSO)怎么实现的

    2024-07-11 21:08:02       22 阅读
  7. 深入理解Spring Boot中的事件驱动架构

    2024-07-11 21:08:02       20 阅读
  8. DDD架构面试问题

    2024-07-11 21:08:02       20 阅读
  9. 解析 pdfminer pdfparser.py

    2024-07-11 21:08:02       22 阅读
  10. 解决vue3子组件onMounted中获取不到props的值

    2024-07-11 21:08:02       17 阅读