-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrack
67 lines (58 loc) · 1.66 KB
/
extrack
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
#!/bin/bash
# --------------------- extrack ---------------------
# A script for extracting a specific track id from
# mkv files that are supported by ffmpeg.
#
# Requirements:
# - FFmpeg
#
# Usage:
# extrack <File / Directory> <Track ID> <File Format>
#
# Arguments:
# 1. File / Directory - A media file or a directory
# with media files in it to extract from.
# 2. Track ID - mkvinfo ID of the track to extract.
# 3. File Format - File extension to use for the extracted track.
# ---------------------------------------------------
#######################################
# Extract a track from an mkv file.
#
# Arguments:
# #1 - Path to an mkv file
# #2 - mkvinfo Track ID to extract
# #3 - File format to save track as
#
# Outputs:
# Extract the track to a separate file.
#######################################
function ffmpeg_extract
{
result_file_name="$(basename "$1" | sed "s/^\(.*\)\..*$/\1/").${3}"
ffmpeg -hide_banner -v "error" -stats -i "$1" -c copy -map 0:"${2}" "$result_file_name" -y
}
function pring_usage
{
echo "Usage: $(basename "$0") <File / Directory> <Track ID> <File Format>"
}
# Assure a valid amount of command-line arguments was passed
if [[ $# -lt 3 ]]; then
echo "Error: Missing parameters." >&2
pring_usage
exit 1
fi
# Assure file / directory exists
if [[ ! -e "$1" ]]; then
echo "Error: Directory / file \"$1\" not found." >&2
pring_usage
exit 1
fi
# Path parameter ($1) is a directory
if [[ -d "$1" ]]; then
for file in "$1"/*.mkv ; do
ffmpeg_extract "$file" "$2" "$3"
done
# Path parameter ($1) is a file
elif [[ -f "$1" ]]; then
ffmpeg_extract "$1" "$2" "$3"
fi