c# - 如何使用 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 包装器。
笔记:
- 我没有文件的物理位置,因为这将通过 HTTP 接收文件的内容。因此,我将只有字节数组,这意味着我需要使用输入流转换为另一种格式。
- FFmpeg 命令用于测试从
.h264
到的转换.ts
(使用文件):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() );
(...)
推荐阅读
- haskell - 为什么这种语法不能正确编译?
- sql - 出于测试目的,在 SQL Server 数据库上的所有查询中随机排列行的顺序?
- node.js - dockerized puppetter 不支持下载字体
- javascript - 使用 React.createContext 将数据从父组件状态传递到子组件
- java - 在Java中,如何将文件所有者设置为root?
- html - 如何使 div 的高度动态适合包含它的 div 的大小?
- flutter - Dart 中的批处理期货
- html - 什么是 HTML 元素内的单个字符串,而不是键值对的一部分
- c# - mongodb C# 驱动程序是否调用服务器来获取数据库?
- java - 有没有办法对代码进行分类,以便我可以最小化某些内容并使用注释来编写它对组织的作用?