-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffcast.sh
executable file
·159 lines (135 loc) · 4.26 KB
/
ffcast.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
#! /bin/bash
# Batch Convert Script by StevenTrux
# The Purpose of this Script is to batch convert any video file to mp4 or mkv format for chromecast compatibility
# this script only convert necessary tracks if the video is already
# in H.264 format it won't convert it saving your time!
# Put all video files need to be converted in a folder!
# the name of files must not have " " Space!
# Rename the File if contain space
# Variable used:
# outmode should be mp4 or mkv
# sourcedir is the directory where to be converted videos are
# indir is the directory where converted video will be created
# usage:
#########################
# cast.sh mp4 /home/user/divx /home/user/chromecastvideos
# or
# cast.sh mkv /home/user/divx /home/user/chromecastvideos
#########################
# working mode
outmode=$1
# check output mode
if [[ $outmode ]]; then
if [ $outmode = "mp4" ] || [ $outmode = "mkv" ]
then
echo "WORKING MODE $outmode"
else
echo "$outmode is NOT a Correct target format. You need to set an output format! like cast.sh mp4 xxxx or cast.sh mkv xxxx"
exit
fi
else
echo "Working mode is missing. You should set a correct target format like mp4 or mkv"
exit
fi
# Source dir
sourcedir=$2
if [[ $sourcedir ]]; then
echo "Using $sourcedir as Input Folder"
else
echo "Error: Check if you have set an input folder"
exit
fi
# Target dir
indir=$3
if [[ $indir ]]; then
if mkdir -p $indir/castable
then
echo "Using $indir/castable as Output Folder"
else
echo "Error: Check if you have the rights to write in $indir"
exit
fi
else
echo "Error: Check if you have set an output folder"
exit
fi
# set format
if [ $outmode=mp4 ]
then
outformat=mp4
else
outformat=matroska
fi
# Check FFMPEG Installation
if ffmpeg -formats > /dev/null 2>&1
then
ffversion=`ffmpeg -version 2> /dev/null | grep ffmpeg | sed -n 's/ffmpeg\s//p'`
echo "Your ffmpeg verson is $ffversion"
else
echo "ERROR: You need ffmpeg installed with x264 and libfdk_aac encoder"
exit
fi
if ffmpeg -formats 2> /dev/null | grep "E mp4" > /dev/null
then
echo "Check mp4 container format ... OK"
else
echo "Check mp4 container format ... NOK"
exit
fi
if ffmpeg -formats 2> /dev/null | grep "E matroska" > /dev/null
then
echo "Check mkv container format ... OK"
else
echo "Check mkv container format ... NOK"
exit
fi
if ffmpeg -codecs 2> /dev/null | grep "libfdk_aac" > /dev/null
then
echo "Check AAC Audio Encoder ... OK"
else
echo "Check AAC Audio Encoder ... NOK"
exit
fi
if ffmpeg -codecs 2> /dev/null | grep "libx264" > /dev/null
then
echo "Check x264 the free H.264 Video Encoder ... OK"
else
echo "Check x264 the free H.264 Video Encoder ... NOK"
exit
fi
echo "Your FFMpeg is OK Entering File Processing"
################################################################
cd "$sourcedir"
for filelist in `ls`
do
if ffmpeg -i $filelist 2>&1 | grep 'Invalid data found' #check if it's video file
then
echo "ERROR File $filelist is NOT A VIDEO FILE can be converted!"
continue
fi
if ffmpeg -i $filelist 2>&1 | grep Video: | grep h264 #check video codec
then
vcodec=copy
else
vcodec=libx264
fi
if ffmpeg -i $filelist 2>&1 | grep Video: | grep "High 10" #10 bit H.264 can't be played by Hardware.
then
vcodec=libx264
fi
if [ ffmpeg -i $filelist 2>&1 | grep Audio: | grep aac ] || [ ffmpeg -i $filelist 2>&1 | grep Audio: | grep mp3 ] #check audio codec
then
acodec=copy
else
acodec=libfdk_aac
fi
echo "Converting $filelist"
echo "Video codec: $vcodec Audio codec: $acodec Container: $outformat"
# using ffmpeg for real converting
echo "ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 192k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 $indir/castable/$filelist.$outmode"
ffmpeg -i $filelist -y -f $outformat -acodec $acodec -ab 192k -ac 2 -absf aac_adtstoasc -async 1 -vcodec $vcodec -vsync 0 -profile:v main -level 3.1 -qmax 22 -qmin 20 -x264opts no-cabac:ref=2 -threads 0 $indir/castable/$filelist.$outmode
done
echo ALL Processed!
###################
echo "DONE, your video files are chromecast ready"
exit