49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
public class VideoSeparator
|
|
{
|
|
private readonly Regex episodePattern = new Regex(@"E\d{1,3}", RegexOptions.IgnoreCase);
|
|
|
|
public async Task<(List<string> seriesFiles, List<string> movieFiles)> SeparateVideosAsync(List<string> videoFiles)
|
|
{
|
|
int coreCount = Environment.ProcessorCount;
|
|
int totalFiles = videoFiles.Count;
|
|
int chunkSize = (int)Math.Ceiling((double)totalFiles / coreCount);
|
|
|
|
var seriesBag = new ConcurrentBag<string>();
|
|
var movieBag = new ConcurrentBag<string>();
|
|
var tasks = new List<Task>();
|
|
|
|
for (int i = 0; i < coreCount; i++)
|
|
{
|
|
int start = i * chunkSize;
|
|
int end = Math.Min(start + chunkSize, totalFiles);
|
|
|
|
tasks.Add(Task.Run(() =>
|
|
{
|
|
for (int j = start; j < end; j++)
|
|
{
|
|
string path = videoFiles[j];
|
|
string filename = Path.GetFileNameWithoutExtension(path);
|
|
|
|
if (filename == null)
|
|
continue;
|
|
|
|
if (episodePattern.IsMatch(filename))
|
|
seriesBag.Add(path);
|
|
else
|
|
movieBag.Add(path);
|
|
}
|
|
}));
|
|
}
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
return (new List<string>(seriesBag), new List<string>(movieBag));
|
|
}
|
|
} |