Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion config/ytdlp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

// DefaultYtdlpFilenameTemplate is used when ytdlp.filename_template is not set.
const DefaultYtdlpFilenameTemplate = "%(title)s.%(ext)s"
// The title is capped at 200 bytes so names stay under the 255-byte NAME_MAX
// limit, with room for suffixes such as ".f140.m4a.part".
const DefaultYtdlpFilenameTemplate = "%(title).200B.%(ext)s"

type YtdlpConfig struct {
// MaxHeight limits the video resolution by height in pixels (e.g. 1080, 720).
Expand Down
71 changes: 71 additions & 0 deletions core/tasks/ytdlp/output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ytdlp

import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"github.com/krau/SaveAny-Bot/config"
)

// TestDefaultFilenameTemplateLongTitle reproduces "[Errno 36] Filename too long" for
// posts whose caption becomes the title. It needs a local yt-dlp and only
// renders the template from an info JSON, so it never touches the network.
func TestDefaultFilenameTemplateLongTitle(t *testing.T) {
bin, err := exec.LookPath("yt-dlp")
if err != nil {
t.Skip("yt-dlp not installed")
}

cases := []struct {
name string
title string
}{
{name: "ascii", title: strings.Repeat("Cita-cita_terbesar_kita_", 20)},
{name: "multibyte", title: strings.Repeat("آية الكرسي ", 40)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
info := filepath.Join(dir, "info.json")
writeInfoJSON(t, info, tc.title)

out, err := exec.Command(bin, "--ignore-config", "--load-info-json", info,
"-o", filepath.Join(dir, config.DefaultYtdlpFilenameTemplate), "--print", "filename").Output()
if err != nil {
t.Fatalf("yt-dlp: %v", err)
}
name := filepath.Base(strings.TrimSpace(string(out)))
// ".part" and format ids (".f140") are appended later; keep room for them.
if got := len(name + ".f140.part"); got > 255 {
t.Fatalf("filename is %d bytes, want <= 255: %q", got, name)
}
if !strings.HasSuffix(name, ".m4a") {
t.Fatalf("extension lost: %q", name)
}
})
}
}

func writeInfoJSON(t *testing.T, path, title string) {
t.Helper()
raw, err := json.Marshal(map[string]any{
"id": "abc123",
"title": title,
"ext": "m4a",
"extractor": "generic",
"extractor_key": "Generic",
"webpage_url": "https://example.invalid/post",
"url": "https://example.invalid/media.m4a",
"formats": []map[string]any{{"format_id": "0", "url": "https://example.invalid/media.m4a", "ext": "m4a"}},
})
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, raw, 0o600); err != nil {
t.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion docs/content/en/usage/ytdlp.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ For safety, flags that execute programs, load external configuration, or read an

## File names

Downloaded files are named after the video title (`%(title)s.%(ext)s`) and saved into the storage directory you selected. Pass a yt-dlp output template to customize the name:
Downloaded files are named after the video title, cut to 200 bytes so long titles stay within file name limits (`%(title).200B.%(ext)s`), and saved into the storage directory you selected. Pass a yt-dlp output template to customize the name:

```bash
/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
Expand Down
2 changes: 1 addition & 1 deletion docs/content/zh/usage/ytdlp.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ weight: 7

## 文件命名

下载文件默认按视频标题命名(`%(title)s.%(ext)s`),保存到你选择的存储目录。可以传入 yt-dlp 输出模板自定义文件名:
下载文件默认按视频标题命名,标题截断至 200 字节以免超出文件名长度限制(`%(title).200B.%(ext)s`),保存到你选择的存储目录。可以传入 yt-dlp 输出模板自定义文件名:

```bash
/download https://www.youtube.com/watch?v=dQw4w9WgXcQ -o "%(uploader)s - %(title)s.%(ext)s"
Expand Down
2 changes: 1 addition & 1 deletion web/ui/src/settingsFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const settingsGroups: SettingsGroupDef[] = [
path: 'filename_template',
label: 'settings.ytdlp.filenameTemplate',
kind: 'text',
placeholder: '%(title)s.%(ext)s',
placeholder: '%(title).200B.%(ext)s',
},
{
path: 'restrict_filenames',
Expand Down