scripts-bash/convertMKV

162 lines
5.2 KiB
Bash

#!/bin/bash
###############################################################################
#
# Script to recursively search a directory and batch convert all files of a given
# file type into another file type via HandBrake conversion.
#
# To run in your environment set the variables:
# hbcli - Path to your HandBrakeCLI
#
# source_dir - Starting directory for recursive search
#
# input_file_type - Input file type to search for
#
# output_file_type - Output file type to convert into
#
#
# Change log:
# 2012-01-08: Initial release. Tested on Mac OS X Lion.
#
###############################################################################
clear
logFile=~/convertMKV.log
echo -e "\n"
echo -e "==> Préparation de l'environnement...\n"|tee $logFile
echo ""
if [ -z "$1" ] ; then
echo "Indiquer le type de vidéo a traiter : avi, mkv..."
echo ""
echo "Syntaxe : convertFilm <typeSource> <cropSize:Y|N| >"
echo " à executer depuis le dossier à traiter."
echo ""
sleep 1
exit 0
fi
date_en_cours=$(date +"%Y%m%dT%H%M%S")
source_dir=$(pwd)
input_file_type=$1
output_file_type="new.mkv"
declare -a mesFichiers
listFileName=~/rsbConvert_${date_en_cours}.sh
# Construction d'un tableau des noms de fichiers
i=0
echo find -L "$source_dir" -type f -iname "*.$input_file_type" |tee -a $logFile
find -L "$source_dir" -type f -iname "*.$input_file_type" > ~/fichiersATraiter.lst
while read -e aLine
do
echo "[NFO] $aLine"|tee -a $logFile
if [ ${#aLine} -ge 2 ]; then
echo " a traiter."|tee -a $logFile
mesFichiers[$i]=$aLine
i=$(($i+1))
fi
done < ~/fichiersATraiter.lst
echo -e "\n"
echo -e "==> Traitement des vidéos en cours...\n"|tee -a $logFile
for in_file in "${mesFichiers[@]}"
do # Liste tous les éléments du tableau.
echo "Traitement de : $in_file"|tee -a $logFile
if [ ${#in_file} -ge 2 ]; then
echo -e "\n________________________________________________________________________________"|tee -a $logFile
echo -e "\n ==> Processing…"|tee -a $logFile
echo -e "\n [NFO] Input $in_file"|tee -a $logFile
# Replace the file type
out_file=$(echo "$in_file"|sed "s/\(.*\.\)$input_file_type/\1$output_file_type/g")
echo -e "\n [NFO] Output $out_file"|tee -a $logFile
if [ "$in_file" != "$out_file" ]; then
# Get cropSize
cropSize=$(ffmpeg -i "$in_file" -ss 00:02:00 -vframes 1 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
# Get duration
duration=$(ffmpeg -i "$in_file" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//)
echo -e "\n\n Crop Size : $cropSize"|tee -a $logFile
echo -e "\n\nDuration : $duration"|tee -a $logFile
# -c:v libx264 -tune zerolatency -preset slow -bf 5 -crf 25 -maxrate 6000k -bufsize 3000k -pix_fmt yuv420p \
# -c:v libvpx-vp9 -pix_fmt yuv420p \
echo -e "\n ==> Demande de compression"|tee -a $logFile
cmd_exec="ffmpeg -y -i \"${in_file}\""
if [ ${#cropSize} -ge 2 ]; then
echo -e "\n ==> CROP ${cropSize} "|tee -a $logFile
# compression réaliser avec un crop
cmd_exec="${cmd_exec} -vf ${cropSize}"
fi
# cmd_exec="${cmd_exec} -map 0:v -map 0:a? -map 0:s?"
cmd_exec="${cmd_exec} -map 0"
#cmd_exec="${cmd_exec} -c:a libvorbis -qscale:a 4"
cmd_exec="${cmd_exec} -c:a aac -b:a 128k -ac 2"
cmd_exec="${cmd_exec} -c:v libx264 "
cmd_exec="${cmd_exec} -preset veryfast "
cmd_exec="${cmd_exec} -bf 5 "
cmd_exec="${cmd_exec} -crf 23 -maxrate 20000k -bufsize 20000k -pix_fmt yuv420p"
# cmd_exec="${cmd_exec} -crf 23 -maxrate 3000k -bufsize 6000k -b:v 3000k -pix_fmt yuv420p"
cmd_exec="${cmd_exec} -color_primaries 1 -color_trc 1 -colorspace 1"
cmd_exec="${cmd_exec} -profile:v main -level 4.0 "
cmd_exec="${cmd_exec} -movflags +faststart -profile:v main"
cmd_exec="${cmd_exec} -tune fastdecode+zerolatency"
cmd_exec="${cmd_exec} -max_muxing_queue_size 1024 -ss 00:00:00 -t $duration"
cmd_exec="${cmd_exec} -c:s copy"
cmd_exec="${cmd_exec} \"${out_file}\""
echo ${cmd_exec}>> ${listFileName}
echo -e "\n ==> On indique le fichier traité et le résultat dans un log"|tee -a $logFile
cmd_exec="echo -e \"\\\"${in_file}\\\" > \\\"${out_file}\\\"\">> ~/rsbConvert.log"
echo ${cmd_exec}>> ${listFileName}
echo -e "\n ==> Finished $out_file"|tee -a $logFile
fi
fi
done
chmod +x ${listFileName}
echo -e "rm ${listFileName}">> ${listFileName}
#./rsbConvert.sh
echo -e "\n ==== DONE CONVERTING FILES ===="|tee -a $logFile
echo -e "\n\n fichier à appeler : ${listFileName}\n";
exit 0