informatique:generalites:script_bash_conversion_video_ffmpeg
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
informatique:generalites:script_bash_conversion_video_ffmpeg [30/08/2021 08:40] – créée lauberterio | informatique:generalites:script_bash_conversion_video_ffmpeg [30/08/2021 09:36] (Version actuelle) – lauberterio | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Script Bash pour réduire des vidéos avec FFMPEG ====== | ||
+ | ===== Introduction ===== | ||
+ | |||
+ | J'ai mis au point ce petit script que j' | ||
+ | |||
+ | * la résolution | ||
+ | * le framerate | ||
+ | * de garder le son ou de l' | ||
+ | * le container de sortie (mp4 ou mkv) | ||
+ | * le codec (h264 ou h265) | ||
+ | * le preset d' | ||
+ | * la qualité crf | ||
+ | |||
+ | C'est volontairement limité mais j' | ||
+ | |||
+ | Ça ajoute // | ||
+ | |||
+ | ===== Exemples d' | ||
+ | |||
+ | Convertir video.mp4 | ||
+ | |||
+ | * en conservant sa résolution originale | ||
+ | * en conservant son framerate original | ||
+ | * en copiant la piste sonore telle quelle | ||
+ | * vers un container mp4 | ||
+ | * en utilisant le codec h265 | ||
+ | * en utilisant le preset FFMPEG veryfast | ||
+ | * en utilisant la qualité crf 35 | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | |||
+ | |||
+ | Convertir video.mp4 | ||
+ | |||
+ | * avec une résolution de sortie HD (720p) | ||
+ | * avec un framerate de 30 images/sec | ||
+ | * en enlevant la piste sonore | ||
+ | * vers un container mkv | ||
+ | * en utilisant le codec h264 | ||
+ | * en utilisant le preset FFMPEG slow | ||
+ | * en utilisant la qualité crf 20 | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | |||
+ | ===== Code ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | if [ -z " | ||
+ | echo " | ||
+ | echo "| Video converter using FFMPEG |" | ||
+ | echo " | ||
+ | echo " | ||
+ | echo "1st argument is filename" | ||
+ | echo "2nd argument is desired resolution (format: ' | ||
+ | echo "3rd argument is if you want to keep the current framerate (enter ' | ||
+ | echo "4th argument is if to keep sound or remove it (' | ||
+ | echo "5th argument is the container : | ||
+ | echo "6th argument is codec (' | ||
+ | echo "7th argument is the preset : ' | ||
+ | echo "8th argument is crf value (51-0). Optional. If not set, will use the default value for the chosen codec: 23 for x.264 and 28 for x.265 - The lower the value, the better the quality." | ||
+ | exit 1 | ||
+ | else | ||
+ | filename=$1 | ||
+ | fi | ||
+ | |||
+ | if [ -z " | ||
+ | echo " | ||
+ | exit 1 | ||
+ | elif [ $2 == ' | ||
+ | resolution="" | ||
+ | else | ||
+ | resolution=" | ||
+ | fi | ||
+ | |||
+ | if [ -z " | ||
+ | echo "You must tell if you want to keep the current framerate or if you want to convert it (' | ||
+ | exit 1 | ||
+ | elif [ $3 == ' | ||
+ | fps="" | ||
+ | else | ||
+ | fps=" | ||
+ | fi | ||
+ | |||
+ | if [ -z " | ||
+ | echo "You must tell if you want to keep or remove sound from the video with either the ' | ||
+ | exit 1 | ||
+ | elif [ $4 == ' | ||
+ | remove_or_copy_sound=" | ||
+ | elif [ $4 == ' | ||
+ | remove_or_copy_sound=" | ||
+ | else | ||
+ | echo "Third argument is invalid." | ||
+ | echo "You must tell if you want to keep or remove sound from the video with either the ' | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | if [ -z " | ||
+ | echo "You must specify the container, either ' | ||
+ | exit 1 | ||
+ | elif [ $5 == ' | ||
+ | container=" | ||
+ | elif [ $5 == ' | ||
+ | container=" | ||
+ | else | ||
+ | echo "Fifth argument is invalid." | ||
+ | echo "You must specify the container, either ' | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | |||
+ | if [ -z " | ||
+ | echo "You must supply the desired output codec (' | ||
+ | exit 1 | ||
+ | elif [ $6 == ' | ||
+ | codec=' | ||
+ | crf=' | ||
+ | elif [ $6 == ' | ||
+ | codec=' | ||
+ | crf=' | ||
+ | else | ||
+ | echo "Sixth argument is invalid" | ||
+ | echo "You must supply the desired output codec (' | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | case $7 in | ||
+ | " | ||
+ | preset=$7 | ||
+ | ;; | ||
+ | *) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | ;; | ||
+ | esac | ||
+ | |||
+ | |||
+ | if [ " | ||
+ | then | ||
+ | crf=$8 | ||
+ | fi | ||
+ | |||
+ | |||
+ | ffmpeg -i " | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Bonus : convertir tous les fichiers d'un répertoire ===== | ||
+ | |||
+ | Pour ligne de commande Linux et MacOS. | ||
+ | |||
+ | Utiliser une simple boucle //for// avec substitution de paramètre. | ||
+ | |||
+ | Exemple : | ||
+ | |||
+ | <code bash>for i in *.mp4; do video_converter.sh " | ||
---- | ---- |
informatique/generalites/script_bash_conversion_video_ffmpeg.1630305621.txt.bz2 · Dernière modification : 30/08/2021 08:40 de lauberterio