如何使用 C#/.NET 的 FFmpeg 包装器从 .h264 转换为 .ts?
How to convert from .h264 to .ts using FFmpeg wrapper for C#/.NET?
上下文
我在我的 .NET Core API 项目中使用 FFMpegCore 接收 .h264
文件(以二进制格式发送,接收并转换为 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 包装器。
备注:
- 我没有文件的物理位置,因为这将通过 HTTP 接收文件的内容。所以,我将只有字节数组,这意味着我需要使用输入流来转换为另一种格式。
- 用于测试从
.h264
到.ts
的转换的FFmpeg命令(使用文件):ffmpeg -i file.h264 -an -vcodec copy -f mpegts output.ts
缺少以下参数:.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() );
(...)
上下文
我在我的 .NET Core API 项目中使用 FFMpegCore 接收 .h264
文件(以二进制格式发送,接收并转换为 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 包装器。
备注:
- 我没有文件的物理位置,因为这将通过 HTTP 接收文件的内容。所以,我将只有字节数组,这意味着我需要使用输入流来转换为另一种格式。
- 用于测试从
.h264
到.ts
的转换的FFmpeg命令(使用文件):ffmpeg -i file.h264 -an -vcodec copy -f mpegts output.ts
缺少以下参数:.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() );
(...)