-
Notifications
You must be signed in to change notification settings - Fork 3
/
transcoder.sh
144 lines (122 loc) · 6.57 KB
/
transcoder.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
cd /home/lab/sportfiles
old=/home/lab/old_pwd.lst
new=/home/lab/new_pwd.lst
while true
do
> $old
> $new
# Create VOD list
for i in `ls`
do
find "$(pwd)"/$i | grep .mp4 >> $old
done
sleep 10
# Check if there are any new VOD
for i in `ls`
do
find "$(pwd)"/$i | grep .mp4 >> $new
done
# Clear log file
> transcoder.log
# Declare vod list
new_cnt=$(cat $new | wc -l)
old_cnt=$(cat $old | wc -l)
# Show the difference files
result=$(diff $old $new | awk '{print $2}')
echo "================================================================"
echo "old/new files count: $new_cnt/$old_cnt"
echo "================================================================"
for i in $result
do
if [ "$new_cnt" -ne "$old_cnt" ]
then
# Detect resolution an itrate from source
resolution=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 $i)
bitrate=$(ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 $i)
# Detect resolution widht and height
widht=$(echo "$resolution" | awk -F "x" '{ print $1}')
height=$(echo "$resolution" | awk -F "x" '{ print $2}')
# Calculate adaptive resolution
height1=$(($widht/3*2))
height2=$(($widht/4*2))
weght1=$(($height/3*2))
weght2=$(($height/4*2))
# Calculate adaptive bitrate
originalbitrate=$(($bitrate/1024))
hdbitrate=$(($bitrate/1024/2))
sdbitrate=$(($bitrate/1024/3))
# Date
NOW=$(date +"%H:%M:%S %d-%m-%Y")
# Find transcoded files
fhd_cnt=$(ls -la ./* | grep "1080.mp4" | wc -l)
hd_cnt=$(ls -la ./* | grep "720.mp4" | wc -l)
sd_cnt=$(ls -la ./* | grep "480.mp4" | wc -l)
# Output log
echo "================================================================"
echo "Stat: = Total File: $new_cnt | transcoded fhd:$fhd_cnt hd:$hd_cnt sd:$sd_cnt"
echo "---------------------------------------------------------------"
echo "Source info"
echo "File: = $i"
echo "Detected Resolution: $resolution"
echo "Detected Bitrate: "$originalbitrate"/Kbps"
echo "---------------------------------------------------------------"
echo "Stream 1"
echo " Resolution: $resolution"
echo " Bitrate: "$originalbitrate"/Kbps"
echo " Profile: high"
echo " Preset: slow"
echo "Stream 2"
echo " Resolution: "$height1"x"$weght1""
echo " Bitrate: "$hdbitrate"/Kbps"
echo " Profile: main"
echo " Preset: medium"
echo "Stream 3"
echo " Resolution: "$height2"x"$weght2""
echo " Bitrate: "$sdbitrate"/Kbps"
echo " Profile: baseline"
echo " Preset: fast"
echo "================================================================"
echo "================================================================" >> transcoder.log
echo "Stat: = Total File: $new_cnt | transcoded fhd:$fhd_cnt hd:$hd_cnt sd:$sd_cnt" >> transcoder.log
echo "---------------------------------------------------------------" >> transcoder.log
echo "Source info" >> transcoder.log
echo "File: = $i" >> transcoder.log
echo "Detected Resolution: $resolution" >> transcoder.log
echo "Detected Bitrate: "$originalbitrate"/Kbps" >> transcoder.log
echo "---------------------------------------------------------------" >> transcoder.log
echo "Stream 1" >> transcoder.log
echo " Resolution: $resolution" >> transcoder.log
echo " Bitrate: "$originalbitrate"/Kbps" >> transcoder.log
echo " Profile: high" >> transcoder.log
echo " Preset: slow" >> transcoder.log
echo "Stream 2" >> transcoder.log
echo " Resolution: "$height1"x"$weght1"" >> transcoder.log
echo " Bitrate: "$hdbitrate"/Kbps" >> transcoder.log
echo " Profile: main" >> transcoder.log
echo " Preset: medium" >> transcoder.log
echo "Stream 3" >> transcoder.log
echo " Resolution: "$height2"x"$weght2"" >> transcoder.log
echo " Bitrate: "$sdbitrate"/Kbps" >> transcoder.log
echo " Profile: baseline" >> transcoder.log
echo " Preset: fast" >> transcoder.log
echo "================================================================" >> transcoder.log
# Transcoding
ffmpeg -n -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512 -vsync 1 -hwaccel cuvid -c:v h264_cuvid -resize $resolution -i $i \
-c:v h264_nvenc -b:v "$originalbitrate"K -g 48 -keyint_min 48 -preset slow -profile:v high -c:a aac -ar 44100 -ac 2 $i-1080p.mp4
ffmpeg -n -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512 -vsync 1 -hwaccel cuvid -c:v h264_cuvid -resize "$height1"x"$weght1" -i $i \
-c:v h264_nvenc -b:v "$hdbitrate"K -g 48 -keyint_min 48 -preset medium -profile:v main -c:a aac -ar 44100 -ac 2 $i-720p.mp4
ffmpeg -n -threads:v 2 -threads:a 8 -filter_threads 2 -thread_queue_size 512 -vsync 1 -hwaccel cuvid -c:v h264_cuvid -resize "$height2"x"$weght2" -i $i \
-c:v h264_nvenc -b:v "$sdbitrate"K -g 48 -keyint_min 48 -preset fast -profile:v baseline -c:a aac -ar 44100 -ac 2 $i-480p.mp4
# Raname files
for f in $i-1080p.mp4; do mv -v "$f" "${f/.mp4-1080p.mp4/_1080.mp4}"; done;
for f in $i-720p.mp4; do mv -v "$f" "${f/.mp4-720p.mp4/_720.mp4}"; done;
for f in $i-480p.mp4; do mv -v "$f" "${f/.mp4-480p.mp4/_480.mp4}"; done;
# Remove transcoded file from list
sed -i '1d' new_pwd.lst
else
echo "file not found, sleep 10 sec"
fi
clear
done
done