-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_video.sh
executable file
·173 lines (134 loc) · 6.19 KB
/
process_video.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
#################################################################
# Title: Process Videos
# Author: tscrip <github.com/tscrip>
# Purpose: Crawls through directory looking for media files
# --> if it finds MP4, it moves the file to the completed dir
# --> if it finds MKV, it will attempt to demux with avconv
# --> if it finds *, it "should" kick off a hanbrake conversion
# ------------------------------------------------------------- #
# Change Log
# ------------------------------------------------------------- #
# tscrip - 10/1/15 - Script created
#################################################################
# Help
# ------------------------------------------------------------- #
#
#################################################################
DOWNLOAD_DIRECTORY="/home/download_complete"
CONVERT_COMPLETE="/home/download_complete"
PROCESSED_DIRECTORY="/home/processed"
LOG_FILE="/home/scripts/process_video.log"
# Enables directories with spaces
IFS=$'\n'
# Writing header
printf "###################################\n### Started $(data) ###\n###################################\n"
# Looping throug download directory
for path in `ls "$DOWNLOAD_DIRECTORY/"`
do
# Checking if $path is directory
if [ -d "$DOWNLOAD_DIRECTORY/$path" ]; then
echo "Dir: "$path"" >> $LOG_FILE
# Looping through directory
for file in `ls "$DOWNLOAD_DIRECTORY/$path"`
do
# Checking if file is MP4
if [[ ${file: -4} == ".mp4" || ${file: -4} == ".m4v" ]]; then
# Found MP4 file
echo "MP4: $file" >> $LOG_FILE
# Moving file
mv "$DOWNLOAD_DIRECTORY/$path/$file" "$PROCESSED_DIRECTORY"
# Checking if file is MKV
elif [[ ${file: -4} == ".mkv" ]]; then
# File has MKV extension
echo "MKV: $file" >> $LOG_FILE
# Creating new file name
newFileName=${file%.*}
# Demuxing file
avconv -i "$DOWNLOAD_DIRECTORY/$path/$file" -codec copy "$CONVERT_COMPLETE/$newFileName.mp4"
# Moving demuxed file to processed directory
mv "$DOWNLOAD_DIRECTORY/$path/$newFileName" "$PROCESSED_DIRECTORY"
# Removing MKV after demux
rm "$DOWNLOAD_DIRECTORY/$path/$file"
elif [[ ${file: -4} == ".avi" || ${file: -4} == ".flv" || ${file: -4} == ".wmv" ]]; then
# File has either wmv/flv/avi
echo "WMV/FLV/AVI: $file" >> $LOG_FILE
# Creating new file name
newFileName=${file%.*}
# Moving file to root directory
mv "$DOWNLOAD_DIRECTORY/$path/$file" "$DOWNLOAD_DIRECTORY"
# Converting file via Handbrake
# HandBrakeCLI -i "$DOWNLOAD_DIRECTORY/$path/$file" -o "$CONVERT_COMPLETE/$newFileName.mp4" -e x264 -q 20.0 -r 30 --pfr -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,none -R Auto,Auto -D 0.0,0.0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 -4 -X 1280 -Y 720 --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 3.1
# Checking if new file exists
# if [ -f "$CONVERT_COMPLETE/$newFileName.mp4" ]; then
# File exists
# Removing file
# rm "$DOWNLOAD_DIRECTORY/$path/$file"
# Moving MP4 file
# mv "$CONVERT_COMPLETE/$newFileName.mp4" "$PROCESSED_DIRECTORY"
# else
# File does not exist
# echo "Conversion of $file failed\n"
# fi
else
# File does not have MP4,MKV,AVI,FLV,WMV extension
# Removing file
rm "$DOWNLOAD_DIRECTORY/$path/$file"
echo "Invalid File Type: ${file: 4}" >> $LOG_FILE
fi
done
echo "Removing directory: $DOWNLOAD_DIRECTORY/$path" >> $LOG_FILE
# Deleting directory
rm -rf "$DOWNLOAD_DIRECTORY/$path"
elif [ -f "$DOWNLOAD_DIRECTORY/$path" ]; then
# echo "File: "$DOWNLOAD_DIRECTORY/$path""
# Checking if file is MP4
if [[ ${path: -4} == ".mp4" || ${path: -4} == ".m4v" ]]; then
# Found MP4 file
echo "MP4: $path" >> $LOG_FILE
# Moving file
mv "$DOWNLOAD_DIRECTORY/$path" "$PROCESSED_DIRECTORY"
# Checking if file is MKV
elif [[ ${path: -4} == ".mkv" ]]; then
# File has MKV extension
echo "MKV: $path" >> $LOG_FILE
# Creating new file name
newFileName=${path%.*}
# Demuxing file
avconv -i "$DOWNLOAD_DIRECTORY/$path" -codec copy "$CONVERT_COMPLETE/$newFileName.mp4"
# Moving demuxed file to processed directory
mv "$DOWNLOAD_DIRECTORY/$newFileName" "$PROCESSED_DIRECTORY"
# Removing MKV after demux
rm "$DOWNLOAD_DIRECTORY/$path"
elif [[ ${path: -4} == ".avi" || ${path: -4} == ".flv" || ${path: -4} == ".wmv" ]]; then
# File has either wmv/flv/avi
echo "WMV/FLV/AVI: $path" >> $LOG_FILE
# Creating new file name
newFileName=${path%.*}
# Moving file to root directory
mv "$DOWNLOAD_DIRECTORY/$path" "$DOWNLOAD_DIRECTORY"
# Converting file via Handbrake
# HandBrakeCLI -i "$DOWNLOAD_DIRECTORY/$path" -o "$CONVERT_COMPLETE/$newFileName.mp4" -e x264 -q 20.0 -r 30 --pfr -a 1,1 -E faac,copy:ac3 -B 160,160 -6 dpl2,none -R Auto,Auto -D 0.0,0.0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mp4 -4 -X 1280 -Y 720 --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 3.1
# Checking if new file exists
# if [ -f "$CONVERT_COMPLETE/$newFileName.mp4" ]; then
# File exists
# Removing file
# rm "$DOWNLOAD_DIRECTORY/$path"
# Moving MP4 file
# mv "$CONVERT_COMPLETE/$newFileName.mp4" "$PROCESSED_DIRECTORY"
# else
# File does not exist
# echo "Conversion of $file failed\n"
# fi
else
# File does not have MP4,MKV,AVI,FLV,WMV extension
# Removing file
rm "$DOWNLOAD_DIRECTORY/$path"
echo "Invalid File Type" >> $LOG_FILE
fi
fi
done
# Cleaning up .ignore files from processed directory
rm $PROCESSED_DIRECTORY/*.ignore
# Writing Footer
printf "###################################\n### Finished $(date) ###\n###################################\n"