30

I'd like to generate a video with a black or white background (or even nothing at all) that lasts for a specific duration (e.g. 2 hour long).

Can you please suggest a fast way to do it programatically (e.g. command line, OpenCV)? Thanks.

3
  • You can use a program such as Paint to produce a black or white image, and then display that image on your screen for any length of time. – bames53 Jul 12 '12 at 13:42
  • 4
    I am not too sure this is programming related at all... – David Rodríguez - dribeas Jul 12 '12 at 13:48
  • Yeah even though I put OpenCV as a hint, I was not clear about the programming in my original question. I updated the question. Thanks. – Son Do Lenh Jul 12 '12 at 15:09
59

You can use ffmpeg for this:

ffmpeg -t 7200 -s 640x480 -f rawvideo -pix_fmt rgb24 -r 25 -i /dev/zero empty.mpeg

UPDATE:

-t:       length of the video (in H:m:s format 00:00:00 or in seconds 0.000)
-s:       frame size
-f:       video format
-pix_fmt: pixel format
-r:       fps
-i:       input
11
  • 1
    Would you please explain all those switches? I beg, Sy Borg. – nullpotent Jul 12 '12 at 13:43
  • 3
    FYI, ffmpeg -t 7200 -s qcif -f rawvideo -pix_fmt rgb24 -r 1/3600 -i /dev/zero -r 24 /tmp/silence.mpeg made the conversion process go faster. – Robᵩ Jul 12 '12 at 13:53
  • 2
    When i set -t, for ex., to 120, VLC shows it's 1:46 time left, but plays 2:00. QuickTime Player calculates movie length correctly. ffmpeg log says time=00:01:59.96. I think it's just player time approximation problem. – fedosov Jul 12 '12 at 17:55
  • 3
    What do we do on Windows, where /dev/zero does not exist? – Brad Jul 18 '15 at 18:48
  • 3
    "What do we do on Windows, where /dev/zero does not exist?" ---> set -i to an image file such as C:\user\Pictures\picture.png – Rublacava Nov 7 '18 at 9:16
26

For an h264 output in a MP4 container, use:

ffmpeg -t 7200 -f lavfi -i color=c=black:s=640x480 -c:v libx264 -tune stillimage -pix_fmt yuv420p output.mp4

If you want to include an audio track, simply add the arguments for the audio input and encoding (if necessary). The output's duration will be determined by the shortest input, i.e. the audio. For example:

ffmpeg -f lavfi -i color=c=black:s=640x480 -i audio.ogg -c:v libx264 -tune stillimage -pix_fmt yuv420p -shortest -c:a aac -b:a 128k output-with-audio.mp4
1
  • if it's needed to generate single IDR frame with black color use '-vframes 1' : ffmpeg -f lavfi -i color=c=black:s=1920x1088 -c:v libx264 -tune stillimage -pix_fmt yuv420p -vframes 1 -y black.h264 – Shevach Riabtsev Feb 19 '20 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.