using FFmpeg.AutoGen; using System.IO.Enumeration; using System.Runtime.InteropServices; using System.Text; namespace FNZCM2.ProofOfConcept; using FSE = (bool isDir, string path, long size, DateTimeOffset lastWrited); internal static class PocFFMPEG { public static unsafe void RunPoc() { //BEGIN init once ffmpeg.RootPath = "./libFFMPEG/bin"; var tryAllocateFormatContext = ffmpeg.avformat_alloc_context(); if(tryAllocateFormatContext!=null) ffmpeg.avformat_free_context(tryAllocateFormatContext); //DynamicallyLoadedBindings.Initialize(); //ffmpeg.avdevice_register_all(); //END init once //choose a file to open var fse = new FileSystemEnumerable( "library", (ref FileSystemEntry p) => new( p.IsDirectory, p.ToFullPath(), p.Length, p.LastWriteTimeUtc ), new() { RecurseSubdirectories = true, }) { ShouldIncludePredicate = (ref FileSystemEntry p) => p.ToFullPath().EndsWith(".flac", true, null) }; var file = fse.First(); var filePath = file.path; //BEGIN session var formatContext = ffmpeg.avformat_alloc_context(); if (0 != ffmpeg.avformat_open_input(&formatContext, filePath, null, null)) { throw new Exception("Could not open file."); } if (ffmpeg.avformat_find_stream_info(formatContext, null) != 0) { throw new Exception("Could not retrieve stream info."); } Dictionary tags = new(); AVDictionaryEntry* tag = null; while ((tag = ffmpeg.av_dict_get(formatContext->metadata, "", tag, 0)) != null) { var k = Marshal.PtrToStringUTF8((IntPtr)tag->key); var v = Marshal.PtrToStringUTF8((IntPtr)tag->value); if (tags.ContainsKey(k)) { int bp = 0; } tags[k] = v; } { var f = formatContext->iformat; var ln = Marshal.PtrToStringUTF8((IntPtr)f->long_name); var n = Marshal.PtrToStringUTF8((IntPtr)f->name); } for (int i = 0; i < formatContext->nb_streams; i++) { var stream = formatContext->streams[i]; var codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id); var codecName = codec != null ? ffmpeg.avcodec_get_name(stream->codecpar->codec_id) : "Unknown codec"; var codeType = stream->codecpar->codec_type; if (stream->disposition == ffmpeg.AV_DISPOSITION_ATTACHED_PIC) { var pkt = stream->attached_pic; //Console.WriteLine($"Embedded cover art: Size={pkt.size} bytes, Format={ffmpeg.avcodec_get_name(pkt.)}"); } } ffmpeg.avformat_close_input(&formatContext); //END session //SUCCESS } }