实现 --save-pattern 选项和智能文件名冲突处理 (Fixes #426) #718
Merged
+217
−18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #426 - Implements
--save-patternoption and smart filename collision handling to avoid uninformative.copy.mp4filenames 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-patternOptionUsers 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 identifierExample:
```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:.1920x1080.mp4,.5.0Mbps.mp4,.1920x1080.5.0Mbps.mp4.en.2ch.m4a,.128kbps.m4a,.en.128kbps.m4a.en.srt,.zh-CN.srt.copysuffix only when all metadata-based approaches failExample (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 filenamesHandleFileCollision()- Intelligently resolves filename collisions using stream metadataUpdated download managers:
SimpleDownloadManager.cs- VOD downloadsSimpleLiveRecordManager2.cs- Live stream recordingHTTPLiveRecordManager.cs- HTTP-based live recordingLocalization:
StaticText.cswith translationsDocumentation:
README.mdwith--save-patternoption descriptionChanges Summary
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
Notes
This implementation provides both explicit control (via
--save-pattern) and automatic intelligent handling (via smart collision detection), ensuring users never encounter confusing.copy.mp4filenames again.🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]