ffmpeg-python outputs black screen when trying to save as mp4

Question:

This is my code. I am trying to make a video out of all pictures in the pics2 directory using ffmpeg.

import subprocess

command = 'ffmpeg -framerate 1 -i "pics2/Image%02d.png" -r 1 -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4'
p = subprocess.call(command, shell=True)

This saves an Output.mp4 successfully but the video has no frames (It is completely black)

How do I solve this?

Asked By: Saya

||

Answers:

The problem with your original script occurs when setting both -framerate and -r. Here is more information about it. Try using this as your command:

command = "ffmpeg -framerate 1 -i 'pics2/Image%02d.png' -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4"

Brief explanation (based on Saaru Lindestøkke’s answer):

ffmpeg               <- call ffmpeg
    -framerate 1     <- set the input framerate to 1
    -i 'pics2/Image%02d.png' <- read PNG images with filename Image01, Image02, etc...
    -vcodec libx264  <- Set the codec to libx264
    -s 1280x720      <- Set the resolution to 1280x720
    -pix_fmt yuv420p <- Set the pixel format to planar YUV 4:2:0, 12bpp
    Output.mp4       <- the output filename
Answered By: nickh

Your player does not like 1 fps

Many players behave similarly.

You can still provide a low frame rate for the input, but give a more normal frame rate for the output. ffmpeg will simply duplicate the frames. The output video won’t look different than your original command, but it will play.

ffmpeg -framerate 1 -i "pics2/Image%02d.png" -r 10 -vcodec libx264 -s 1280x720 -pix_fmt yuv420p Output.mp4
Answered By: llogan

can scale the video using -vf scale=500:-2 this fixed black video issue for me

Answered By: Akshay Chouhan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.