-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffmpeg-batch
69 lines (53 loc) · 3.39 KB
/
ffmpeg-batch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /bin/bash
# Read the filename from the source file and set it to the destination file. Works with one and multible file inside the same folder.
# Syntax (for systemwide use) $ffmpeg-batch [source container] [target container] [path/from/sourceDIR] [path/to/destination]
# for example: ffmpeg-batch avi mp4 /home/user/Movies /home/user/Movies/optimized
# Important: Don't use "/" at path ending otherwise the script doesn't work!
srcExt=$1 #Source container (avi, flv, mkv, ...). The Script grap only files with this container.
destExt=$2 #Target container (mp4, mkv, ...)
srcDir=$3 #Source folder
destDir=$4 #Destination folder
number=1
# Options for ffmpeg (the fallowing options are optimized for native HW decoding with Plesk)
opts="-movflags faststart -c:v hevc -crf 20 -preset medium --profile main --level-idc 4.0 --high-tier -ac 6 -c:a aac -b:a 192k"
# Grap the Source filename.
for filename in "$srcDir"/*.$srcExt; do
basePath=${filename%.*}
baseName=${basePath##*/}
# Checks whether the file already exists in the target folder. IMPORTANT: This checks only if the file exists. It can NOT continue an unfinished encoding!
if [ -e "$destDir"/"$baseName"."$destExt" ] ; then
echo -e "\033[34m\033[1m>>>>> The file "$baseName"."$destExt" alredy exist in "$destDir"! Continue with next file <<<<<\033[0m"
sleep 3
return 0
else
# Create a file with chapter information for set it in the target video.
ffmpeg -i "$filename" -f ffmetadata /tmp/"$baseName".txt
# Check values to crop black borders. The -t 240 value (240 seconds = 4 minutes) are a good value for an relative save time to set the correct value.
echo -e "\033[32m\033[1m>>>>> Checking information for croping black borders. This can take a while <<<<<\033[0m"
cropvalue=$(ffmpeg -i "$filename" -t 240 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1)
# Search for the forced marked subtitle.
echo -e "\033[32m\033[1m>>>>> Searching for the subtitle maked as forced <<<<<\033[0m"
sleep 3
subIndex=$(ffprobe -v 0 -select_streams s -show_entries stream=index:disposition=forced -of compact=nk=0 "$filename" | grep disposition:forced=1 | sed -ne 's#^stream|index=\([0-9]\+\)|.*$#\1#p')
if [ -z $subIndex ] ; then
echo -e "\033[32m\033[1m>>>>> No forced subtitle present. <<<<<\033[0m"
sleep 3
else
echo -e "\033[32m\033[1m>>>>> Select subtitle Track:"$subIndex" <<<<<\033[0m"
sleep 3
fi
if [ -z $subIndex ] ; then
# Convert and remove black borders if no forced subtitle is present.
echo -e "\033[32m\033[1m>>>>> Convert $filename. No forced subtitle present <<<<<\033[0m"
sleep 3
ffmpeg -y -loglevel error -stats -i "$filename" -i /tmp/metadata.txt -map 0:0 -map 0:a:m:language:ger -vf "$cropvalue" -map_metadata 1 -map_chapters 1 $opts "$destDir"/"$baseName"."$destExt"
else
# Convert and remove black borders if forced subtitle is present.
echo -e "\033[32m\033[1m>>>>> Convert $filename. A forced subtitle is present <<<<<\033[0m"
sleep 3
ffmpeg -y -loglevel error -stats -i "$filename" -i /tmp/metadata.txt -map 0:0 -map 0:a:m:language:ger -map 0:$subIndex -vf "$cropvalue" -map_metadata 1 -map_chapters 1 $opts "$destDir"/"$baseName"."$destExt"
fi
# Delete the Metadata file.
rm -f /tmp/"$baseName".txt
fi
done