Découper et redimensionner une vidéo VirtualBox

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: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 faire un crop et upsacler la vidéo d’origine :

ffmpeg -i input.webm \
  -vf "crop=640:400:320:200,scale=1280:800:flags=neighbor" \
  -c:v libx264 -crf 10 -preset veryslow \
  -c:a copy \
  output_1280x800.mp4

ffmpeg -i input.webm \
  -vf "crop=640:400:320:200,scale=1280:800:flags=neighbor" \
  -c:v libvpx-vp9 -crf 10 -b:v 0 -deadline best -cpu-used 0 \
  -c:a copy \
  output_1280x800.webm

Pour automatiser (en Python) :
Usage : python3 convert.py input.webm 0 00:01:30 par exemple

convert1280.py

import subprocess
import sys
import os

if len(sys.argv) < 4:
    print("Usage: python3 script.py   ")
    print("Ex: python3 script.py input.webm 0 00:01:30")
    sys.exit(1)

input  = sys.argv[1]
start  = sys.argv[2]
end    = sys.argv[3]

output_name = "output"

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 MP4 HD 1280x800 (H.264) ===")
subprocess.run([
    "ffmpeg",
    "-i", temp_file,
    "-vf", "crop=640:400:320:200,scale=1280:800:flags=neighbor",
    "-c:v", "libx264",
    "-crf", "10",
    "-preset", "veryslow",
    "-pix_fmt", "yuv420p",
    "-c:a", "aac",
    "-b:a", "192k",
    "-movflags", "+faststart",
    f"{output_name}-1280x800.mp4"
], check=True)

print("=== Export WEBM HD 1280x800 (VP9) ===")
subprocess.run([
    "ffmpeg",
    "-i", temp_file,
    "-vf", "crop=640:400:320:200,scale=1280:800:flags=neighbor",
    "-c:v", "libvpx-vp9",
    "-crf", "10",
    "-b:v", "0",
    "-deadline", "best",
    "-cpu-used", "0",
    "-auto-alt-ref", "0",
    "-c:a", "copy",
    f"{output_name}-1280x800.webm"
], check=True)

print("=== Suppression du fichier temporaire ===")
if os.path.exists(temp_file):
    os.remove(temp_file)

print("=== Terminé ===")

Pour YouTube, il faut passer à 5120×3200 pour que la vidéo ne soit pas dégradée fortement.

convert5120.py

import subprocess
import sys
import os

if len(sys.argv) < 4:
    print("Usage: python3 script.py   ")
    print("Ex: python3 script.py input.webm 0 00:01:30")
    sys.exit(1)

input  = sys.argv[1]
start  = sys.argv[2]
end    = sys.argv[3]

output_name = "output"

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 MP4 HD 5120x3200 (H.264) ===")
subprocess.run([
    "ffmpeg",
    "-i", temp_file,
    "-vf", "crop=640:400:320:200,scale=5120:3200:flags=neighbor",
    "-c:v", "libx264",
    "-crf", "10",
    "-preset", "veryslow",
    "-pix_fmt", "yuv420p",
    "-c:a", "aac",
    "-b:a", "192k",
    "-movflags", "+faststart",
    f"{output_name}-5120x3200.mp4"
], check=True)

print("=== Suppression du fichier temporaire ===")
if os.path.exists(temp_file):
    os.remove(temp_file)

print("=== Terminé ===")