Skip to content

Commit ffd6614

Browse files
committed
feat(cmd): add gonictranscode command
1 parent 3cfbbdf commit ffd6614

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

cmd/gonictranscode/gonictranscode.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//nolint:gochecknoglobals,gochecknoinits
2+
package main
3+
4+
import (
5+
"context"
6+
"log"
7+
"os"
8+
"strconv"
9+
"strings"
10+
"time"
11+
12+
"go.senan.xyz/gonic/countrw"
13+
"go.senan.xyz/gonic/server/scanner/tags"
14+
"go.senan.xyz/gonic/server/transcode"
15+
)
16+
17+
var args = os.Args
18+
19+
const (
20+
bin = iota
21+
prof
22+
bitrate
23+
seek
24+
path
25+
argc
26+
)
27+
28+
func main() {
29+
if len(args) != argc {
30+
log.Fatalf("usage %s <profile> <bitrate kbps|0> <seek s|0> <path>", args[bin])
31+
}
32+
33+
profile, ok := transcode.UserProfiles[args[prof]]
34+
if !ok {
35+
log.Fatalf("can't find profile %q in %s", prof, strings.Join(profileNames, ", "))
36+
}
37+
38+
if bitrate, _ := strconv.Atoi(args[bitrate]); bitrate > 0 {
39+
profile = transcode.WithBitrate(profile, transcode.BitRate(bitrate))
40+
}
41+
if seek, _ := strconv.Atoi(args[seek]); seek > 0 {
42+
profile = transcode.WithSeek(profile, time.Duration(seek)*time.Second)
43+
}
44+
45+
var tr tags.TagReader
46+
trags, err := tr.Read(args[path])
47+
if err != nil {
48+
log.Fatalf("error reading tags: %v", err)
49+
}
50+
51+
w := countrw.NewCountWriter(os.Stdout)
52+
log.Printf("size guessed: %12dB", transcode.GuessExpectedSize(profile, time.Duration(trags.Length())*time.Second))
53+
defer func() { log.Printf("size actual: %12dB", w.Count()) }()
54+
55+
t := transcode.NewFFmpegTranscoder()
56+
if err := t.Transcode(context.Background(), profile, args[path], w); err != nil {
57+
log.Panicf("error transcoding: %v", err)
58+
}
59+
}
60+
61+
var profileNames []string
62+
63+
func init() {
64+
for name := range transcode.UserProfiles {
65+
profileNames = append(profileNames, name)
66+
}
67+
}

0 commit comments

Comments
 (0)