ffmpeg 转换es流成为ps流

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

写入到文件中

#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;
}

写入到缓存

#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;
}

相关推荐

  1. ffmpeg 转换es成为ps

    2024-05-03 09:56:08       34 阅读
  2. IO转换

    2024-05-03 09:56:08       27 阅读
  3. FFmpeg实现RTSP推

    2024-05-03 09:56:08       67 阅读
  4. FFmpeg实现rtp推

    2024-05-03 09:56:08       51 阅读

最近更新

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

    2024-05-03 09:56:08       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-03 09:56:08       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-03 09:56:08       87 阅读
  4. Python语言-面向对象

    2024-05-03 09:56:08       96 阅读

热门阅读

  1. 揭秘FastStone Capture

    2024-05-03 09:56:08       45 阅读
  2. Nacos在微服务架构中如何支持服务发现和注册

    2024-05-03 09:56:08       40 阅读
  3. 区分Vue2和Vue3的配置读取(附Demo)

    2024-05-03 09:56:08       32 阅读
  4. 高可用系列四:loadbalancer 负载均衡

    2024-05-03 09:56:08       31 阅读
  5. 愚安科技安全工程师面经:

    2024-05-03 09:56:08       32 阅读
  6. Windows 系统运维常用命令

    2024-05-03 09:56:08       28 阅读
  7. 什么是Vuex它的作用是什么怎么用

    2024-05-03 09:56:08       36 阅读
  8. 新人天上下凡尘

    2024-05-03 09:56:08       30 阅读
  9. redis

    redis

    2024-05-03 09:56:08      32 阅读
  10. 孩子学编程,考级和竞赛二者间的比较

    2024-05-03 09:56:08       29 阅读