mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-25 04:33:04 +01:00
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:
parent
7b15d11cc1
commit
7a96e1c40a
@ -6,6 +6,7 @@ using Nikse.SubtitleEdit.Core.VobSub;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
@ -707,5 +708,57 @@ namespace Nikse.SubtitleEdit.Core.Common
|
|||||||
|
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ext = Path.GetExtension(fileName).ToLowerInvariant();
|
var ext = Path.GetExtension(videoFileName).ToLowerInvariant();
|
||||||
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(fileName))
|
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName))
|
||||||
{
|
{
|
||||||
var item = new BatchVideoAndSub();
|
var item = new BatchVideoAndSub();
|
||||||
item.VideoFileName = fileName;
|
item.VideoFileName = videoFileName;
|
||||||
item.VideoFileSizeInBytes = new FileInfo(fileName).Length;
|
item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length;
|
||||||
|
|
||||||
var path = Path.GetDirectoryName(fileName);
|
var path = Path.GetDirectoryName(videoFileName);
|
||||||
var fileNameNoExt = Path.GetFileNameWithoutExtension(fileName);
|
var fileNameNoExt = Path.GetFileNameWithoutExtension(videoFileName);
|
||||||
var subFileName = Path.ChangeExtension(fileName, ".ass");
|
|
||||||
|
|
||||||
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 mediaInfo = FfmpegMediaInfo.Parse(videoFileName);
|
||||||
{
|
|
||||||
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);
|
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0)
|
if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0)
|
||||||
@ -2189,18 +2167,18 @@ namespace Nikse.SubtitleEdit.Forms
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var vInfo = new VideoInfo { Success = false };
|
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)
|
if (!vInfo.Success)
|
||||||
{
|
{
|
||||||
vInfo = LibMpvDynamic.GetVideoInfo(fileName);
|
vInfo = LibMpvDynamic.GetVideoInfo(videoFileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!vInfo.Success)
|
if (!vInfo.Success)
|
||||||
{
|
{
|
||||||
vInfo = UiUtil.GetVideoInfo(fileName);
|
vInfo = UiUtil.GetVideoInfo(videoFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
width = vInfo.Width;
|
width = vInfo.Width;
|
||||||
@ -2209,11 +2187,11 @@ namespace Nikse.SubtitleEdit.Forms
|
|||||||
|
|
||||||
if (width == 0 || height == 0)
|
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
|
return; // skip audio or damaged files
|
||||||
}
|
}
|
||||||
|
|
||||||
var listViewItem = new ListViewItem(fileName);
|
var listViewItem = new ListViewItem(videoFileName);
|
||||||
listViewItem.Tag = item;
|
listViewItem.Tag = item;
|
||||||
listViewItem.SubItems.Add($"{width}x{height}");
|
listViewItem.SubItems.Add($"{width}x{height}");
|
||||||
var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes);
|
var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes);
|
||||||
|
Loading…
Reference in New Issue
Block a user