-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp4.go
70 lines (61 loc) · 1.68 KB
/
mp4.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
package main
import (
"bytes"
"errors"
"fmt"
"log/slog"
"os"
"strings"
"github.com/Eyevinn/mp4ff/mp4"
)
func tryExtractPSSHFromMP4(b []byte) error {
var box mp4.Box
var err error
m, err := mp4.DecodeFile(bytes.NewReader(b))
if err == nil {
for _, pssh := range m.Moov.Psshs {
inspectPSSHBox(pssh)
}
os.Exit(0)
return nil
}
slog.Debug("unable to parse input as mp4", "err", err.Error())
b = tryBase64(b)
box, err = mp4.DecodeBox(0, bytes.NewReader(b))
if err != nil {
slog.Debug("unable to parse input as mp4 box", "err", err.Error())
return errors.New("unable to parse input as mp4 or mp4 box")
}
if psshBox, ok := box.(*mp4.PsshBox); ok {
inspectPSSHBox(psshBox)
} else {
slog.Debug("box is not a PSSH box")
}
os.Exit(0)
return nil
}
func inspectPSSHBox(pssh *mp4.PsshBox) {
switch strings.ToLower(pssh.SystemID.String()) {
case mp4.UUIDPlayReady:
slog.Debug("detected playready", "system_id", pssh.SystemID.String(), "version", int(pssh.Version))
if err := decodePlayReadyObject(pssh.Data); err != nil {
slog.Debug("failed to decode playready pssh", "err", err)
}
fmt.Fprintln(os.Stderr, "")
case mp4.UUIDWidevine:
slog.Debug("detected widevine", "system_id", pssh.SystemID.String(), "version", int(pssh.Version))
var buf bytes.Buffer
if err := pssh.Encode(&buf); err != nil {
slog.Debug("failed to extract widevine pssh data", "err", err)
return
}
if err := decodeWidevine(buf.Bytes()); err != nil {
slog.Debug("failed to decode widevine pssh", "err", err)
return
}
fmt.Fprintln(os.Stderr, "")
default:
slog.Debug("skipping unknown pssh", "system_id", pssh.SystemID.String(), "version", int(pssh.Version))
return
}
}