Pour découper une vidéo (trop longue) :
ffmpeg -i input.webm -ss 0 -to 00:01:30 -c copy output.webm
Ma vidéo, enregistrée avec VirtualBox, est au format 1280×800 mais l’image est entourée de noir que je veux supprimer. La partie centrale qui m’intéresse est au format 640×400. Je fais donc un crop centré :
ffmpeg -i input.webm -vf "crop=640:400:320:200" -c:a copy output.webm
Pour une meilleure qualité :
ffmpeg -i input.webm -vf "crop=640:400:320:200" -c:v libvpx-vp9 -crf 15 -b:v 0 -c:a copy output.webm
ffmpeg -i input.webm \
-vf "crop=640:400:320:200" \
-c:v libx264 \
-crf 16 \
-preset slow \
-pix_fmt yuv420p \
-c:a aac -b:a 192k \
-movflags +faststart \
output.mp4
Pour automatiser (en Python) :
Usage : python3 convert.py input.webm 0 00:01:30 par exemple
import subprocess
import sys
import os
if len(sys.argv) < 4:
print("Usage: python script.py ")
print("Ex: python script.py input.webm 0 00:01:30")
sys.exit(1)
input = sys.argv[1]
start = sys.argv[2]
end = sys.argv[3]
temp_file = "temp_file.webm"
print("=== Coupe de la vidéo ===")
subprocess.run([
"ffmpeg",
"-i", input,
"-ss", start,
"-to", end,
"-c", "copy",
temp_file
], check=True)
print("=== Export WEBM (VP9) ===")
subprocess.run([
"ffmpeg",
"-i", temp_file,
"-vf", "crop=640:400:320:200",
"-c:v", "libvpx-vp9",
"-crf", "15",
"-b:v", "0",
"-c:a", "copy",
"output.webm"
], check=True)
print("=== Export MP4 (H.264) ===")
subprocess.run([
"ffmpeg",
"-i", temp_file,
"-vf", "crop=640:400:320:200",
"-c:v", "libx264",
"-crf", "16",
"-preset", "slow",
"-pix_fmt", "yuv420p",
"-c:a", "aac",
"-b:a", "192k",
"-movflags", "+faststart",
"output.mp4"
], check=True)
print("=== Suppression du fichier temporaire ===")
if os.path.exists(temp_file):
os.remove(temp_file)
print("=== Terminé ===")