-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkvtool.go
364 lines (328 loc) · 10.6 KB
/
mkvtool.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/fatih/structs"
"github.com/jedib0t/go-pretty/table"
ParseTorrentName "github.com/middelink/go-parse-torrent-name"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// A friendly chat about Matroska metadata track numbers.
//
// Matroska tracks numbers are confusing. Tracks are stored in the file
// starting at 1 (ONE). Some mkvtoolnix commands such as mkvmerge and
// mkvextract expect tracks to start at offset zero (ZERO), while others like
// mkvpropedit, expect offset 1. Due to this, the following conventions were
// adopted here:
//
// - Tracks are always displayed starting at 0 (as the output of mkvmerge --identify)
// - Any actions using mkvpropedit automatically add one to the track number.
// - Any actions using mkvmerge or mkvextract will use the track number unchanged.
//
// Track Types. See https://www.matroska.org/technical/specs/index.html
const (
typeSubtitle = "subtitles"
)
// trackFileInfo holds information about an exported track file.
type trackFileInfo struct {
language string
fname string
}
// BuildVersion holds the git build number (set by make).
var BuildVersion string
// show lists all tracks in a file.
func show(mkv matroska, showUID bool) {
tab := table.NewWriter()
tab.SetOutputMirror(os.Stdout)
if showUID {
tab.AppendHeader(table.Row{"Number", "UID", "Type", "Name", "Language", "Codec", "Default"})
} else {
tab.AppendHeader(table.Row{"Number", "Type", "Name", "Language", "Codec", "Default"})
}
for _, track := range mkv.Tracks {
// Create a row with the desired columns.
// mkvmerge reports tracks starting at zero, so we add one to match the file.
row := []interface{}{track.ID}
if showUID {
row = append(row, uint64(track.Properties.UID))
}
row = append(row, track.Type, track.Properties.TrackName, track.Properties.Language, track.Codec)
// Make default flag easier to see.
if track.Properties.DefaultTrack {
row = append(row, "<=====")
} else {
row = append(row, "")
}
tab.AppendRow(row)
}
tab.Render()
}
// setdefault resets flagDefault on all subtitle tracks and sets it on the chosen track UID.
func setdefault(mkv matroska, tracknum int, cmd runner) error {
command := []string{
"mkvpropedit",
mkv.FileName,
}
for _, track := range mkv.Tracks {
if track.Type == typeSubtitle {
// mkvpropedit uses base 1 for track (not zero).
command = append(command, "--edit", fmt.Sprintf("track:%d", track.ID+1), "--set", "flag-default=0")
}
}
if err := cmd.run(command[0], command[1:]...); err != nil {
return err
}
return adddefault(mkv, tracknum, cmd)
}
// trackByLanguage returns the track number (base 0) for the first track with
// one of the specified languages. The list of languages works as a priority,
// meaning that languages=["eng","fra"] will first attempt to find a track with
// the English language, and failing that, French. The special language
// "default" will cause tracks without a language code to be selected (Matroska
// has the concept of a "default language", which is usually English -- tracks
// with this language will not have a language code).
//
// The ignore slice contains a list of strings for case-insentive search
// against the track name. If the selected language contains one of the strings
// in this slice, it will be ignored. This is useful to select tracks by
// language while ignoring 'Forced' tracks.
func trackByLanguage(mkv matroska, languages []string, ignore []string) (int, error) {
for _, lang := range languages {
if lang == "default" {
lang = ""
}
for _, track := range mkv.Tracks {
// Match subtitle and language.
if track.Type != typeSubtitle || track.Properties.Language != lang {
continue
}
// Make sure track should not be ignored.
if stringInSlice(track.Properties.TrackName, ignore) {
continue
}
return track.ID, nil
}
}
return 0, fmt.Errorf("no track with language(s): %s", strings.Join(languages, ","))
}
// stringInSlice returns true if a string exists inside a slice of strings.
// Comparison is case insensitive.
func stringInSlice(s string, slc []string) bool {
for _, substr := range slc {
if strings.Contains(strings.ToLower(s), strings.ToLower(substr)) {
return true
}
}
return false
}
// extract extracts a given track into a file.
func extract(mkv matroska, tracknum int, cmd runner) (trackFileInfo, error) {
// Fetch language for the track. Fail if track does not exist.
ok := false
language := ""
for _, track := range mkv.Tracks {
if track.ID == tracknum {
ok = true
language = track.Properties.Language
break
}
}
if !ok {
return trackFileInfo{}, fmt.Errorf("track #%d not found in file %s", tracknum, mkv.FileName)
}
// Extract into a temporary file
tmpfile, err := ioutil.TempFile("", "mkvtool")
if err != nil {
return trackFileInfo{}, err
}
temp := tmpfile.Name()
_ = tmpfile.Close()
command := []string{
"mkvextract",
mkv.FileName,
"tracks",
fmt.Sprintf("%d:%s", tracknum, temp),
}
if err := cmd.run(command[0], command[1:]...); err != nil {
return trackFileInfo{}, err
}
return trackFileInfo{language: language, fname: temp}, nil
}
// submux merges an input file (usually an mkv file) and multiple subtitles into a
// destination, optionally removing all other subtitles from the source.
func submux(infile, outfile string, nosubs bool, cmd runner, subs ...trackFileInfo) error {
cmdline := []string{"mkvmerge", "-o", outfile}
if nosubs {
cmdline = append(cmdline, "-S")
}
cmdline = append(cmdline, infile)
for _, sub := range subs {
cmdline = append(cmdline, "--language", fmt.Sprintf("0:%s", sub.language))
cmdline = append(cmdline, sub.fname)
}
return cmd.run(cmdline[0], cmdline[1:]...)
}
// remux re-multiplexes the input file(s) into the output file. Setting subs to
// false will cause subs not to be copied.
func remux(infiles []string, outfile string, cmd runner, subs bool) error {
cmdline := []string{"mkvmerge"}
if !subs {
cmdline = append(cmdline, "-S")
}
cmdline = append(cmdline, infiles...)
cmdline = append(cmdline, "-o", outfile)
return cmd.run(cmdline[0], cmdline[1:]...)
}
// adddefault adds the default flag to a given track UID.
func adddefault(mkv matroska, tracknum int, cmd runner) error {
for _, track := range mkv.Tracks {
if track.ID == tracknum {
// mkvpropedit uses base 1 for tracks.
return cmd.run("mkvpropedit", mkv.FileName, "--edit", fmt.Sprintf("track:%d", tracknum+1), "--set", "flag-default=1")
}
}
return fmt.Errorf("file %s does not contain track %d", mkv.FileName, tracknum)
}
// rename renames a file according to the "Scene" information in the file.
func rename(mask, fname string, dryrun bool) error {
newname, err := format(fname, mask)
if err != nil {
return err
}
dir, _ := filepath.Split(fname)
newfile := filepath.Join(dir, newname)
fmt.Printf("%s => %s\n", fname, newfile)
if dryrun {
return nil
}
return os.Rename(fname, newfile)
}
// format parses "Scene" information in the file and returns a string formatted
// according to a formatting mask. The mask may contain the following tokens:
//
// %[format]{audio}
// %[format]{codec}
// %[format]{container} (this matches the original extension)
// %[format]{episode}
// %[format]{episodeName}
// %[format]{excess}
// %[format]{extended}
// %[format]{garbage}
// %[format]{group}
// %[format]{hardcoded}
// %[format]{language}
// %[format]{proper}
// %[format]{quality}
// %[format]{region}
// %[format]{repack}
// %[format]{resolution}
// %[format]{season}
// %[format]{title}
// %[format]{website}
// %[format]{widescreen}
// %[format]{year}
//
// Where "format" is a printf style format sizing specification. Complete
// examples:
// - %-02.2{season} - Season formatted as two characters, left padded wth zeroes.
// - %-20{title} - Title truncated to 20 characters
//
// Anything not matching the %[format]{xxxx} construct will be interpreted literally.
//
// Formatting will fail if any element present in the mask cannot be resolved
// (a typical example is asking for episode numbers for movies).
func format(mask, fname string) (string, error) {
// Split the filename so we can work on parts separately.
_, file := filepath.Split(fname)
parsed, err := ParseTorrentName.Parse(file)
if err != nil {
return "", err
}
fields := structs.Map(parsed)
// tags are formatted as %[format]{value}
re, err := regexp.Compile(`%((?:-?[\d]+)?(?:\.\d+)?){([a-z]+)}`)
if err != nil {
return "", err
}
var errlist []string
formatted := re.ReplaceAllStringFunc(mask, func(match string) string {
// Split matched tag into size formatting specifier and tag name.
// Tag must be capitalized to match the keys in the map.
e := re.FindStringSubmatch(match)
sizespec := e[1]
tag := cases.Title(language.English).String(e[2])
if i, ok := fields[tag]; ok {
switch t := i.(type) {
case string:
val := i.(string)
if val == "" {
break
}
// Special case for title: Capitalize
if tag == "Title" {
val = cases.Title(language.English).String(val)
}
return fmt.Sprintf("%"+sizespec+"s", val)
case int:
val := i.(int)
if val <= 0 {
break
}
return fmt.Sprintf("%"+sizespec+"d", i.(int))
default:
errlist = append(errlist, fmt.Sprintf("Internal error: Unknown type %T for %q", match, t))
return "*ERROR*"
}
}
errlist = append(errlist, fmt.Sprintf("Unable to parse data for %s", match))
return "*ERROR*"
})
if len(errlist) != 0 {
return "", fmt.Errorf("%s", strings.Join(errlist, ";"))
}
return formatted, nil
}
// requirements returns nil if all required tools are installed and an error indicating
// the tools missing otherwise.
func requirements() error {
var tools = []string{"mkvextract", "mkvmerge", "mkvpropedit"}
missing := []string{}
for _, t := range tools {
_, err := exec.LookPath(t)
if err != nil {
missing = append(missing, t)
}
}
if len(missing) != 0 {
return fmt.Errorf("required 3rd party tool(s) missing: %s", strings.Join(missing, ","))
}
return nil
}
// mustParseFile parses the MKV file using the JSON output from mkmerge --identify.
// error message in case of problems.
func mustParseFile(fname string) matroska {
var stdout bytes.Buffer
cmd := exec.Command("mkvmerge", "--identify", "-F", "json", fname)
cmd.Stdout = &stdout
err := cmd.Run()
if err != nil {
log.Printf("mkvmerge failed: %v", err)
log.Fatalf("--- Output ---\n%s\n", stdout.String())
}
// Decode JSON.
var mkv matroska
err = json.Unmarshal(stdout.Bytes(), &mkv)
if err != nil {
log.Fatalf("Error decoding JSON output from mkvmerge: %v", err)
}
return mkv
}