-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp.sh
87 lines (78 loc) · 4.06 KB
/
pp.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# @author Samuel Walters-Nevet #
# @date Summer, 2016 #
# @brief Batch converter of files to the HEVC codec via FFmpeg, using the libx265 video codec #
# @file pp.sh #
# #
# Prerequisits: #
# ffmpeg #
# mediainfo (sudo apt install mediainfo) #
# AtomicParsely (sudo apt install atomicparsley) #
# #
# Note: #
# The following function can be placed cut and pasted into your bashrc file ('~/.bashrc') to make calling this script easier: #
#
pp(){ #
bash "$HOME/scripts/pp.sh" "$@" #
} #
# #
#################################################################################################################################
set -euo pipefail
USAGE="\tUsage: ppg [-d|-D|-g] <video file[s]>\n"
TRASH=false
DELETE=false
GROUP=false
cleanup() {
echo 'Something went wrong during the transcoding process...' >&2;
echo "'$f_265' will be moved to the trash." >&2;
rm "$f_265" && exit 255;
}
for opt in $@; do
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
# Prints usage if requested
echo -e "$USAGE"
exit 0;
elif [[ "$1" = "-d" ]]; then
TRASH=true;
shift
elif [[ "$1" = "-D" ]]; then
DELETE=true;
shift
elif [[ "$1" = "-g" ]] || [[ "$1" = "-G" ]]; then
GROUP=true;
shift
fi
done
if [[ -z "$1" ]]; then
# Prints usage if no files provided
echo -e "$USAGE"
exit -1;
fi
for f in "$@"; do
[[ ! -f "$f" ]] && echo -e "$USAGE" && exit 1;
f_265="${f%.*}";
[[ "${f##*.}" == "mp4" ]] && f_265="${f_265}_x265"
f_265="${f_265}.mp4";
a_codec="aac"
[[ $(ffprobe "$f" 2>&1 | egrep "Audio: aac") ]] && a_codec="copy";
{
ffmpeg -threads 6 -i "$f" -preset slow -c:v libx265 -c:a "$a_codec" "$f_265" && \
if [[ "$(mediainfo --Inform="General;%Cover%" "$f")" == "Yes" ]]; then
cover="${f%.*}_artwork_1.jpg"
AtomicParsley "$f" -E && \
AtomicParsley "$f_265" --artwork "$cover" --overWrite && \
rm ./"$cover"
fi
} || {
cleanup
}
if [[ "$TRASH" = true ]]; then gvfs-trash "$f"
elif [[ "$DELETE" = true ]]; then rm "$f"
elif [[ "$GROUP" = true ]]; then
verted="$(dirname "$f")/Converted";
[[ -d "$verted" ]] || mkdir "$verted";
mv --backup=numbered "$f" "$verted"
fi && \
{ [[ "$f_265" == "${f%.*}.mp4" ]] || mv "$f_265" "${f%.*}.mp4" ; } && \
echo -e "\nDone!"
done