From 4220dc45bf87b4d9647edb7f0b3213673870feac Mon Sep 17 00:00:00 2001 From: Rob Schneider Date: Fri, 25 Apr 2025 19:50:31 +0200 Subject: [PATCH 1/2] Adds fractional duration support for media playlist --- PlaylistsNET/Content/HlsContent.cs | 4 ++-- PlaylistsNET/Models/HlsPlaylistEntry.cs | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/PlaylistsNET/Content/HlsContent.cs b/PlaylistsNET/Content/HlsContent.cs index 35150f4..06d810d 100644 --- a/PlaylistsNET/Content/HlsContent.cs +++ b/PlaylistsNET/Content/HlsContent.cs @@ -204,10 +204,10 @@ private HlsMediaPlaylist GetMediaHls(List playlistLines) continue; } - var match = Regex.Match(currentLine, @"^#EXTINF:(-?\d*),(.*)$"); + var match = Regex.Match(currentLine, @"^#EXTINF:(-?\d*)(.\d*)?,(.*)$"); if (match.Success) { - currentEntry.Duration = string.IsNullOrEmpty(match.Groups[1].Value) ? 0 : int.Parse(match.Groups[1].Value); + currentEntry.Duration = string.IsNullOrEmpty(match.Groups[1].Value) ? 0 : double.Parse(match.Groups[1].Value); currentEntry.Title = match.Groups[2].Value; continue; } diff --git a/PlaylistsNET/Models/HlsPlaylistEntry.cs b/PlaylistsNET/Models/HlsPlaylistEntry.cs index 7c41dd4..2a42bea 100644 --- a/PlaylistsNET/Models/HlsPlaylistEntry.cs +++ b/PlaylistsNET/Models/HlsPlaylistEntry.cs @@ -4,6 +4,8 @@ namespace PlaylistsNET.Models { + using System.Globalization; + public abstract class HlsPlaylistEntry : BasePlaylistEntry { public Dictionary CustomProperties { get; set; } @@ -37,7 +39,7 @@ protected void CheckEmptyStringAndAppend(string tag, public class HlsMediaPlaylistEntry : HlsPlaylistEntry { - public int Duration { get; set; } + public double Duration { get; set; } public string Title { get; set; } public long MediaSequence { get; set; } public bool Discontinuity { get; set; } @@ -71,8 +73,7 @@ public override string ToString() sb.AppendLine("#EXT-X-DISCONTINUITY"); } - string durationTitle = String.Join(",", - new string[] { Duration == 0 ? "" : Duration.ToString(), Title }); + string durationTitle = String.Join(",", Duration == 0 ? "" : Duration.ToString(), Title); CheckAndAppend("#EXTINF", durationTitle, sb, dt => !dt.Equals(",")); foreach(var kv in CustomProperties) From aa200d34244e9428d652b50386066f53171615ba Mon Sep 17 00:00:00 2001 From: Rob Schneider Date: Fri, 25 Apr 2025 20:31:04 +0200 Subject: [PATCH 2/2] Fixes regex grouping of duration fractions --- PlaylistsNET/Content/HlsContent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PlaylistsNET/Content/HlsContent.cs b/PlaylistsNET/Content/HlsContent.cs index 06d810d..ebe1df3 100644 --- a/PlaylistsNET/Content/HlsContent.cs +++ b/PlaylistsNET/Content/HlsContent.cs @@ -204,7 +204,7 @@ private HlsMediaPlaylist GetMediaHls(List playlistLines) continue; } - var match = Regex.Match(currentLine, @"^#EXTINF:(-?\d*)(.\d*)?,(.*)$"); + var match = Regex.Match(currentLine, @"^#EXTINF:(-?\d*\.?\d*?),(.*)$"); if (match.Success) { currentEntry.Duration = string.IsNullOrEmpty(match.Groups[1].Value) ? 0 : double.Parse(match.Groups[1].Value);