123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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<FSE>(
- "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<string, string> 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
- }
- }
|