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,29 +2141,52 @@ namespace Nikse.SubtitleEdit.Forms
var ext = Path.GetExtension(videoFileName).ToLowerInvariant();
if ((Utilities.AudioFileExtensions.Contains(ext) || Utilities.VideoFileExtensions.Contains(ext)) && File.Exists(videoFileName))
{
var item = new BatchVideoAndSub();
item.VideoFileName = videoFileName;
item.VideoFileSizeInBytes = new FileInfo(videoFileName).Length;
var videoDimension = GetVideoDimension(videoFileName);
if (!videoDimension.IsValid())
{
return;
}
var batchVideoAndSub = CreateBatchVideoAndSub(videoFileName);
var listViewItem = new ListViewItem(videoFileName) { Tag = batchVideoAndSub };
listViewItem.SubItems.Add(videoDimension.ToString());
var s = Utilities.FormatBytesToDisplayFileSize(batchVideoAndSub.VideoFileSizeInBytes);
listViewItem.SubItems.Add(s);
listViewItem.SubItems.Add(Path.GetFileName(batchVideoAndSub.SubtitleFileName));
listViewItem.SubItems.Add(string.Empty);
listViewBatch.Items.Add(listViewItem);
_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))
{
item.SubtitleFileName = subtitleFile;
item.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length;
batchVideoAndSub.SubtitleFileName = subtitleFile;
batchVideoAndSub.SubtitleFileFileSizeInBytes = new FileInfo(subtitleFile).Length;
}
var mediaInfo = FfmpegMediaInfo.Parse(videoFileName);
int width;
int height;
if (mediaInfo.VideoWidth > 0 && mediaInfo.VideoHeight > 0)
{
width = mediaInfo.VideoWidth;
height = mediaInfo.VideoHeight;
return batchVideoAndSub;
}
else
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))
{
@ -2179,26 +2202,14 @@ namespace Nikse.SubtitleEdit.Forms
vInfo = UiUtil.GetVideoInfo(videoFileName);
}
width = vInfo.Width;
height = vInfo.Height;
}
if (width == 0 || height == 0)
if (vInfo.Width == 0 || vInfo.Height == 0)
{
SeLogger.Error("Skipping burn-in file with no video: " + videoFileName);
return; // skip audio or damaged files
// return; // skip audio or damaged files
return new Dimension();
}
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(Path.GetFileName(item.SubtitleFileName));
listViewItem.SubItems.Add(string.Empty);
listViewBatch.Items.Add(listViewItem);
_batchVideoAndSubList.Add(item);
}
return new Dimension(vInfo.Height, vInfo.Width);
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)