From 1cf4ccd05bfd510cf17a0d9468ab75ebfb465625 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Sun, 17 Nov 2024 21:13:58 +0100 Subject: [PATCH] Allow reading matroska files with "mp4" ext Related to #9010 --- src/libse/Common/FileUtil.cs | 12 ++++++++++++ src/ui/Forms/Main.cs | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index 2ac9056e1..6565a7ebe 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -859,6 +859,18 @@ namespace Nikse.SubtitleEdit.Core.Common } } + public static bool IsMatroskaFileFast(string fileName) + { + using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + var buffer = new byte[4]; + fs.Read(buffer, 0, buffer.Length); + + // 1a 45 df a3 + return buffer[0] == 0x1a && buffer[1] == 0x45 && buffer[2] == 0xdf && buffer[3] == 0xa3; + } + } + /// /// Checks if a file is locked by attempting to open it with exclusive read access. /// diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index e0e1f01f4..7bd115b15 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -2952,7 +2952,12 @@ namespace Nikse.SubtitleEdit.Forms } } - if (ext == ".mkv" || ext == ".mks") + if (ext == ".mkv" || ext == ".mks" || + + // allow for mkv files with wrong extension + ((ext == ".mp4" || ext == ".mov" || ext == ".m4v" || ext == ".wmv") && FileUtil.IsMatroskaFileFast(fileName) && FileUtil.IsMatroskaFile(fileName)) + + ) { ImportSubtitleFromMatroskaFile(fileName); return;