-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertbad.sh
66 lines (56 loc) · 3.9 KB
/
convertbad.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
#!/bin/bash
#Tell the script not to throw errors if the extension is not found
shopt -s nullglob
#for mp4 files specifically in the directories below the cwd
for file in *.{mp4,MP4}; do
#Use ffprobe to determine if the video and audio stream are h264 and aac
VIDEO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")
AUDIO=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")
#set the transcode flags to 0 until they are updated
VTRANSCODE=0
ATRANSCODE=0
#set the transcode flags if transcoding needs to take place
if [ "$VIDEO" != "h264" ]; then
VTRANSCODE=1
fi
if [ "$AUDIO" != "aac" ]; then
ATRANSCODE=1
fi
#if statements for various possibilities of transcoding
if [ "$VTRANSCODE" == 1 ] && [ "$ATRANSCODE" == 1 ]; then
#ffmpeg call to do the appropriate transcoding, automatically answering yes to overwrite files, and because this is already an mp4 file, don't delete it at the end.
ffmpeg -y -i "$file" -map 0:v -map 0:a -map -0:s -c:v libx264 -preset superfast -crf 23 -tune film -b:v 8M -maxrate:v 8M -bufsize:v 8M -c:a aac -strict experimental -ac 2 -b:a 256k -metadata "title=${file%.*}" "${file%.*}.mp4"
elif [ "$VTRANSCODE" == 0 ] && [ "$ATRANSCODE" == 1 ]; then
#the handy file% arguments say, "hey, get that filename, and delete everything after the period" so we can lose the original file extension
ffmpeg -y -i "$file" -map 0:v -map 0:a -map -0:s -c:v copy -c:a aac -strict experimental -ac 2 -b:a 256k -metadata "title=${file%.*}" "${file%.*}.mp4"
elif [ "$VTRANSCODE" == 1 ] && [ "$ATRANSCODE" == 0 ]; then
ffmpeg -y -i "$file" -map 0:v -map 0:a -map -0:s -c:v libx264 -preset superfast -crf 23 -tune film -b:v 8M -maxrate:v 8M -bufsize:v 8M -c:a copy -metadata "title=${file%}" "${file%}.mp4"
#if no transcoding is necessary, skip this file and continue the loop
elif [ "$VTRANSCODE" == 0 ] && [ "$ATRANSCODE" == 0 ]; then
#we're gonna convert it to aac channels 2 because alex is the FUCKING worst
ffmpeg -y -i "$file" -map 0:v -map 0:a -map -0:s -c:v copy -c:a aac -strict experimental -ac 2 -b:a 256k -metadata "title=${file%.*}" "${file%.*}.mp4"
fi
done
#for video files in the directories below the cwd
for file in *.{wmv,WMV,mkv,MKV,avi,AVI,m4v,M4V,ts,TS,mov,MOV}; do
VIDEO=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")
AUDIO=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$file")
VTRANSCODE=0
ATRANSCODE=0
if [ "$VIDEO" != "h264" ]; then
VTRANSCODE=1
fi
if [ "$AUDIO" != "aac" ]; then
ATRANSCODE=1
fi
if [ "$VTRANSCODE" == 1 ] && [ "$ATRANSCODE" == 1 ]; then
##ffmpeg call to do the appropriate transcoding, automatically answering yes to overwrite files, and because this is not already an mp4 file, delete the original file at the end.
ffmpeg -i "$file" -map 0:v -map 0:a -map -0:s -c:v libx264 -preset superfast -crf 23 -tune film -b:v 8M -maxrate:v 8M -bufsize:v 8M -c:a aac -strict experimental -ac 2 -b:a 256k -metadata "title=${file%.*}" "${file%.*}.mp4" && rm "$file"
elif [ "$VTRANSCODE" == 0 ] && [ "$ATRANSCODE" == 1 ]; then
ffmpeg -i "$file" -map 0:v -map 0:a -map -0:s -c:v copy -c:a aac -strict experimental -ac 2 -b:a 256k -metadata "title=${file%.*}" "${file%.*}.mp4" && rm "$file"
elif [ "$VTRANSCODE" == 1 ] && [ "$ATRANSCODE" == 0 ]; then
ffmpeg -i "$file" -map 0:v -map 0:a -map -0:s -c:v libx264 -preset superfast -crf 23 -tune film -b:v 8M -maxrate:v 8M -bufsize:v 8M -c:a copy -metadata "title=${file%}" "${file%}.mp4" && rm "$file"
elif [ "$VTRANSCODE" == 0 ] && [ "$ATRANSCODE" == 0 ]; then
ffmpeg -i "$file" -map 0:v -map 0:a -map -0:s -c:v copy -c:a copy -metadata "title=${file%.*}" "${file%.*}.mp4" && rm "$file"
fi
done