Skip to content
This repository was archived by the owner on Sep 28, 2020. It is now read-only.

Commit da05fde

Browse files
committed
Remove illegal characters from the video title
1 parent 9108494 commit da05fde

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

YoutubeExtractor/ExampleApplication/ExampleApplication.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
4848
</PropertyGroup>
4949
<ItemGroup>
50+
<Reference Include="System" />
5051
<Reference Include="System.Core" />
5152
</ItemGroup>
5253
<ItemGroup>

YoutubeExtractor/ExampleApplication/Program.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Text.RegularExpressions;
56
using YoutubeExtractor;
67

78
namespace ExampleApplication
@@ -25,7 +26,8 @@ private static void DownloadAudio(IEnumerable<VideoInfo> videoInfos)
2526
*/
2627

2728
var audioDownloader = new AudioDownloader(video,
28-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), video.Title + video.AudioExtension));
29+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
30+
RemoveIllegalPathCharacters(video.Title) + video.AudioExtension));
2931

3032
// Register the progress events. We treat the download progress as 85% of the progress
3133
// and the extraction progress only as 15% of the progress, because the download will
@@ -54,7 +56,8 @@ private static void DownloadVideo(IEnumerable<VideoInfo> videoInfos)
5456
* The second argument is the path to save the video file.
5557
*/
5658
var videoDownloader = new VideoDownloader(video,
57-
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), video.Title + video.VideoExtension));
59+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
60+
RemoveIllegalPathCharacters(video.Title) + video.VideoExtension));
5861

5962
// Register the ProgressChanged event and print the current progress
6063
videoDownloader.DownloadProgressChanged += (sender, args) => Console.WriteLine(args.ProgressPercentage);
@@ -80,5 +83,12 @@ private static void Main()
8083
//DownloadAudio(videoInfos);
8184
DownloadVideo(videoInfos);
8285
}
86+
87+
private static string RemoveIllegalPathCharacters(string path)
88+
{
89+
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
90+
var r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
91+
return r.Replace(path, "");
92+
}
8393
}
8494
}

0 commit comments

Comments
 (0)