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;
}
相关推荐
Designerpupu6 小时前
PS2026 移除工具灰色无法点击怎么办?2026 最新故障解决教程
photoshop·ps·nano banana·创成式填充·移除工具·ps2026·智能擦除
霸道流氓气质1 天前
视频预览链路三件套:ZLMediaKit · MediaMTX · FFmpeg 完全指南
ffmpeg·音视频
xgc_java1 天前
在Java里把ONNX/OpenCV/FFmpeg跑稳:28篇bytedeco实战小册完整指南
java·opencv·ffmpeg
Designerpupu5 天前
Photoshop 手机壳印花贴图:3 步快速做出真实产品效果图
gpt·photoshop·贴图·ps·跨境电商·手机壳贴图·贴图样机
Designerpupu7 天前
纸巾亚马逊详情页怎么做?3 步用 GPT 生成高转化产品图片
gpt·ai·photoshop·ps·跨境电商·亚马逊·详情页
鲲穹AI全能助理7 天前
实用音频合成工具分享:灵境配音 使用场景与功能说明
ffmpeg·音视频
我是Superman丶7 天前
Windows系统FFmpeg官方下载+完整安装配置保姆级教程(2026最新版)
windows·ffmpeg
luoyayun3619 天前
Qt + FFmpeg 视频工具:视频一键压缩功能实现
qt·ffmpeg·音视频·视频压缩
雪的季节9 天前
ffmpeg源码国内gitee下载
ffmpeg·gitee
Mister Leon11 天前
FFmpeg - Jetson Orin 实战部署
ffmpeg