informatique:generalites:script_bash_conversion_video_ffmpeg
Ceci est une ancienne révision du document !
Script Bash pour réduire des vidéos avec FFMPEG
J'ai mis au point ce petit script un peu sale que j'utilise pour réduire la taille de vidéos. Les options permettent de choisir :
- la résolution
- le framerate
- de garder le son ou de l'enlever
- le container de sortie (mp4 ou mkv)
- le codec (h264 ou h265)
- le preset d'encodage
- la qualité crf
C'est volontairement limité. J'utilise ça tous les jours, ça me suffit. C'est sûrement du code dégueulasse, mais je ne suis pas informaticien.
#!/bin/bash if [ -z "$1" ]; then echo "--------------------------------" echo "| Video converter using FFMPEG |" echo "--------------------------------" echo "Usage:" echo "1st argument is filename" echo "2nd argument is desired resolution (format: '1280:720' or '-1:720' or '1280:-1'). To keep the original resolution, enter the word 'resolution'" echo "3rd argument is if you want to keep the current framerate (enter 'fps') or change it, in which case enter a numeric value" echo "4th argument is if to keep sound or remove it ('sound', 'nosound')" echo "5th argument is the container : 'mp4' or 'mkv'" echo "6th argument is codec ('264' or '265')" echo "7th argument is the preset : 'ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', or 'slow'" 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 "$2" ]; then echo "Please enter either the word 'resolution' to keep it as it is, or the desired resolution in the following format (1280:720 or -1:720 or 1280:-1)" exit 1 elif [ $2 == 'resolution' ]; then resolution="" else resolution="-vf scale=$2" fi if [ -z "$3" ]; then echo "You must tell if you want to keep the current framerate or if you want to convert it ('fps' or an integer)" exit 1 elif [ $3 == 'fps' ]; then fps="" else fps="-r $3" fi if [ -z "$4" ]; then echo "You must tell if you want to keep or remove sound from the video with either the 'sound' or 'nosound' flag." exit 1 elif [ $4 == 'nosound' ]; then remove_or_copy_sound="-an" elif [ $4 == 'sound' ]; then remove_or_copy_sound="-c:a copy" else echo "Third argument is invalid." echo "You must tell if you want to keep or remove sound from the video with either the 'sound' or 'nosound' flag." exit 1 fi if [ -z "$5" ]; then echo "You must specify the container, either 'mp4' or 'mkv'." exit 1 elif [ $5 == 'mp4' ]; then container=".mp4" elif [ $5 == 'mkv' ]; then container=".mkv" else echo "Fifth argument is invalid." echo "You must specify the container, either 'mp4' or 'mkv'." exit 1 fi if [ -z "$6" ]; then echo "You must supply the desired output codec ('264' or '265')" exit 1 elif [ $6 == '264' ]; then codec='libx264' crf='23' elif [ $6 == '265' ]; then codec='libx265' crf='28' else echo "Sixth argument is invalid" echo "You must supply the desired output codec ('264' or '265')" exit 1 fi case $7 in "ultrafast"|"superfast"|"veryfast"|"faster"|"fast"|"medium"|"slow") preset=$7 ;; *) echo "Seventh argument: you must supply the preset ('ultrafast', 'superfast', 'veryfast', 'faster', 'fast', 'medium', or 'slow')" exit 1 ;; esac if [ "$8" ] then crf=$8 fi ffmpeg -i $filename $resolution $fps -c:v $codec -crf $crf -preset $preset $remove_or_copy_sound "${filename%.*}-resized${container}"
informatique/generalites/script_bash_conversion_video_ffmpeg.1630306486.txt.bz2 · Dernière modification : 30/08/2021 08:54 de lauberterio