Skip to content

Commit ea740e1

Browse files
committed
improve opus encoding handling
1 parent 93b7ac1 commit ea740e1

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

internal/ytworker/download.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ var (
4141
// capture progress output e.g '100 of 10000000 / 10000000 eta 30'
4242
ytProgressRe = regexp.MustCompile(`([\d]+) of ([\dNA]+) / ([\d.NA]+) eta ([\d]+)`)
4343

44-
noReencodeSites = []string{"youtube.com", "twitter.com", "rumble.com"}
45-
4644
// filename sanitization
4745
// swap specific special characters
4846
filenameReplacer = strings.NewReplacer(
@@ -68,6 +66,7 @@ type YTInfo struct {
6866
FileSize int64
6967
Extension string `json:"ext"`
7068
SponsorBlockChapters []interface{} `json:"sponsorblock_chapters"`
69+
AudioCodec string `json:"acodec"`
7170
}
7271
type Info struct {
7372
Id int64
@@ -200,14 +199,6 @@ func (yt *Download) Work(j *jobs.Job) {
200199

201200
func (yt *Download) download(ctx context.Context, id int64, outCh chan<- util.Msg, url *url.URL) error {
202201

203-
forceOpus := true
204-
for _, h := range noReencodeSites {
205-
if strings.Contains(url.Host, h) {
206-
forceOpus = false
207-
break
208-
}
209-
}
210-
211202
// filename is md5 sum of URL
212203
urlSum := md5.Sum([]byte(url.String()))
213204
diskFileNameTmp := filepath.Join(yt.webRoot, yt.outPath, "t", "ytdl-"+fmt.Sprintf("%x", urlSum))
@@ -247,13 +238,14 @@ func (yt *Download) download(ctx context.Context, id int64, outCh chan<- util.Ms
247238
"--sponsorblock-remove", yt.sponsorBlockCats,
248239
}...)
249240
}
250-
if forceOpus {
251-
args = append(args, []string{
252-
"--audio-format", "opus",
253-
"--audio-quality", "32K",
254-
// "--postprocessor-args", `ExtractAudio:-compression_level 0`, // fastest, lowest quality compression
255-
}...)
256-
}
241+
args = append(args, []string{
242+
// re-encode mp3 to opus, leave opus as-is, otherwise remux to m4a (re-encode to aac)
243+
"--audio-format", "mp3>opus/opus>opus/m4a",
244+
// Use 32K bitrate.
245+
// This only applies to mp3>opus conversion. Other input formats will retain original bitrate.
246+
"--audio-quality", "32K",
247+
// "--postprocessor-args", `ExtractAudio:-compression_level 0`, // fastest, lowest quality compression
248+
}...)
257249
args = append(args, url.String())
258250

259251
slog.Info("Running command", "command", append([]string{yt.ytCmd}, args...))
@@ -316,8 +308,14 @@ func (yt *Download) download(ctx context.Context, id int64, outCh chan<- util.Ms
316308
m := util.Msg{Key: KeyInfo, Value: info}
317309
outCh <- m
318310

311+
opusEncode := false
312+
319313
// output size of opus file as it gets written
320-
if forceOpus {
314+
if ytInfo.AudioCodec == "mp3" {
315+
opusEncode = true
316+
}
317+
318+
if opusEncode {
321319
go func() {
322320
err := getOpusFileSize(ctx, id, info, outCh, diskFileNameTmp+".opus", yt.outPath)
323321
if err != nil {
@@ -411,7 +409,7 @@ loop:
411409
}
412410
finalFileName := finalFileNameNoExt + ext
413411

414-
if forceOpus {
412+
if opusEncode {
415413
fi, err := os.Stat(diskFileNameTmp2)
416414
if err != nil {
417415
return err
@@ -432,8 +430,8 @@ loop:
432430
}
433431

434432
info.DownloadURL = filepath.Join(yt.outPath, filepath.Base(finalFileName))
435-
// don't send link for forceOpus as that's handled in getOpusFileSize goroutine
436-
if !forceOpus {
433+
// don't send link for opusEncode as that's handled in getOpusFileSize goroutine
434+
if !opusEncode {
437435
m := util.Msg{Key: KeyLinkStream, Value: info}
438436
outCh <- m
439437
}

0 commit comments

Comments
 (0)