首页 > 解决方案 > 如何使用 C#/.NET 的 FFmpeg 包装器从 .h264 转换为 .ts?

问题描述

语境

我在我的 .NET Core API 项目中使用FFMpegCore,该项目接收一个.h264文件(以二进制格式发送,接收并转换为 a byte array)以转换为.ts.

我想使用 FFmpeg将.h264流转换为输出流。.ts

当前方法

(...)

byte[] body;
using ( var ms = new MemoryStream() )
{
    await request.Body.CopyToAsync( ms ); // read sent .h264 data
    body = ms.ToArray();
}

var outputStream = new MemoryStream();

// FFMpegCore
await FFMpegArguments
                .FromPipeInput( new StreamPipeSource( new MemoryStream( body ) ) )
                .OutputToPipe( new StreamPipeSink( outputStream ), options => options
                .ForceFormat( VideoType.MpegTs ) )
                .ProcessAsynchronously();

// view converted ts file
await File.WriteAllBytesAsync( "output.ts", outputStream.ToArray() );

(...)

问题

我没有得到工作.ts文件。我做错了什么?你能给我一些提示或帮助我吗?即使您有其他您认为更适合此问题的 FFmpeg 包装器。

笔记:

标签: c#.net-coreffmpegsharpffmpeg

解决方案


缺少以下参数:.WithVideoCodec( "h264" )on FFMpegArguments

(...)

byte[] body;
using ( var ms = new MemoryStream() )
{
    await request.Body.CopyToAsync( ms ); // read sent .h264 data
    body = ms.ToArray();
}

var outputStream = new MemoryStream();

// FFMpegCore
await FFMpegArguments
                .FromPipeInput( new StreamPipeSource( new MemoryStream( body ) ) )
                .OutputToPipe( new StreamPipeSink( outputStream ), options => options
                .WithVideoCodec( "h264" ) // added this argument
                .ForceFormat( "mpegts" ) ) // or VideoType.MpegTs
                .ProcessAsynchronously();

// view converted ts file
await File.WriteAllBytesAsync( "output.ts", outputStream.ToArray() );

(...)

推荐阅读