This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmkv-transcode-flac
executable file
·124 lines (106 loc) · 3.81 KB
/
mkv-transcode-flac
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
#!/bin/bash
#
# Version 5
#
# (C) 2017 Daniel Faust <hessijames@gmail.com>
#
# A BASH script that transcodes all FLAC tracks of a matroska file to Ogg Vorbis
# while preserving the track's name and language and it's default and forced flags as well as its delay.
#
# Requires mktemp, mkvmerge, mkvextract and oggenc (with FLAC decoder enabled)
#
# Usage:
# chmod +x mkv-transcode-flac
# ./mkv-transcode-flac "My File.mkv"
#
# This will create a new file called "My File [Ogg Vorbis].mkv".
#
# For batch converting all .mkv files in the current directoy
# copy mkv-transcode-flac to a directory in your PATH and execute:
#
# find -iname "*.mkv" -exec mkv-transcode-flac '{}' \;
if [[ "$1" == "" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "-h" ]]; then
echo
echo "Transcode single file:"
echo "mkv-transcode-flac file.mkv"
echo
echo "Transcode multiple files:"
echo "find -iname \"*.mkv\" -exec mkv-transcode-flac '{}' \;"
echo
exit
fi
hash mktemp 2>/dev/null || { echo >&2 "Error: Command mktemp not found"; exit 1; }
hash mkvmerge 2>/dev/null || { echo >&2 "Error: Command mkvmerge not found"; exit 1; }
hash mkvextract 2>/dev/null || { echo >&2 "Error: Command mkvextract not found"; exit 1; }
hash oggenc 2>/dev/null || { echo >&2 "Error: Command oggenc not found"; exit 1; }
file_name=$1
tmp_dir=$(mktemp -d)
trap 'rm -r "$tmp_dir"' EXIT
option_file_name="$tmp_dir/options_file"
IFS='
'
tracks=$(LANG=en_US.utf8 LANGUAGE=en_US.utf8 mkvmerge --identify-verbose "$file_name" | grep -E "(A_FLAC|A_PCM)")
track_ids_string=""
for track in $tracks; do
track_id=""
[[ "$track" =~ Track\ ID\ ([0-9]+) ]] &&
track_id=${BASH_REMATCH[1]}
[[ "$track" =~ language:([a-z]+) ]] &&
language=${BASH_REMATCH[1]}
[[ "$track" =~ track_name:([^ ]+) ]] &&
track_name=${BASH_REMATCH[1]}
[[ "$track" =~ default_track:([0-1]) ]] &&
default_track=${BASH_REMATCH[1]}
[[ "$track" =~ forced_track:([0-1]) ]] &&
forced_track=${BASH_REMATCH[1]}
ext=""
if [[ "$track" =~ A_FLAC ]]; then
ext="flac"
elif [[ "$track" =~ A_PCM ]]; then
ext="pcm"
fi
if [[ "$track_id" == "" ]]; then
continue
fi
mkvextract timecodes_v2 "$file_name" "$track_id:$tmp_dir/$track_id.ts"
mkvextract tracks "$file_name" "$track_id:$tmp_dir/$track_id.$ext"
oggenc --quality=6 -o "$tmp_dir/$track_id.ogg" "$tmp_dir/$track_id.$ext"
rm "$tmp_dir/$track_id.$ext"
track_ids_string="$track_ids_string,$track_id"
delay=$(sed -n "2p" "$tmp_dir/$track_id.ts")
echo "--audio-tracks" >> "$option_file_name"
echo "0" >> "$option_file_name"
if [[ "$track_name" != "" ]]; then
track_name=${track_name/\\s/\ } # replace '\s' with ' '
track_name=${track_name/\\2/\"} # replace '\2' with '"'
track_name=${track_name/\\c/:} # replace '\c' with ':'
track_name=${track_name/\\h/#} # replace '\h' with '#'
# track_name=${track_name/\\\\/\\} # replace '\\' with '\' doesn't work
track_name=$(echo "$track_name" | sed -r 's/(flac|pcm)/Ogg Vorbis/i')
echo "--track-name" >> "$option_file_name"
echo "0:$track_name" >> "$option_file_name"
fi
if [[ "$language" != "" ]]; then
echo "--language" >> "$option_file_name"
echo "0:$language" >> "$option_file_name"
fi
if [[ "$default_track" != "" ]]; then
echo "--default-track" >> "$option_file_name"
echo "0:$default_track" >> "$option_file_name"
fi
if [[ "$forced_track" != "" ]]; then
echo "--forced-track" >> "$option_file_name"
echo "0:$forced_track" >> "$option_file_name"
fi
if [[ $delay != 0 ]]; then
echo "--sync" >> "$option_file_name"
echo "0:$delay" >> "$option_file_name"
fi
echo "$tmp_dir/$track_id.ogg" >> "$option_file_name"
done
track_ids_string=${track_ids_string:1} # remove first comma
if [[ "$track_ids_string" != "" ]]; then
mkvmerge -o "${file_name%.*} [Ogg Vorbis].mkv" --audio-tracks "!$track_ids_string" "$file_name" "@$option_file_name"
else
echo "No flac or pcm stream found, doing nothing"
fi