Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ func appendFFmpegLiveOutputStreamArgs(args []string, audioOnly bool) []string {
)
}

func twitchVideoDownloadArgs(quality, url, outputPath, configArgs string) []string {
args := []string{
"-f", quality,
url,
"-o", outputPath,
"--merge-output-format", "mp4", "--no-part",
"--no-warnings", "--progress", "--newline", "--no-check-certificate",
// Twitch VOD playlists can change their fMP4 initialization segment after a
// discontinuity. The native HLS downloader rejects a second EXT-X-MAP, while
// ffmpeg handles the new initialization section correctly.
"--hls-prefer-ffmpeg",
}

// User arguments are intentionally last so an explicit downloader preference
// in the configuration can override the default.
for _, arg := range strings.Split(configArgs, ",") {
if strings.TrimSpace(arg) != "" {
args = append(args, arg)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

return args
}

// DownloadTwitchVideo downloads a Twitch video.
func DownloadTwitchVideo(ctx context.Context, video ent.Vod) error {
// Get video channel
Expand Down Expand Up @@ -115,26 +139,13 @@ func DownloadTwitchVideo(ctx context.Context, video ent.Vod) error {
tmpVideoDownloadExt := filepath.Ext(video.TmpVideoDownloadPath)
tmpVideoDownloadPathNoExt := strings.TrimSuffix(video.TmpVideoDownloadPath, tmpVideoDownloadExt)

// Get user arguments from config
configYtDlpArgs := config.Get().Parameters.YtDlpVideo
configYtDlpArgsArr := strings.Split(configYtDlpArgs, ",")

var cmdArgs []string
cmdArgs = append(cmdArgs,
"-f", qualityString,
cmdArgs := twitchVideoDownloadArgs(
qualityString,
url,
"-o", fmt.Sprintf("%s.%%(ext)s", tmpVideoDownloadPathNoExt),
"--merge-output-format", "mp4", "--no-part",
"--no-warnings", "--progress", "--newline", "--no-check-certificate",
fmt.Sprintf("%s.%%(ext)s", tmpVideoDownloadPathNoExt),
config.Get().Parameters.YtDlpVideo,
)

// Sanitize config args before appending
for _, arg := range configYtDlpArgsArr {
if strings.TrimSpace(arg) != "" {
cmdArgs = append(cmdArgs, arg)
}
}

// Create yt-dlp command
cmd, cookieFile, err := ytdlpSvc.CreateCommand(ctx, cmdArgs, true)
defer func() {
Expand Down
29 changes: 29 additions & 0 deletions internal/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ func TestVodArchiveProcessAttributes(t *testing.T) {
}
}

func TestTwitchVideoDownloadArgsPreferFFmpegForHLS(t *testing.T) {
t.Parallel()

args := twitchVideoDownloadArgs(
"best[height=1080]/best",
"https://twitch.tv/videos/2838897713",
"/tmp/video.%(ext)s",
"--fragment-retries,20",
)

ffmpegPreference := -1
customArgs := -1
for i, arg := range args {
switch arg {
case "--hls-prefer-ffmpeg":
ffmpegPreference = i
case "--fragment-retries":
customArgs = i
}
}

if ffmpegPreference == -1 {
t.Fatalf("Twitch VOD arguments do not prefer the ffmpeg HLS downloader: %v", args)
}
if customArgs == -1 || customArgs < ffmpegPreference {
t.Fatalf("configured yt-dlp arguments must follow defaults: %v", args)
}
}

func TestPostProcessVideoFFmpegArgsIncludesTitleMetadata(t *testing.T) {
t.Parallel()

Expand Down
Loading