Lagging Output Video from PNG Sequence with FFmpeg
I am trying to export a high-quality video with upscaling and anti-aliasing from a PNG sequence using FFmpeg. However, the output video has significant lag when playing.
Here are some sample PNG sequence files: easyupload.io/8t4szv
And here is the output video: easyupload.io/j6rmu9
The video was created using the following Python script:
import os
import cv2
import subprocess
# Input and output folder paths
input_folder = r'D:\Desktop\svg_files\test_frames'
output_video = r'D:\Desktop\output_video.mp4'
# Get the list of PNG files in the directory and sort them numerically based on the number in the filenames
png_files = [f for f in os.listdir(input_folder) if f.endswith('.png')]
png_files.sort(key=lambda f: int(f.split('.')[0])) # Sort files numerically (e.g., 840.png, 841.png, ...)
# Read the first image to get the dimensions (resolution)
image_path = os.path.join(input_folder, png_files[0])
img = cv2.imread(image_path)
# Get the resolution of the images
height, width, _ = img.shape
# Create a pattern for FFmpeg (e.g., 840.png, 841.png, ..., 875.png)
input_pattern = os.path.join(input_folder, '%d.png')
# Define the FFmpeg command
ffmpeg_cmd = [
r'C:\ffmpeg-7.1.1-essentials_build\bin\ffmpeg.exe', # Specify the FFmpeg path
'-y', # Overwrite output file if it exists
'-framerate', '30', # Adjust frame rate based on your PNG sequence (reduce if needed)
'-start_number', '400', # Start number of the sequence (starting from 840)
'-i', input_pattern, # Input PNG sequence pattern (e.g., 840.png, 841.png, ...)
'-c:v', 'libx264', # Use H.264 codec
'-crf', '18', # CRF value for high-quality video
'-preset', 'veryfast', # Use a faster preset (adjusted for speed vs compression)
'-threads', '8', # Use 8 threads for multi-threaded encoding (adjust according to your CPU)
'-pix_fmt', 'yuv420p', # Pixel format for compatibility
'-vsync', '1', # Enable frame sync to avoid lag
'-tune', 'animation', # Tune the encoding for animation-style content (bar chart race)
output_video # Output video file
]
# Run the FFmpeg command
subprocess.run(ffmpeg_cmd, check=True)
print(f"Video successfully saved to {output_video}")
The problem is that the output video is lagging when played.
Can anyone help identify the issue and suggest what might be causing the lag? Why isn’t the video playing smoothly?
Read more here: Source link
