Browse Source

MK2: ffmpeg reader err handle

HOME 7 months ago
parent
commit
0805dac958

+ 15 - 4
FNZCM2/FNZCM2.Abstractions/Utility/FfMpegReader.cs

@@ -1,4 +1,5 @@
 using System.Runtime.InteropServices;
+using System.Text;
 using FFmpeg.AutoGen;
 using FNZCM2.Abstractions.Models.Metadata;
 
@@ -13,21 +14,30 @@ public static class FfMpegReader
         ffmpeg.RootPath = "./libFFMPEG/bin";
     }
 
-    public static unsafe FileMetaEntry Read(string filePath, long fileId)
+    public static unsafe FileMetaEntry? Read(string filePath, long fileId, out string? err)
     {
         // 创建 AVFormatContext 对象
         var formatContext = ffmpeg.avformat_alloc_context();
         try
         {
-            if (ffmpeg.avformat_open_input(&formatContext, filePath, null, null) != 0)
+            var avFormatOpenInputRet = ffmpeg.avformat_open_input(&formatContext, filePath, null, null);
+            if (avFormatOpenInputRet != 0)
             {
-                throw new Exception("无法打开文件");
+                var buf = stackalloc byte[512];
+                ffmpeg.av_strerror(avFormatOpenInputRet, buf, 512);
+                var str = Encoding.ASCII.GetString(buf, 512).TrimEnd('\0');
+                err = "open:" + str;
+                return null;
             }
 
             // 读取流信息
             if (ffmpeg.avformat_find_stream_info(formatContext, null) != 0)
             {
-                throw new Exception("无法获取流信息");
+                var buf = stackalloc byte[512];
+                ffmpeg.av_strerror(avFormatOpenInputRet, buf, 512);
+                var str = Encoding.ASCII.GetString(buf, 512).TrimEnd('\0');
+                err = "find stream:" + str;
+                return null;
             }
 
             int? durMs = formatContext->duration > 0 ? (int)(formatContext->duration / 1000) : null;
@@ -121,6 +131,7 @@ public static class FfMpegReader
                 Streams = streams.ToArray(),
             };
 
+            err = null;
             return fileMetaEntry;
         }
         finally

+ 66 - 4
FNZCM2/FNZCM2.ProofOfConcept/PocFfMpegReader.cs

@@ -13,9 +13,71 @@ internal static class PocFfMpegReader
         //Mp4Poc();
         //JpgPoc();
         //WebPPoc();
+        //LogPoc();
+        //IsoPoc();
         //SUCCESS
     }
 
+    private static void IsoPoc()
+    {
+        //choose a file to open
+        var fse = new FileSystemEnumerable<FSE>(
+            "library-fs",
+            (ref FileSystemEntry p) => new(
+                p.IsDirectory,
+                p.ToFullPath(),
+                p.Length,
+                p.LastWriteTimeUtc
+            ),
+            new()
+            {
+                RecurseSubdirectories = true,
+            })
+        {
+            ShouldIncludePredicate = (ref FileSystemEntry p) => p.ToFullPath().EndsWith(".iso", true, null)
+        };
+
+        var file = fse.First();
+        var filePath = file.path;
+
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
+        if (meta == null && err != null)
+        {
+            int bp1 = 0;
+        }
+        int bp = 0;
+    }
+
+    private static void LogPoc()
+    {
+        //choose a file to open
+        var fse = new FileSystemEnumerable<FSE>(
+            "library",
+            (ref FileSystemEntry p) => new(
+                p.IsDirectory,
+                p.ToFullPath(),
+                p.Length,
+                p.LastWriteTimeUtc
+            ),
+            new()
+            {
+                RecurseSubdirectories = true,
+            })
+        {
+            ShouldIncludePredicate = (ref FileSystemEntry p) => p.ToFullPath().EndsWith(".log", true, null)
+        };
+
+        var file = fse.First();
+        var filePath = file.path;
+
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
+        if (meta == null && err != null)
+        {
+            int bp1 = 0;
+        }
+        int bp = 0;
+    }
+
     private static void WebPPoc()
     {
         //choose a file to open
@@ -38,7 +100,7 @@ internal static class PocFfMpegReader
         var file = fse.First();
         var filePath = file.path;
 
-        var meta = FfMpegReader.Read(filePath, 0);
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
         int bp = 0;
     }
 
@@ -64,7 +126,7 @@ internal static class PocFfMpegReader
         var file = fse.First();
         var filePath = file.path;
 
-        var meta = FfMpegReader.Read(filePath, 0);
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
         int bp = 0;
     }
 
@@ -90,7 +152,7 @@ internal static class PocFfMpegReader
         var file = fse.First();
         var filePath = file.path;
 
-        var meta = FfMpegReader.Read(filePath, 0);
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
         int bp = 0;
     }
 
@@ -116,7 +178,7 @@ internal static class PocFfMpegReader
         var file = fse.First();
         var filePath = file.path;
 
-        var meta = FfMpegReader.Read(filePath, 0);
+        var meta = FfMpegReader.Read(filePath, 0, out var err);
         int bp = 0;
     }
 }