Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG: how to save input camera stream into the file with the SAME codec format?

I have the camera-like device that produces video stream and passes it into my Windows-based machine via USB port.

Using the command:

ffmpeg -y -f vfwcap -i list

I see that (as expected) FFmpeg finds the input stream as stream #0.

Using the command:

ffmpeg -y -f vfwcap -r 25 -i 0 c:\out.mp4

I can successfully save the input stream into the file.

From the log I see:

Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 240x320, 25 tbr, 1k tbn, 25 tbc No pixel format specified, yuv422p for H.264 encoding chosen.

So, my input format is transcoded to yuv422p.

My question:

  • How can I cause FFmpeg to save my input video stream into out.mp4 WITHOUT transcoding - actually, to copy input stream to output file as close as possible, with the same format?
like image 358
ZviDan Avatar asked Jan 19 '14 12:01

ZviDan


1 Answers

How can I cause ffmpeg to save my input videostream into out.mp4 WITHOUT transcoding

You can not. You can stream copy the rawvideo from vfwcap, but the MP4 container format does not support rawvideo. You have several options:

  • Use a different output container format.
  • Stream copy to rawvideo then encode.
  • Use a lossless encoder (and optionally re-encode it after capturing).

Use a different output container format

This meets your requirement of saving your input without re-encoding.

ffmpeg -f vfwcap -i 0 -codec:v copy rawvideo.nut
  • rawvideo creates huge file sizes.

Stream copy to rawvideo then encode

This is the same as above, but the rawvideo is then encoded to a more common format.

ffmpeg -f vfwcap -i 0 -codec:v copy rawvideo.nut
ffmpeg -i rawvideo.nut -codec:v libx264 -crf 23 -preset medium -pix_fmt yuv420p -movflags +faststart output.mp4
  • See the FFmpeg and x264 Encoding Guide for more information about -crf, -preset, and additional detailed information on creating H.264 video.

  • -pix_fmt yuv420p will use a pixel format that is compatible with dumb players like QuickTime. Refer to colorspace and chroma subsampling for more info.

  • -movflags +faststart relocates the moov atom which allows the video to begin playback before it is completely downloaded by the client. Useful if you are hosting the video and users will view it in their browser.

Use a lossless encoder

Using huffyuv:

ffmpeg -f vfwcap -i 0 -codec:v huffyuv lossless.mkv

Using lossless H.264:

ffmpeg -f vfwcap -i 0 -codec:v libx264 -qp 0 lossless.mp4
  • Lossless files can be huge, but not as big as rawvideo.
  • Re-encoding the lossless output is the same as re-encoding the rawvideo.
like image 190
llogan Avatar answered Sep 20 '22 11:09

llogan