简体   繁体   English

如何使用 C#/.NET 的 FFmpeg 包装器从 .h264 转换为 .ts?

[英]How to convert from .h264 to .ts using FFmpeg wrapper for C#/.NET?

Context语境

I'm using FFMpegCore in my .NET Core API project that receives a .h264 file (sent in a binary format that is received and converted into a byte array ) to be converted to a .ts .我在我的 .NET Core API 项目中使用FFMpegCore接收一个.h264文件(以二进制格式发送,接收并转换为byte array )转换为.ts

I want to convert a .h264 stream into a .ts output stream using FFmpeg.我想使用 FFmpeg 将.h264 stream 转换为.ts output stream。

Current Approach当前方法

(...)

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() );

(...)

Problem问题

I'm not getting a working .ts file.我没有得到一个有效的.ts文件。 What I'm a doing wrong?我做错了什么? Could you please give some hint or help me with this?你能给我一些提示或帮助我吗? Even if you have other FFmpeg wrappers that you consider more suitable for this problem.即使您有其他 FFmpeg 包装器,您认为更适合解决此问题。

Notes:笔记:

  • I don't have the physical location of the files since this will receive the content of the files over HTTP. So, I will only have the byte array meaning that I need to use the input stream to convert to another format.我没有文件的物理位置,因为这将接收超过 HTTP 的文件内容。因此,我将只有字节数组,这意味着我需要使用输入 stream 来转换为另一种格式。
  • FFmpeg command used to test the conversion from .h264 to .ts (using files): ffmpeg -i file.h264 -an -vcodec copy -f mpegts output.ts FFmpeg 命令用于测试从.h264.ts的转换(使用文件): ffmpeg -i file.h264 -an -vcodec copy -f mpegts output.ts

Missing the following argument: .WithVideoCodec( "h264" ) on FFMpegArguments .缺少以下参数:FFMpegArguments 上的FFMpegArguments .WithVideoCodec( "h264" )

(...)

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() );

(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM