ffmpeg 转换es流成为ps流

目的是将es流转换成为ps流

写入到文件中

c 复制代码
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/timestamp.h>

int main(int argc, char** argv) {
    const char* input_filename = "input.mp4"; // 输入文件名
    const char* output_filename = "output.ps"; // 输出PS文件名

    // 初始化FFmpeg
    av_register_all();
    avformat_network_init();

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

    // 获取输入流信息
    if (avformat_find_stream_info(ifmt_ctx, 0) < 0) {
        fprintf(stderr, "Failed to retrieve input stream information\n");
        return -1;
    }

    // 打开输出文件
    AVFormatContext* ofmt_ctx = NULL;
    avformat_alloc_output_context2(&ofmt_ctx, NULL, "mpeg", output_filename);
    if (!ofmt_ctx) {
        printf("Could not create output context\n");
        return -1;
    }

    // 复制流并设置输出参数
    for (unsigned int i = 0; i < ifmt_ctx->nb_streams; i++) {
        AVStream* in_stream = ifmt_ctx->streams[i];
        AVStream* out_stream = avformat_new_stream(ofmt_ctx, in_stream->codecpar->codec);
        if (!out_stream) {
            fprintf(stderr, "Failed allocating output stream\n");
            return -1;
        }
        avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
        out_stream->time_base = in_stream->time_base;
    }

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

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

    // 重采样和转封装的逻辑通常在这里,但因为我们只是简单地转封装,可以使用av_interleaved_write_frame直接写入
    AVPacket pkt;
    while (1) {
        AVStream* in_stream, *out_stream;
        if (av_read_frame(ifmt_ctx, &pkt) >= 0) {
            in_stream  = ifmt_ctx->streams[pkt.stream_index];
            out_stream = ofmt_ctx->streams[pkt.stream_index];

            // 设置pts/dts
            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));
            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));
            pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
            pkt.pos = -1;

            // 写入包
            if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0) {
                fprintf(stderr, "Error muxing packet\n");
                break;
            }
            av_packet_unref(&pkt);
        } else {
            break;
        }
    }

    // 写入尾部并关闭文件
    av_write_trailer(ofmt_ctx);
    if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
        avio_closep(&ofmt_ctx->pb);

    // 释放资源
    avformat_close_input(&ifmt_ctx);
    avformat_free_context(ofmt_ctx);

    return 0;
}

写入到缓存

c 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <libavformat/avformat.h>

int main(int argc, char *argv[]) {
    AVFormatContext *inFormatContext = NULL;
    AVFormatContext *outFormatContext = NULL;
    AVOutputFormat *outputFormat = NULL;
    AVPacket packet;
    int ret;
    uint8_t *psBuffer = NULL;
    int psBufferSize = 0;

    // 注册所有的FFmpeg组件
    av_register_all();

    // 打开输入文件
    ret = avformat_open_input(&inFormatContext, argv[1], NULL, NULL);
    if (ret < 0) {
        fprintf(stderr, "无法打开输入文件\n");
        return -1;
    }

    // 查找流信息
    ret = avformat_find_stream_info(inFormatContext, NULL);
    if (ret < 0) {
        fprintf(stderr, "无法获取流信息\n");
        return -1;
    }

    // 打开输出文件
    outFormatContext = avformat_alloc_context();
    if (!outFormatContext) {
        fprintf(stderr, "无法创建输出文件上下文\n");
        return -1;
    }

    outputFormat = av_guess_format(NULL, "output.ps", NULL);
    if (!outputFormat) {
        fprintf(stderr, "无法猜测输出格式\n");
        return -1;
    }

    outFormatContext->oformat = outputFormat;

    // 逐个处理输入文件的流
    for (int i = 0; i < inFormatContext->nb_streams; i++) {
        AVStream *inStream = inFormatContext->streams[i];
        AVStream *outStream = avformat_new_stream(outFormatContext, NULL);
        if (!outStream) {
            fprintf(stderr, "无法创建输出流\n");
            return -1;
        }

        ret = avcodec_parameters_copy(outStream->codecpar, inStream->codecpar);
        if (ret < 0) {
            fprintf(stderr, "无法复制编解码参数\n");
            return -1;
        }
    }

    // 分配内存缓冲区
    avio_open_dyn_buf(&outFormatContext->pb);
    
    // 写入输出文件的头部信息
    ret = avformat_write_header(outFormatContext, NULL);
    if (ret < 0) {
        fprintf(stderr, "无法写入输出文件的头部信息\n");
        return -1;
    }

    // 读取数据包并写入内存缓冲区
    while (av_read_frame(inFormatContext, &packet) >= 0) {
        // 将数据包写入内存缓冲区
        ret = av_write_frame(outFormatContext, &packet);
        if (ret < 0) {
            fprintf(stderr, "无法写入数据包\n");
            break;
        }
        av_packet_unref(&packet);
    }

    // 写入输出文件的尾部信息
    av_write_trailer(outFormatContext);

    // 获取内存缓冲区的大小和数据指针
    psBufferSize = avio_close_dyn_buf(outFormatContext->pb, &psBuffer);

    // 对PS数据进行处理,例如可以将其写入文件或进行其他操作
    // 注意:psBuffer 现在包含了整个PS流的数据,其大小为psBufferSize

    // 释放资源
    avformat_close_input(&inFormatContext);
    avformat_free_context(outFormatContext);
    av_free(psBuffer);

    return 0;
}
相关推荐
melonbo1 小时前
检测音视频文件的声压
ffmpeg
windxgz2 小时前
FFmpeg——视频拼接总结
c++·ffmpeg·音视频
hunandede3 小时前
音视频开发35 FFmpeg 编码- 将YUV 和 pcm合成一个mp4文件
ffmpeg·音视频·pcm
paidaxing_s3 小时前
【FFMPEG基础(一)】解码源码
linux·c++·ffmpeg
chenzuoli7 小时前
ffmpeg图片视频编辑器工具的安装与使用
ffmpeg·编辑器·音视频
安步当歌18 小时前
【FFmpeg】av_write_trailer函数
c语言·c++·ffmpeg·视频编解码·video-codec
TMS320VC5257H21 小时前
ffmpeg在powershell和ubuntu终端下的不同格式
linux·ubuntu·ffmpeg
玖尾猫1 天前
04.ffmpeg打印音视频媒体信息
ffmpeg·音视频·媒体
暮雨疏桐2 天前
ffmpeg loglevel
ffmpeg