Skip to content

Conversation

@outhud
Copy link
Contributor

@outhud outhud commented Oct 19, 2025

Description

Fixes #426 - Implements --save-pattern option and smart filename collision handling to avoid uninformative .copy.mp4 filenames when downloading multiple streams.

Problem

When downloading multiple streams of the same type (e.g., 1080p + 720p video) in a single invocation, the second stream would be saved as .copy.mp4, .copy.copy.mp4, etc., making it impossible to identify which file contains which quality.

Solution

1. New --save-pattern Option

Users can now customize output filenames using template variables:

Available variables:

  • <SaveName> - User-specified save name
  • <Resolution> - Video resolution (e.g., 1920x1080)
  • <Bandwidth> - Stream bandwidth/bitrate
  • <Codecs> - Codec information (e.g., avc1.64001f)
  • <Language> - Language code (e.g., en, zh-CN)
  • <MediaType> - VIDEO, AUDIO, or SUBTITLES
  • <Channels> - Audio channel configuration
  • <FrameRate> - Frame rate
  • <VideoRange> - SDR/HDR information
  • <GroupId> - Stream group identifier

Example:
```bash
N_m3u8DL-RE --select-video all --save-pattern "_"

Output: video_1920x1080.mp4, video_1280x720.mp4

```

2. Smart Collision Handling

Even without --save-pattern, the tool now automatically uses stream metadata to generate unique filenames when a collision is detected:

  • Video streams: Appends resolution and/or bandwidth
    • Examples: .1920x1080.mp4, .5.0Mbps.mp4, .1920x1080.5.0Mbps.mp4
  • Audio streams: Appends language, channels, and/or bandwidth
    • Examples: .en.2ch.m4a, .128kbps.m4a, .en.128kbps.m4a
  • Subtitle streams: Appends language
    • Examples: .en.srt, .zh-CN.srt
  • Falls back to .copy suffix only when all metadata-based approaches fail

Example (automatic handling):
```bash
N_m3u8DL-RE --select-video all --save-name "video"

Old behavior: video.mp4, video.copy.mp4 ❌

New behavior: video.mp4, video.1280x720.mp4 ✅

```

Technical Implementation

New utility methods (src/N_m3u8DL-RE/Util/OtherUtil.cs):

  • FormatSavePattern() - Parses template variables and generates filenames
  • HandleFileCollision() - Intelligently resolves filename collisions using stream metadata

Updated download managers:

  • SimpleDownloadManager.cs - VOD downloads
  • SimpleLiveRecordManager2.cs - Live stream recording
  • HTTPLiveRecordManager.cs - HTTP-based live recording

Localization:

  • Full support for 3 languages: English (en-US), Simplified Chinese (zh-CN), Traditional Chinese (zh-TW)
  • Updated StaticText.cs with translations

Documentation:

  • Updated README.md with --save-pattern option description
  • Added detailed "More Help" section with examples

Changes Summary

  • 7 files changed
  • +217 lines added, -18 lines removed
  • 0 compilation errors
  • All existing warnings remain unchanged

Testing

Build & Compilation:
```bash
dotnet build N_m3u8DL-RE.sln -c Debug

Result: Build succeeded, 0 errors

```

Help Output:
```bash
N_m3u8DL-RE --help

--save-pattern option appears with all variables documented

```

Real-world testing: 🔄 Needs community testing with actual multi-quality streams

Example Usage

Download multiple video qualities with custom pattern

```bash
N_m3u8DL-RE "https://example.com/manifest.mpd"
--select-video all
--save-pattern ""
--save-name "video"

Output:

video_1920x1080_5000000.mp4

video_1280x720_2500000.mp4

```

Download multiple audio tracks with languages

```bash
N_m3u8DL-RE "https://example.com/manifest.mpd"
--select-audio all
--save-pattern "_ch"

Output:

en_2ch.m4a

es_2ch.m4a

ja_6ch.m4a

```

Automatic smart collision handling (no pattern needed)

```bash
N_m3u8DL-RE "https://example.com/manifest.mpd"
--select-video "res=1920|res=1280"
--save-name "video"

Output:

video.mp4 (first stream)

video.1280x720.mp4 (second stream, auto-detected)

```

Checklist

  • Code follows project style and conventions
  • All files compile without errors
  • Localization strings added for all supported languages
  • Documentation updated (README.md)
  • Bilingual commit message (Chinese/English)
  • Tested with real multi-quality streams (needs community validation)

Notes

This implementation provides both explicit control (via --save-pattern) and automatic intelligent handling (via smart collision detection), ensuring users never encounter confusing .copy.mp4 filenames again.


🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

新增功能:
1. 实现 --save-pattern 命令行选项
   - 支持使用模板变量自定义输出文件名
   - 可用变量: <SaveName>, <Id>, <Codecs>, <Language>, <Resolution>,
     <Bandwidth>, <MediaType>, <Channels>, <FrameRate>, <VideoRange>, <GroupId>
   - 完整支持三种语言 (en-US, zh-CN, zh-TW)

2. 智能文件名冲突处理
   - 当文件名冲突时,自动使用流元数据生成唯一文件名
   - 视频流: 优先使用分辨率和带宽 (例如: .1920x1080.mp4, .5.0Mbps.mp4)
   - 音频流: 优先使用语言、声道和带宽 (例如: .en.2ch.m4a, .128kbps.m4a)
   - 字幕流: 优先使用语言 (例如: .en.srt)
   - 只在元数据方案失败时才回退到 ".copy" 后缀

技术实现:
- 新增 OtherUtil.FormatSavePattern() - 模板变量解析器
- 新增 OtherUtil.HandleFileCollision() - 智能冲突处理器
- 更新所有下载管理器以支持新功能:
  * SimpleDownloadManager.cs (VOD下载)
  * SimpleLiveRecordManager2.cs (直播录制)
  * HTTPLiveRecordManager.cs (HTTP直播录制)

文档更新:
- README.md: 添加 --save-pattern 选项说明和使用示例
- 添加 More Help 章节详细说明用法

解决问题:
修复 nilaoda#426 - 下载多个相同类型流时的文件名冲突问题
不再生成难以识别的 .copy.mp4, .copy.copy.mp4 文件名

---

Implement --save-pattern option and smart filename collision handling (Fixes nilaoda#426)

New Features:
1. Implement --save-pattern command-line option
   - Support custom output filename patterns with template variables
   - Available variables: <SaveName>, <Id>, <Codecs>, <Language>, <Resolution>,
     <Bandwidth>, <MediaType>, <Channels>, <FrameRate>, <VideoRange>, <GroupId>
   - Full support for 3 languages (en-US, zh-CN, zh-TW)

2. Smart filename collision handling
   - Auto-generate unique filenames using stream metadata on collision
   - Video: prioritize resolution and bandwidth (e.g., .1920x1080.mp4, .5.0Mbps.mp4)
   - Audio: prioritize language, channels, and bandwidth (e.g., .en.2ch.m4a, .128kbps.m4a)
   - Subtitles: prioritize language (e.g., .en.srt)
   - Fall back to ".copy" suffix only when metadata approaches fail

Technical Implementation:
- Added OtherUtil.FormatSavePattern() - template variable parser
- Added OtherUtil.HandleFileCollision() - smart collision handler
- Updated all download managers to support new features:
  * SimpleDownloadManager.cs (VOD downloads)
  * SimpleLiveRecordManager2.cs (Live recording)
  * HTTPLiveRecordManager.cs (HTTP live recording)

Documentation:
- README.md: Added --save-pattern option description and examples
- Added More Help section with detailed usage

Fixes:
Resolves nilaoda#426 - filename collision when downloading multiple streams of the same type
No more unidentifiable .copy.mp4, .copy.copy.mp4 filenames

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@nilaoda
Copy link
Owner

nilaoda commented Oct 23, 2025

感谢您的贡献!等我有空时会仔细审查,确认无误后便会合并。

Thank you for your contribution! I’ll review it carefully when I have time and merge it once everything looks good.

@nilaoda nilaoda merged commit f3a6754 into nilaoda:main Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple streams of one type: avoid 'copy.mp4' in the filename?

2 participants