diff options
| author | cvium <clausvium@gmail.com> | 2022-01-11 23:30:30 +0100 |
|---|---|---|
| committer | cvium <clausvium@gmail.com> | 2022-01-11 23:30:30 +0100 |
| commit | 6ffa9539bbfbfb1090b02cebc8a28283a8c69041 (patch) | |
| tree | 13f4a1d968780f90cd7d0c99e422970117a380f0 /src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs | |
| parent | c658a883a2bc84b46ed73d209d2983e8a324cdce (diff) | |
Refactor and add scheduled task
Diffstat (limited to 'src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs')
| -rw-r--r-- | src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs | 101 |
1 files changed, 50 insertions, 51 deletions
diff --git a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs index 6a8a55643..8bb1ff00d 100644 --- a/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs +++ b/src/Jellyfin.MediaEncoding.Keyframes/Matroska/MatroskaKeyframeExtractor.cs @@ -4,73 +4,72 @@ using System.IO; using Jellyfin.MediaEncoding.Keyframes.Matroska.Extensions; using NEbml.Core; -namespace Jellyfin.MediaEncoding.Keyframes.Matroska +namespace Jellyfin.MediaEncoding.Keyframes.Matroska; + +/// <summary> +/// The keyframe extractor for the matroska container. +/// </summary> +public static class MatroskaKeyframeExtractor { /// <summary> - /// The keyframe extractor for the matroska container. + /// Extracts the keyframes in ticks (scaled using the container timestamp scale) from the matroska container. /// </summary> - public static class MatroskaKeyframeExtractor + /// <param name="filePath">The file path.</param> + /// <returns>An instance of <see cref="KeyframeData"/>.</returns> + public static KeyframeData GetKeyframeData(string filePath) { - /// <summary> - /// Extracts the keyframes in ticks (scaled using the container timestamp scale) from the matroska container. - /// </summary> - /// <param name="filePath">The file path.</param> - /// <returns>An instance of <see cref="KeyframeData"/>.</returns> - public static KeyframeData GetKeyframeData(string filePath) - { - using var stream = File.OpenRead(filePath); - using var reader = new EbmlReader(stream); + using var stream = File.OpenRead(filePath); + using var reader = new EbmlReader(stream); + + var seekHead = reader.ReadSeekHead(); + var info = reader.ReadInfo(seekHead.InfoPosition); + var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo); - var seekHead = reader.ReadSeekHead(); - var info = reader.ReadInfo(seekHead.InfoPosition); - var videoTrackNumber = reader.FindFirstTrackNumberByType(seekHead.TracksPosition, MatroskaConstants.TrackTypeVideo); + var keyframes = new List<long>(); + reader.ReadAt(seekHead.CuesPosition); + reader.EnterContainer(); - var keyframes = new List<long>(); - reader.ReadAt(seekHead.CuesPosition); + while (reader.FindElement(MatroskaConstants.CuePoint)) + { reader.EnterContainer(); + ulong? trackNumber = null; + // Mandatory element + reader.FindElement(MatroskaConstants.CueTime); + var cueTime = reader.ReadUInt(); - while (reader.FindElement(MatroskaConstants.CuePoint)) + // Mandatory element + reader.FindElement(MatroskaConstants.CueTrackPositions); + reader.EnterContainer(); + if (reader.FindElement(MatroskaConstants.CuePointTrackNumber)) { - reader.EnterContainer(); - ulong? trackNumber = null; - // Mandatory element - reader.FindElement(MatroskaConstants.CueTime); - var cueTime = reader.ReadUInt(); - - // Mandatory element - reader.FindElement(MatroskaConstants.CueTrackPositions); - reader.EnterContainer(); - if (reader.FindElement(MatroskaConstants.CuePointTrackNumber)) - { - trackNumber = reader.ReadUInt(); - } - - reader.LeaveContainer(); + trackNumber = reader.ReadUInt(); + } - if (trackNumber == videoTrackNumber) - { - keyframes.Add(ScaleToTicks(cueTime, info.TimestampScale)); - } + reader.LeaveContainer(); - reader.LeaveContainer(); + if (trackNumber == videoTrackNumber) + { + keyframes.Add(ScaleToTicks(cueTime, info.TimestampScale)); } reader.LeaveContainer(); - - var result = new KeyframeData(ScaleToTicks(info.Duration ?? 0, info.TimestampScale), keyframes); - return result; } - private static long ScaleToTicks(ulong unscaledValue, long timestampScale) - { - // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns - return (long)unscaledValue * timestampScale / 100; - } + reader.LeaveContainer(); - private static long ScaleToTicks(double unscaledValue, long timestampScale) - { - // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns - return Convert.ToInt64(unscaledValue * timestampScale / 100); - } + var result = new KeyframeData(ScaleToTicks(info.Duration ?? 0, info.TimestampScale), keyframes); + return result; + } + + private static long ScaleToTicks(ulong unscaledValue, long timestampScale) + { + // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns + return (long)unscaledValue * timestampScale / 100; + } + + private static long ScaleToTicks(double unscaledValue, long timestampScale) + { + // TimestampScale is in nanoseconds, scale it to get the value in ticks, 1 tick == 100 ns + return Convert.ToInt64(unscaledValue * timestampScale / 100); } } |
