0

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.

I want to convert a .h264 stream into a .ts output stream using FFmpeg.

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. 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.

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.
  • FFmpeg command used to test the conversion from .h264 to .ts (using files): ffmpeg -i file.h264 -an -vcodec copy -f mpegts output.ts
juan_marti
  • 433
  • 1
  • 3
  • 17
  • What do you mean by not getting working .ts file? Corrupted? Not expected codecs? Doesn't open in video application? – Evk Oct 27 '20 at 11:11
  • The video doesn't execute. Do you know if the command that I used with FFmpegCore wrapper is correct? I think the problem could be related to the instruction of the FFmpegCore that I'm using. Maybe some missing argument or codec @Evk – juan_marti Oct 27 '20 at 11:27
  • I just tried to convert mp4 video (with h264 video + aac audio) using your code and resulting file seems to be fine (it's not corrupted and plays) – Evk Oct 27 '20 at 11:32
  • With the code that I shared? @Evk – juan_marti Oct 27 '20 at 11:47
  • Yes with exactly that code (except reading input from file, but also to the byte array as you do). – Evk Oct 27 '20 at 11:55
  • 1
    How may bytes are in your output file? You need to set the position on the memory stream to zero after writing and before reading. So you are missing : outputStream.Position = 0; – jdweng Oct 27 '20 at 12:11
  • Apparently was missing the following argument: .WithVideoCodec( "h264" ) on FFMpegArguments instruction. Thank you @Evk. Should I update my question with this working code? – juan_marti Oct 27 '20 at 15:09
  • I've tried that but it wasn't the issue but you're right. thank you @jdweng – juan_marti Oct 27 '20 at 15:10

1 Answers1

1

Missing the following argument: .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() );

(...)
juan_marti
  • 433
  • 1
  • 3
  • 17