-
Notifications
You must be signed in to change notification settings - Fork 2
/
ffprobe.go
76 lines (68 loc) · 1.54 KB
/
ffprobe.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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
)
type ffprobeTags struct {
Title string
Artist string
Show string
Description string
}
type ffprobe struct {
Streams []struct {
Tags ffprobeTags
}
Format struct {
Tags ffprobeTags
}
}
func runFFprobe(ctx context.Context, ffprobeCmd, filename string, timeout time.Duration) (*ffprobe, error) {
ffCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
args := []string{"-i", filename,
"-print_format", "json",
// "-v", "quiet",
"-show_streams",
"-show_format",
}
// fmt.Printf("ffprobe cmd %s, filename %s, args %v\n", ffprobeCmd, filename, args)
out, err := RunCommand(ffCtx, ffprobeCmd, args...)
if err != nil {
return nil, fmt.Errorf("error running ffprobe: '%w'", err)
}
ff := &ffprobe{}
err = json.Unmarshal(out, ff)
if err != nil {
return nil, err
}
return ff, nil
}
func titleArtistDescription(ff *ffprobe) (title, artist, description string) {
if ff.Format.Tags.Title != "" {
title = ff.Format.Tags.Title
}
if ff.Format.Tags.Artist != "" {
artist = ff.Format.Tags.Artist
}
if ff.Format.Tags.Description != "" {
description = ff.Format.Tags.Description
}
if title == "" && len(ff.Streams) > 0 {
title = ff.Streams[0].Tags.Title
artist = ff.Streams[0].Tags.Artist
if artist == "" && ff.Streams[0].Tags.Show != "" {
artist = ff.Streams[0].Tags.Show
}
description = ff.Streams[0].Tags.Description
}
if title == "" {
title = "unknown title"
}
if artist == "" {
artist = "unknown artist"
}
return
}