PocFFMPEG.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using FFmpeg.AutoGen;
  2. using System.IO.Enumeration;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. namespace FNZCM2.ProofOfConcept;
  6. using FSE = (bool isDir, string path, long size, DateTimeOffset lastWrited);
  7. internal static class PocFFMPEG
  8. {
  9. public static unsafe void RunPoc()
  10. {
  11. //BEGIN init once
  12. ffmpeg.RootPath = "./libFFMPEG/bin";
  13. var tryAllocateFormatContext = ffmpeg.avformat_alloc_context();
  14. if(tryAllocateFormatContext!=null) ffmpeg.avformat_free_context(tryAllocateFormatContext);
  15. //DynamicallyLoadedBindings.Initialize();
  16. //ffmpeg.avdevice_register_all();
  17. //END init once
  18. //choose a file to open
  19. var fse = new FileSystemEnumerable<FSE>(
  20. "library",
  21. (ref FileSystemEntry p) => new(
  22. p.IsDirectory,
  23. p.ToFullPath(),
  24. p.Length,
  25. p.LastWriteTimeUtc
  26. ),
  27. new()
  28. {
  29. RecurseSubdirectories = true,
  30. })
  31. {
  32. ShouldIncludePredicate = (ref FileSystemEntry p) => p.ToFullPath().EndsWith(".flac", true, null)
  33. };
  34. var file = fse.First();
  35. var filePath = file.path;
  36. //BEGIN session
  37. var formatContext = ffmpeg.avformat_alloc_context();
  38. if (0 != ffmpeg.avformat_open_input(&formatContext, filePath, null, null))
  39. {
  40. throw new Exception("Could not open file.");
  41. }
  42. if (ffmpeg.avformat_find_stream_info(formatContext, null) != 0)
  43. {
  44. throw new Exception("Could not retrieve stream info.");
  45. }
  46. Dictionary<string, string> tags = new();
  47. AVDictionaryEntry* tag = null;
  48. while ((tag = ffmpeg.av_dict_get(formatContext->metadata, "", tag, 0)) != null)
  49. {
  50. var k = Marshal.PtrToStringUTF8((IntPtr)tag->key);
  51. var v = Marshal.PtrToStringUTF8((IntPtr)tag->value);
  52. if (tags.ContainsKey(k))
  53. {
  54. int bp = 0;
  55. }
  56. tags[k] = v;
  57. }
  58. {
  59. var f = formatContext->iformat;
  60. var ln = Marshal.PtrToStringUTF8((IntPtr)f->long_name);
  61. var n = Marshal.PtrToStringUTF8((IntPtr)f->name);
  62. }
  63. for (int i = 0; i < formatContext->nb_streams; i++)
  64. {
  65. var stream = formatContext->streams[i];
  66. var codec = ffmpeg.avcodec_find_decoder(stream->codecpar->codec_id);
  67. var codecName = codec != null ? ffmpeg.avcodec_get_name(stream->codecpar->codec_id) : "Unknown codec";
  68. var codeType = stream->codecpar->codec_type;
  69. if (stream->disposition == ffmpeg.AV_DISPOSITION_ATTACHED_PIC)
  70. {
  71. var pkt = stream->attached_pic;
  72. //Console.WriteLine($"Embedded cover art: Size={pkt.size} bytes, Format={ffmpeg.avcodec_get_name(pkt.)}");
  73. }
  74. }
  75. ffmpeg.avformat_close_input(&formatContext);
  76. //END session
  77. //SUCCESS
  78. }
  79. }