-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp_nvenc.sh
113 lines (98 loc) · 5.54 KB
/
pp_nvenc.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
# @author Samuel Walters-Nevet #
# @date 2016-2017 #
# @brief Batch converter of files to H264 codec via FFmpeg, using NVidia's 'h264_nvenc' codec #
# @file pp_nenv.sh #
# #
# Prerequisits: #
# ffmpeg with nvenc (see 'build_ffmpeg.sh') #
# 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: #
ppn(){ #
bash "$HOME/scripts/pp_nvenc".sh "$@" #
} #
# #
#################################################################################################################################################################################
TRASH=false
DELETE=false
GROUP=false
GROUP_HERE=false;
USAGE="\tUsage: $(basename $0) [-d|-D|-g|-G] <video file[s]>
-d
Move original file to the trash after transcoding.
-D
Imediately delete the original file after transcoding
-g
Group the original file in a directory named 'Converted'.
(The directory is created in the same directory of the original file.)
-G
Group the original file in a directory named 'Converted'.
(The directory is created in the current working directory.)
"
cleanup() {
echo 'Something went wrong during the transcoding process...' >&2;
echo "'$f_264' will be moved to the trash." >&2;
rm "$f_264" && exit $1;
}
while getopts ":dDgGh" opt
do
case $opt in
d) TRASH=true;;
D) DELETE=true;;
g) GROUP=true;;
G) GROUP_HERE=true;;
h) echo -e "$USAGE"; exit ;;
*) echo "Un-imlemented option chosen"
echo "Try '$0 -h' for usage details."
exit;;
esac
done
shift $((OPTIND-1))
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_264="${f%.*}";
[[ "${f##*.}" == "mp4" ]] && f_264="${f_264}_nvenc"
f_264="${f_264}.mp4";
a_codec="aac"
[[ $(ffprobe "$f" 2>&1 | egrep "Audio: aac") ]] && a_codec="copy";
{
ffmpeg -threads 6 -i "$f" -preset slow -c:v h264_nvenc -c:a "$a_codec" "$f_264"
# Check FFmpeg exit code:
FFexit=$?
[[ $FFexit -ne 0 ]] && cleanup $FFexit
if [[ "$(mediainfo --Inform="General;%Cover%" "$f")" == "Yes" ]]; then
cover="${f%.*}_artwork_1.jpg"
AtomicParsley "$f" -E && \
AtomicParsley "$f_264" --artwork "$cover" --overWrite && \
rm ./"$cover"
fi
} || {
cleanup 1
}
if [[ "$TRASH" = true ]]; then
gvfs-trash "$f";
[[ "${f_264%.*}" != "${f%.*}" ]] && mv "$f_264" "${f%.*}.mp4";
elif [[ "$DELETE" = true ]]; then
rm "$f"
[[ "${f_264%.*}" != "${f%.*}" ]] && mv "$f_264" "${f%.*}.mp4";
elif [[ "$GROUP" = true ]]; then
verted="$(dirname "$f")/Converted";
[[ -d "$verted" ]] || mkdir "$verted";
mv --backup=numbered "$f" "$verted";
[[ "${f_264%.*}" != "${f%.*}" ]] && mv "$f_264" "${f%.*}.mp4";
elif [[ "$GROUP_HERE" = true ]]; then
verted="$PWD/Converted";
[[ -d "$verted" ]] || mkdir "$verted";
mv --backup=numbered "$f" "$verted";
[[ "${f_264%.*}" != "${f%.*}" ]] && mv "$f_264" "${f%.*}.mp4";
fi && \
echo -e "\nDone!"
done;