Skip to content

Instantly share code, notes, and snippets.

@ClimenteA
Last active June 20, 2023 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClimenteA/59ea825b64f6f673a49df1b6e4b512e4 to your computer and use it in GitHub Desktop.
Save ClimenteA/59ea825b64f6f673a49df1b6e4b512e4 to your computer and use it in GitHub Desktop.
Re-encode videos to mp4 1080p 24 fps in case videos are corrupted
import os
import subprocess
input_folder = "./vids"
output_folder = "./reencoded"
# Create the output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Get a list of all the .mp4 files in the input folder
input_files = [
file for file in os.listdir(input_folder) if file.lower().endswith(".mp4")
]
for input_file in input_files:
input_path = os.path.join(input_folder, input_file)
output_path = os.path.join(output_folder, input_file)
# FFmpeg command for reencoding
ffmpeg_cmd = [
"ffmpeg",
"-i", input_path,
"-vf", "scale=1920:1080",
"-r", "24",
"-c:v", "libx264",
"-crf", "23",
"-preset", "slow",
"-c:a", "aac",
"-b:a", "192k",
output_path,
]
# Run the FFmpeg command
# ffmpeg -i [input_path] -vf scale=1920:1080 -r 24 -c:v libx264 -crf 23 -preset slow -c:a aac -b:a 192k [output_path]
subprocess.run(ffmpeg_cmd)
print("Reencoding complete!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment