You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET中如何将内存文件地址传递给FFMPEGCore.Join函数?

解决方案:使用FFMPEGCore全程在内存中处理byte[]文件并执行拼接操作

核心思路

FFmpeg原生支持通过管道(pipe)读写内存数据,FFMPEGCore虽然上层封装的Join方法默认依赖本地文件路径,但可以通过底层的FFMpegArguments构建管道命令,完全绕开本地文件的读写操作,实现全程内存处理。

具体实现步骤

  • 将内存中的byte[]转为MemoryStream作为输入源
  • 用FFMpegArguments构建支持管道的FFmpeg命令,指定输入输出均为内存流
  • 执行命令后将输出流转为byte[]返回

代码示例

using FFMPEGCore;
using FFMPEGCore.Pipes;
using System.IO;

public byte[] JoinMemoryFile(byte[] inputFileContent)
{
    // 把输入byte[]包装为内存流
    using var inputStream = new MemoryStream(inputFileContent);
    // 创建用于接收结果的输出内存流
    using var outputStream = new MemoryStream();

    // 构建FFmpeg管道命令
    var ffmpegArgs = FFMpegArguments
        .FromPipeInput(new StreamPipeSource(inputStream))
        .OutputToPipe(new StreamPipeSink(outputStream), opts => opts
            .WithVideoCodec("libx264") // 根据需求指定视频编码
            .WithAudioCodec("aac")    // 指定音频编码
            .ForceFormat("mp4"));     // 指定输出文件格式

    // 同步执行命令
    ffmpegArgs.ProcessSynchronously();

    // 将输出流转为byte[]返回
    outputStream.Position = 0;
    return outputStream.ToArray();
}

扩展说明

  • 多文件内存拼接:如果需要合并多个内存中的文件,只需添加多个AddPipeInput即可,示例:
    var ffmpegArgs = FFMpegArguments
        .FromPipeInput(new StreamPipeSource(inputStream1))
        .AddPipeInput(new StreamPipeSource(inputStream2))
        .OutputToPipe(new StreamPipeSink(outputStream), opts => opts
            .WithVideoCodec("libx264")
            .WithAudioCodec("aac")
            .ForceFormat("mp4"));
    
  • 参数调整:可根据实际需求修改编码格式、分辨率、比特率等FFmpeg参数,确保输出符合预期。

内容的提问来源于stack exchange,提问作者aminrd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:02:15