Skip to content

Commit

Permalink
Added metadata to the ffmpegdecoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
filoe committed Jul 8, 2017
1 parent a37d558 commit fc6dfc0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CSCore.Ffmpeg/AvFormatContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CSCore.Ffmpeg.Interops;

namespace CSCore.Ffmpeg
Expand Down Expand Up @@ -53,6 +55,8 @@ public unsafe AVFormatContext FormatContext
}
}

public Dictionary<string,string> Metadata { get; private set; }

public unsafe AvFormatContext(FfmpegStream stream)
{
_formatContext = FfmpegCalls.AvformatAllocContext();
Expand All @@ -78,6 +82,16 @@ private unsafe void Initialize()
FfmpegCalls.AvFormatFindStreamInfo(_formatContext);
BestAudioStreamIndex = FfmpegCalls.AvFindBestStreamInfo(_formatContext);
_stream = new AvStream((IntPtr)_formatContext->streams[BestAudioStreamIndex]);

Metadata = new Dictionary<string, string>();
if (_formatContext->metadata != null)
{
var metadata = _formatContext->metadata->Elements;
foreach (var element in metadata)
{
Metadata.Add(element.Key, element.Value);
}
}
}

public void SeekFile(double seconds)
Expand Down
14 changes: 14 additions & 0 deletions CSCore.Ffmpeg/FfmpegDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

Expand Down Expand Up @@ -26,6 +27,19 @@ public class FfmpegDecoder : IWaveSource
private int _overflowOffset;
private long _position;

/// <summary>
/// Gets a dictionary with found metadata.
/// </summary>
public Dictionary<string, string> Metadata
{
get
{
if(_formatContext == null)
return new Dictionary<string, string>();
return _formatContext.Metadata;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="FfmpegDecoder" /> class based on a specified filename or url.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions CSCore.Ffmpeg/Interops/FFmpeg.avformat.g.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSCore.Ffmpeg.Interops
Expand All @@ -17,6 +18,25 @@ internal unsafe partial struct AVBPrint

internal unsafe partial struct AVDictionary
{
internal int count;
internal AVDictionaryEntry* elems;

public AVDictionaryEntry[] Elements
{
get { return GetElements(); }
}

private AVDictionaryEntry[] GetElements()
{
var cpy = elems;
AVDictionaryEntry[] elements = new AVDictionaryEntry[count];
for (int i = 0; i < count; i++)
{
elements[i] = cpy[i];
}

return elements;
}
}

internal unsafe partial struct AVCodecInternal
Expand Down
10 changes: 10 additions & 0 deletions CSCore.Ffmpeg/Interops/FFmpeg.avutil.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ internal unsafe partial struct AVDictionaryEntry
{
internal sbyte* @key;
internal sbyte* @value;

public string Key
{
get { return Marshal.PtrToStringAnsi((IntPtr) key); }
}

public string Value
{
get { return Marshal.PtrToStringAnsi((IntPtr) value); }
}
}

internal unsafe partial struct AVDictionary
Expand Down

0 comments on commit fc6dfc0

Please sign in to comment.