Refactor video and subtitle batch creation code

Code for creating and validating batch video and subtitle entities has been extracted into separate methods, improving the readability of the main method. These changes also include a rectification in the logic for handling invalid video dimensions.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-04-30 12:35:53 +01:00
parent 3d24cdcbb6
commit 93c9e9947a

View File

@ -2141,66 +2141,77 @@ namespace Nikse.SubtitleEdit.Forms
var ext = Path.GetExtension(videoFileName).ToLowerInvariant(); var ext = Path.GetExtension(videoFileName).ToLowerInvariant();
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName)) if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName))
{ {
var item = new BatchVideoAndSub(); var videoDimension = GetVideoDimension(videoFileName);
item.VideoFileName = videoFileName; if (!videoDimension.IsValid())
item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length;
var path = Path.GetDirectoryName(videoFileName);
// try to locate subtitle file for the input vide file
var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName);
if (File.Exists(subtitleFile))
{ {
item.SubtitleFileName = subtitleFile; return;
item.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length;
} }
var mediaInfo = FfmpegMediaInfo.Parse(videoFileName); var batchVideoAndSub = CreateBatchVideoAndSub(videoFileName);
int width; var listViewItem = new ListViewItem(videoFileName) { Tag = batchVideoAndSub };
int height; listViewItem.SubItems.Add(videoDimension.ToString());
if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0) var s = Utilities.FormatBytesToDisplayFileSize(batchVideoAndSub.VideoFileSizeInBytes);
{
width = mediaInfo.VideoWidth;
height = mediaInfo.VideoHeight;
}
else
{
var vInfo = new VideoInfo { Success = false };
if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
{
vInfo = QuartsPlayer.GetVideoInfo(videoFileName);
if (!vInfo.Success)
{
vInfo = LibMpvDynamic.GetVideoInfo(videoFileName);
}
}
if (!vInfo.Success)
{
vInfo = UiUtil.GetVideoInfo(videoFileName);
}
width = vInfo.Width;
height = vInfo.Height;
}
if (width == 0 || height == 0)
{
SeLogger.Error("Skipping burn-in file with no video: " + videoFileName);
return; // skip audio or damaged files
}
var listViewItem = new ListViewItem(videoFileName);
listViewItem.Tag = item;
listViewItem.SubItems.Add($"{width}x{height}");
var s = Utilities.FormatBytesToDisplayFileSize(item.VideoFileSizeInBytes);
listViewItem.SubItems.Add(s); listViewItem.SubItems.Add(s);
listViewItem.SubItems.Add(Path.GetFileName(item.SubtitleFileName)); listViewItem.SubItems.Add(Path.GetFileName(batchVideoAndSub.SubtitleFileName));
listViewItem.SubItems.Add(string.Empty); listViewItem.SubItems.Add(string.Empty);
listViewBatch.Items.Add(listViewItem); listViewBatch.Items.Add(listViewItem);
_batchVideoAndSubList.Add(item); _batchVideoAndSubList.Add(batchVideoAndSub);
} }
} }
private BatchVideoAndSub CreateBatchVideoAndSub(string videoFileName)
{
var batchVideoAndSub = new BatchVideoAndSub
{
VideoFileName = videoFileName,
VideoFileSizeInBytes = new FileInfo(videoFileName).Length
};
var path = Path.GetDirectoryName(videoFileName);
// try to locate subtitle file for the input vide file
var subtitleFile = FileUtil.TryLocateSubtitleFile(path, videoFileName);
if (File.Exists(subtitleFile))
{
batchVideoAndSub.SubtitleFileName = subtitleFile;
batchVideoAndSub.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length;
}
return batchVideoAndSub;
}
private Dimension GetVideoDimension(string videoFileName)
{
var mediaInfo = FfmpegMediaInfo.Parse(videoFileName);
if (mediaInfo.Dimension.IsValid())
{
return mediaInfo.Dimension;
}
var vInfo = new VideoInfo { Success = false };
if (videoFileName.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))
{
vInfo = QuartsPlayer.GetVideoInfo(videoFileName);
if (!vInfo.Success)
{
vInfo = LibMpvDynamic.GetVideoInfo(videoFileName);
}
}
if (!vInfo.Success)
{
vInfo = UiUtil.GetVideoInfo(videoFileName);
}
if (vInfo.Width == 0 || vInfo.Height == 0)
{
SeLogger.Error("Skipping burn-in file with no video: " + videoFileName);
// return; // skip audio or damaged files
return new Dimension();
}
return new Dimension(vInfo.Height, vInfo.Width);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{ {
var indices = new List<int>(); var indices = new List<int>();