GithubHelp home page GithubHelp logo

Transcode in Memory about ffmpegcore HOT 10 CLOSED

rosenbjerg avatar rosenbjerg commented on July 22, 2024
Transcode in Memory

from ffmpegcore.

Comments (10)

Agrejus avatar Agrejus commented on July 22, 2024

The weird thing is, the code works with this line and no changes var processor = arguments.OutputToFile(@"output_test.mp4"); but var processor = arguments.OutputToPipe(new StreamPipeSink(output)); does not work.

from ffmpegcore.

Agrejus avatar Agrejus commented on July 22, 2024

Also, when I run FFMpeg using the command line I can convert my files without issue.

Command: ffmpeg -i "input.avi" "output.mp4"

from ffmpegcore.

rosenbjerg avatar rosenbjerg commented on July 22, 2024

Is there a reason for not using .FromInputFiles(..).ToOutputFile(..) ?

from ffmpegcore.

Agrejus avatar Agrejus commented on July 22, 2024

@rosenbjerg I was trying to do everything in memory, reading from a file stream and writing to a memory stream. I do not want to save the output file, I want to send the output stream to another destination, that's why I am opting for a memory stream.

from ffmpegcore.

rosenbjerg avatar rosenbjerg commented on July 22, 2024

Okay. What sort of stream are you outputting to?
Have you tried from file to pipe? It seems you are using an input file, from your example

from ffmpegcore.

max619 avatar max619 commented on July 22, 2024

@Agrejus Hi, this might be an issue with mp4 container cause it requires seeking when encoding. Try different container, for example mov or webm, as i remember it works with them

from ffmpegcore.

Agrejus avatar Agrejus commented on July 22, 2024

@rosenbjerg I am trying to output to a MemoryStream. I tried from file to pipe and I get a different error, this time with FFMpeg. I suspect that would work if I could get my parameters correct. Error is:

[mp4 @ 00000234cf5120c0] muxer does not support non seekable output
Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument
Error initializing output stream 0:0 -- 
[aac @ 00000234cefa7980] Qavg: 10458.081
[aac @ 00000234cefa7980] 2 frames left in the queue on closing

@max619 webm does work! Thank you

from ffmpegcore.

Agrejus avatar Agrejus commented on July 22, 2024

@rosenbjerg It does work in memory if I output to a webm instead of mp4. I believe we can consider this resolved. Not sure why it says pipe broken for an mp4, but webm works.

from ffmpegcore.

rosenbjerg avatar rosenbjerg commented on July 22, 2024

That might be because FFMpeg tries to seek in the stream when using mp4, as Max explains.
From the FFMpeg output you posted, it seems Max was spot on :)
Max is the expert on the piping support in this library. He implemented it

I'll close this issue now that your problem is solved :)

from ffmpegcore.

dev-fatih-erol avatar dev-fatih-erol commented on July 22, 2024

Sorry for raising the issue.
My research took a lot of time. I hope it helps someone.

var source = await _fileService.UploadAsync("videos", request.Video);

            var startTime = TimeSpan.FromSeconds(20);
            var duration = TimeSpan.FromSeconds(10);

            using var outputStream = new MemoryStream();

            await FFMpegArguments.FromUrlInput(new Uri(source), options => options
                    .WithCustomArgument("-ss " + startTime.ToString(@"hh\:mm\:ss"))
                    .WithDuration(duration))
                    .OutputToPipe(new StreamPipeSink(outputStream), options => options
                    .ForceFormat(VideoType.Mp4)
                    .WithCustomArgument("-movflags frag_keyframe+empty_moov"))
                    .ProcessAsynchronously();

            var outputData = outputStream.ToArray();

In detail, the code consists of the following steps:

  • The first line calls the "_fileService.UploadAsync" method to upload the video file and assigns the result to the "source" variable.

  • The second line assigns the starting time of the segment to the "startTime" variable and the duration of the cut to the "duration" variable.

  • The third line creates a "MemoryStream" object which will be used as a memory stream to write the cut video data to.

  • On the fourth line, an argument is created using the FFMpegArguments library to mark the video to be cut during the specified start time and duration.

  • On the fifth line, the cut video with the specified settings is written to the memory stream using the "OutputToPipe" method.

  • On the sixth line, the "ForceFormat" method is called to save the video in the MP4 format, with the "frag_keyframe+empty_moov" properties.

  • Finally, the "ProcessAsynchronously" method is used to start the process asynchronously, and the cut video is returned as a byte array using the "outputStream.ToArray()" method.

from ffmpegcore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.