使用ffmpeg-autogen从c#中的ffmpeg复制C元数据示例 - av_dict_get失败

时间:2017-09-30 09:23:34

标签: c# ffmpeg

我正在尝试使用https://ffmpeg.org/doxygen/3.0/doc_2examples_2metadata_8c_source.html

中的包装器在C#中复制https://github.com/Ruslan-B/FFmpeg.AutoGen

我可以打开并阅读该文件的某些属性,但tag始终为空,即使在调用av_dict_get

之后也是如此

我的代码如下

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using FFmpeg.AutoGen;

namespace ffmpeg_test
{
    class Program
    {
        static void Main(string[] args)
        {
            var currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var libPath = Path.Combine(currentPath, "lib");
            SetDllDirectory(libPath);
            ffmpeg.av_register_all();
            ffmpeg.avcodec_register_all();

            DoRiskyThings();

        }

        private static unsafe void DoRiskyThings()
        {
            var pFormatContext = ffmpeg.avformat_alloc_context();

            if (ffmpeg.avformat_open_input(&pFormatContext, "01 - 999,999.opus", null, null) != 0)
                throw new ApplicationException(@"Could not open file.");

            ffmpeg.avformat_find_stream_info(pFormatContext, null);

            AVStream* pStream = null;
            pStream = pFormatContext->streams[0];
            var codecContext = *pStream->codec;

            Console.WriteLine($"codec name: {ffmpeg.avcodec_get_name(codecContext.codec_id)}");
            Console.WriteLine($"number of streams: {pFormatContext->nb_streams}");

            //attempting to replicate https://ffmpeg.org/doxygen/3.0/doc_2examples_2metadata_8c_source.html
            AVDictionaryEntry* tag = null;
            tag = ffmpeg.av_dict_get(pFormatContext->metadata, "", null, 2);
            while (tag != null)
            {
                tag = ffmpeg.av_dict_get(pFormatContext->metadata, "", tag, 2);
                Console.WriteLine(Encoding.UTF8.GetString(tag->key,100));
                //tag->key and //tag->value are byte pointers
            }
        }

        [DllImport("kernel32", SetLastError = true)]
        private static extern bool SetDllDirectory(string lpPathName);
    }
}

1 个答案:

答案 0 :(得分:1)

你需要编组字符串。这就像一个魅力:

var url = @"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

var pFormatContext = ffmpeg.avformat_alloc_context();

if (ffmpeg.avformat_open_input(&pFormatContext, url, null, null) != 0)
    throw new ApplicationException(@"Could not open file.");

if (ffmpeg.avformat_find_stream_info(pFormatContext, null) != 0)
    throw new ApplicationException(@"Could not find stream info");

AVDictionaryEntry* tag = null;
while ((tag = ffmpeg.av_dict_get(pFormatContext->metadata, "", tag, ffmpeg.AV_DICT_IGNORE_SUFFIX)) != null)
{
    var key = Marshal.PtrToStringAnsi((IntPtr) tag->key);
    var value = Marshal.PtrToStringAnsi((IntPtr) tag->value);
    Console.WriteLine($"{key} = {value}");
}