Add method to locate subtitle files for video

A method 'TryLocateSubtitleFile' was implemented in FileUtil. It attempts to locate matching subtitle files for video files in known directories. The method was integrated into 'AddInputFile' of 'GenerateVideoWithHardSubs' and simplifies the earlier implementation for finding subtitles. This change will make the process of matching video files to subtitles more efficient and cleaner.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-04-29 16:09:24 +01:00
parent 7b15d11cc1
commit 7a96e1c40a
2 changed files with 73 additions and 42 deletions

View File

@ -6,6 +6,7 @@ using Nikse.SubtitleEdit.Core.VobSub;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@ -707,5 +708,57 @@ namespace Nikse.SubtitleEdit.Core.Common
return false;
}
public static string TryLocateSubtitleFile(string path, string videoFileNameNoExtension)
{
// search in these directories
var knownSubtitleDirectories = new[]
{
path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles")
};
foreach (var knownSubtitleDirectory in knownSubtitleDirectories)
{
var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileNameNoExtension);
if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory))
{
return subtitleFileFromKnownDirectory;
}
}
return string.Empty;
string LocateSubtitle(string localPath, in string localVideoFileName)
{
if (!Directory.Exists(localPath))
{
return string.Empty;
}
// try to locate subtitle file that has the same name as the video file
var defaultSubtitles = new[]
{
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".ass")),
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".srt"))
};
foreach (var defaultSubtitle in defaultSubtitles)
{
if (File.Exists(defaultSubtitle))
{
return defaultSubtitle;
}
}
var assEnumerable = Directory.EnumerateFiles(localPath, "*.ass", SearchOption.TopDirectoryOnly);
var subripEnumerable = Directory.EnumerateFiles(localPath, "*.srt", SearchOption.TopDirectoryOnly);
var subtitleFile = assEnumerable.Concat(subripEnumerable).FirstOrDefault();
// subtitle file found in top directory
if (!string.IsNullOrEmpty(subtitleFile))
{
return subtitleFile;
}
return string.Empty;
}
}
}
}

View File

@ -2131,54 +2131,32 @@ namespace Nikse.SubtitleEdit.Forms
}
}
private void AddInputFile(string fileName)
private void AddInputFile(string videoFileName)
{
if (string.IsNullOrEmpty(fileName))
if (string.IsNullOrEmpty(videoFileName))
{
return;
}
var ext = Path.GetExtension(fileName).ToLowerInvariant();
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(fileName))
var ext = Path.GetExtension(videoFileName).ToLowerInvariant();
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName))
{
var item = new BatchVideoAndSub();
item.VideoFileName = fileName;
item.VideoFileSizeInBytes = new FileInfo(fileName).Length;
item.VideoFileName = videoFileName;
item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length;
var path = Path.GetDirectoryName(fileName);
var fileNameNoExt = Path.GetFileNameWithoutExtension(fileName);
var subFileName = Path.ChangeExtension(fileName, ".ass");
var path = Path.GetDirectoryName(videoFileName);
var fileNameNoExt = Path.GetFileNameWithoutExtension(videoFileName);
if (!File.Exists(subFileName))
// try locate subtitle file for the input vide file
var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName);
if (File.Exists(subtitleFile))
{
subFileName = Path.ChangeExtension(fileName, ".srt");
item.SubtitleFileName = subtitleFile;
item.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length;
}
if (!File.Exists(subFileName))
{
var files = Directory.GetFiles(path, fileNameNoExt + "*.ass");
if (files.Length > 0)
{
subFileName = files[0];
}
}
if (!File.Exists(subFileName))
{
var files = Directory.GetFiles(path, fileNameNoExt + "*.srt");
if (files.Length > 0)
{
subFileName = files[0];
}
}
if (File.Exists(subFileName))
{
item.SubtitleFileName = subFileName;
item.SubtitleFileFileSizeInBytes = new FileInfo(subFileName).Length;
}
var mediaInfo = FfmpegMediaInfo.Parse(fileName);
var mediaInfo = FfmpegMediaInfo.Parse(videoFileName);
int width;
int height;
if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0)
@ -2189,18 +2167,18 @@ namespace Nikse.SubtitleEdit.Forms
else
{
var vInfo = new VideoInfo { Success = false };
if (fileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
{
vInfo = QuartsPlayer.GetVideoInfo(fileName);
vInfo = QuartsPlayer.GetVideoInfo(videoFileName);
if (!vInfo.Success)
{
vInfo = LibMpvDynamic.GetVideoInfo(fileName);
vInfo = LibMpvDynamic.GetVideoInfo(videoFileName);
}
}
if (!vInfo.Success)
{
vInfo = UiUtil.GetVideoInfo(fileName);
vInfo = UiUtil.GetVideoInfo(videoFileName);
}
width = vInfo.Width;
@ -2209,11 +2187,11 @@ namespace Nikse.SubtitleEdit.Forms
if (width == 0 || height == 0)
{
SeLogger.Error("Skipping burn-in file with no video: " + fileName);
SeLogger.Error("Skipping burn-in file with no video: " + videoFileName);
return; // skip audio or damaged files
}
var listViewItem = new ListViewItem(fileName);
var listViewItem = new ListViewItem(videoFileName);
listViewItem.Tag = item;
listViewItem.SubItems.Add($"{width}x{height}");
var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes);