forked from BrettSheleski/comchap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscodeToMp4.sh
executable file
·132 lines (111 loc) · 4.64 KB
/
transcodeToMp4.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
#!/usr/bin/env bash
ldPath=${LD_LIBRARY_PATH}
unset LD_LIBRARY_PATH
exitcode=0
ffmpegPath="ffmpeg"
ffprobePath="ffprobe"
command -v $ffmpegPath >/dev/null 2>&1 || { echo >&2 "I require $ffmpegPath but it's not installed. Aborting."; exit 1; }
command -v $ffprobePath >/dev/null 2>&1 || { echo >&2 "I require $ffprobePath but it's not installed. Aborting."; exit 1; }
command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq but it's not installed. Aborting."; exit 1; }
command -v bc >/dev/null 2>&1 || { echo >&2 "I require bc but it's not installed. Aborting."; exit 1; }
if [[ $# -lt 1 ]]; then
exename=$(basename "$0")
echo "Converts ts or mkv to mp4 keeping subtitles and all audio, audio is converted (mp2->acc, ac3 -> acc)"
echo ""
echo "Usage: $exename infile"
echo "Options:"
echo " --dry-run Do not convert, only print command"
echo " --start-second Provide start point in seconds"
echo " --end-second Povide end point in seconds (NOT duration, seconds from start of original source)"
echo " --start-time Time in format 00:00:00"
echo " --end-time Time in format 00:00:00"
echo " --h264Preset Change preset from 'ultrafast' to something you like"
echo " --duration Time in format 00:00:00"
echo "Output: infile.mp4"
exit 1
fi
dryrun=false
startsecond=0
endfsecond=0
duration=0
h264Preset=medium
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
--dry-run)
dryrun=true
shift
;;
--start-second=*)
startsecond="${key#*=}"
shift
;;
--end-second=*)
endsecond="${key#*=}"
shift
;;
--start-time=*)
startsecond=$(echo "${key#*=}" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
shift
;;
--end-time=*)
endsecond=$(echo "${key#*=}" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
shift
;;
--duration=*)
endsecond=0
duration=$(echo "${key#*=}" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }')
shift
;;
--h264Preset=*)
h264Preset="${key#*=}"
shift
;;
*)
break
;;
esac
done
filename=$1
resolution=`$ffprobePath -v error -select_streams v:0 -show_entries stream=width,height -of json=c=0 "$filename" | jq -r '.streams[] | "\(.width)x\(.height)"'`
numberOfAudioStreams=`$ffprobePath -v error -select_streams a -show_entries stream=index -of json=c=0 "$filename" | jq -r '.streams | length'`
numberOfSubtitleStreams=`$ffprobePath -v error -select_streams s -show_entries stream=index -of json=c=0 "$filename" | jq -r '.streams | length'`
audio=""
subtitle=""
for audioStream in $(seq 1 $numberOfAudioStreams)
do
internalASNB=$(($audioStream-1))
streamCodec=`$ffprobePath -v error -select_streams a:$internalASNB -show_entries stream=codec_name -of json=c=0 "$filename" | jq -r '.streams[] | "\(.codec_name)"'`
if [ "$streamCodec" == "ac3" ] || [ "$streamCodec" == "mp2" ]; then
audio="$audio -acodec aac -strict experimental -map a:$internalASNB"
fi
done
for subtitleStream in $(seq 1 $numberOfSubtitleStreams)
do
internalSubtitleStream=$(($subtitleStream-1))
streamCodec=`$ffprobePath -v error -select_streams s:$internalSubtitleStream -show_entries stream=codec_name -of json=c=0 "$filename" | jq -r '.streams[] | "\(.codec_name)"'`
echo "$ffprobePath -v error -select_streams s:$internalSubtitleStream -show_entries stream=codec_name -of json=c=0 \"$filename\" | jq -r '.streams[] | \"\(.codec_name)\"'"
echo "$streamCodec"
if [ "$streamCodec" == "srt" ]; then
subtitle="$subtitle -scodec mov_text -map s:$internalSubtitleStream"
elif [ "$streamCodec" == "dvbsub" ]; then
subtitle="$subtitle -scodec dvdsub -map s:$internalSubtitleStream -s $resolution"
fi
echo "$subtitle"
done
if [ $(echo "$duration == 0"|bc) -eq 1 ]; then
duration=`echo "$endsecond" "$startsecond" | awk '{printf "%f", $1 - $2}'`
fi
startEnd=""
if [ $(echo "$duration > 0"|bc) -eq 1 ]; then
startEnd="-ss $startsecond -t $duration"
fi
echo "$ffmpegPath -i \"$filename\" -f ffmetadata -y \"$filename.txt\""
echo "$ffmpegPath -canvas_size $resolution -threads auto $startEnd -i \"$filename\" -i \"$filename.txt\" -map_metadata 1 -vcodec .... -map v:0 -preset slow ..... -profile:v high -level 41 $audio $subtitle -y \"$filename.mp4\""
if ! $dryrun; then
$ffmpegPath -i "$filename" -f ffmetadata -y "$filename.txt"
$ffmpegPath -init_hw_device vaapi=foo:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device foo -canvas_size $resolution $startEnd -i "$filename" -i "$filename.txt" -map_metadata 1 -filter_hw_device foo -vf 'deinterlace_vaapi,hwupload' -vcodec h264_vaapi -map v:0 -preset slow -profile:v high -level 4.1 -qp 22 $audio $subtitle -y "$filename.mp4"
fi
echo "------------"
echo ""