Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

c# - Can I use ffmpeg to output JPEGs to a memory stream instead of a file?

I have a C# app. I have 100 JPEGs (for an example).

I can easily encode this into a video file.

When it has finished encoding on the client app I FTP upload to my server.

It would be 'neater' if I could not write the video file to a disc but instead write it to a memory stream (or array of bytes) and upload via web service perhaps?

I have checked out the ffmpeg documentation but as a C# developer I do not find it natural to understand the C examples.

I guess my first question is it possible, and if so can anyone post an example code in C#? At the moment I am using the process class in C# to execute ffmpeg.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your process method is already good, just needs adjustments:

  1. Set StartupInfo.RedirectStandardOutput = true and StartupInfo.UseShellExecute = false.
  2. Instead of an output file name, call ffmpeg with pipe:, which will make it write to the standard output. Also, since the format cannot be determined from the file name anymore, make sure you use the -f <format> switch as well.
  3. Start the process.
  4. Read from Process.StandardOutput.BaseStream (.BaseStream, so the StreamReader that is .StandardOutput doesn't mess anything up) while the process is running into your memory stream.
  5. Read anything still remaining buffered in Process.StandardOutput.BaseStream.
  6. Profit.

I coded a thumbnailer a while back (BSD 2-clause), that has actual code that demonstrates this. Doesn't matter if it is an image or a video coming out of ffmpeg in the end.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...