Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlaffargue committed Jul 6, 2024
1 parent 4b600eb commit 9ff15c7
Show file tree
Hide file tree
Showing 61 changed files with 1,758 additions and 1,657 deletions.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# M3U8 HLS Parser C#

## Installation

Package NuGet : https://www.nuget.org/packages/M3U8Parser/

```dotnet add package M3U8Parser```

## Documentation

M3U8Parser makes it easy to read, edit and create a m3u8 file.
M3U8Parser makes it easy to read, edit and create a m3u8 file.

* M3U8Parser does not try to validate your file, it's your responsability to follow RFC.
* Require .netstandard2.0 / .net6

Usage:

Read a file and edit it

```csharp
// Load a file
var masterPlaylist = MasterPlaylist.LoadFromFile("master.m3u8");
Expand All @@ -29,6 +32,7 @@ Read a file and edit it
```

Or your can create a master file

```csharp
var masterPlaylist = new MasterPlaylist(hlsVersion: 4);

Expand Down Expand Up @@ -67,6 +71,7 @@ Or your can create a master file
```

This code should produce the following master playlist:

```
#EXTM3U
#EXT-X-VERSION:4
Expand All @@ -80,23 +85,26 @@ v0.m3u8
v1.m3u8
```

Limitation:
* Alpha version : Only master is supported (playlist be coming)
Limitation:

* Alpha version : Only master is supported (playlist be coming)

## Supported tags

The following tags should be fully supported:

### Basics
| TAGS |
| ------------- |
| EXTM3U |

| TAGS |
|---------------|
| EXTM3U |
| EXT-X-VERSION |

### Master Playlist Tags
| TAGS | ATTRIBUTE |
| ------------- |---------------------------------------------------------------------------------------------------------------------|
| EXT-X-MEDIA | GROUP-ID, AUTOSELECT, DEFAULT, LANGUAGE, NAME, TYPE, URI, CHARACTERISTICS, INSTREAM-ID |
| EXT-X-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, URI, SUBTITLES, VIDEO-RANGE, HDCP-LEVEL, AVERAGE-BANDWIDTH |
| EXT-X-I-FRAME-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, AUDIO, VIDEO, CLOSED-CAPTIONS |

| TAGS | ATTRIBUTE |
|--------------------------|--------------------------------------------------------------------------------------------|
| EXT-X-MEDIA | GROUP-ID, AUTOSELECT, DEFAULT, LANGUAGE, NAME, TYPE, URI, CHARACTERISTICS, INSTREAM-ID |
| EXT-X-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, URI, SUBTITLES, VIDEO-RANGE, HDCP-LEVEL, AVERAGE-BANDWIDTH |
| EXT-X-I-FRAME-STREAM-INF | BANDWIDTH, CODECS, RESOLUTION, AUDIO, VIDEO, CLOSED-CAPTIONS |

7 changes: 4 additions & 3 deletions src/M3U8Parser/Attributes/Audio.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using M3U8Parser.Attributes.BaseAttribute;

namespace M3U8Parser.Attributes
{
using M3U8Parser.Attributes.BaseAttribute;

public class Audio : StringAttribute
{
public Audio() : base("AUDIO")
public Audio()
: base("AUDIO")
{
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/M3U8Parser/Attributes/Autoselect.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using M3U8Parser.Attributes.BaseAttribute;

namespace M3U8Parser.Attributes
{
using M3U8Parser.Attributes.BaseAttribute;

public class Autoselect : BoolAttribute
{
public Autoselect() : base("AUTOSELECT")
public Autoselect()
: base("AUTOSELECT")
{
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/M3U8Parser/Attributes/AverageBandwidth.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using M3U8Parser.Attributes.BaseAttribute;

namespace M3U8Parser.Attributes
{
public class AverageBandwidth: CustomAttribute<long?>
using M3U8Parser.Attributes.BaseAttribute;

public class AverageBandwidth : CustomAttribute<long?>
{
public AverageBandwidth() : base("AVERAGE-BANDWIDTH")
public AverageBandwidth()
: base("AVERAGE-BANDWIDTH")
{
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/M3U8Parser/Attributes/Bandwidth.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using M3U8Parser.Attributes.BaseAttribute;

namespace M3U8Parser.Attributes
{
using M3U8Parser.Attributes.BaseAttribute;

public class Bandwidth : CustomAttribute<long>
{
public Bandwidth() : base("BANDWIDTH")
public Bandwidth()
: base("BANDWIDTH")
{
}
}
Expand Down
57 changes: 29 additions & 28 deletions src/M3U8Parser/Attributes/BaseAttribute/BoolAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
using System.Text.RegularExpressions;

namespace M3U8Parser.Attributes.BaseAttribute
{
public class BoolAttribute : CustomAttribute<bool>
{
using System.Text.RegularExpressions;

public class BoolAttribute : CustomAttribute<bool>
{
private const string YesValue = "YES";
private const string NoValue = "NO";

public BoolAttribute(string attributeName) : base(attributeName)
{
}
public BoolAttribute(string attributeName)
: base(attributeName)
{
}

public override string ToString()
{
return $"{AttributeName}={BoolToString(Value)}";
}
public override string ToString()
{
return $"{AttributeName}={BoolToString(Value)}";
}

public override void Read(string content)
public override void Read(string content)
{
var pattern = $"(?={AttributeName})(.*?)(?=,|$)";
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase);
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase);

if (match.Success)
{
var valueFounded = match.Groups[0].Value.Split('=')[1];
Value = StringToBool(valueFounded);
}
}
{
var valueFounded = match.Groups[0].Value.Split('=')[1];
Value = StringToBool(valueFounded);
}
}

private string BoolToString(bool value)
{
return value ? YesValue : NoValue;
}
private string BoolToString(bool value)
{
return value ? YesValue : NoValue;
}

private bool StringToBool(string value)
{
return value == YesValue;
}
}
private bool StringToBool(string value)
{
return value == YesValue;
}
}
}
114 changes: 57 additions & 57 deletions src/M3U8Parser/Attributes/BaseAttribute/CustomAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
using System;
using System.Text.RegularExpressions;
using M3U8Parser.Interfaces;

namespace M3U8Parser.Attributes.BaseAttribute
{
public class CustomAttribute<T> : IAttribute
{
public CustomAttribute(string attributeName)
{
AttributeName = attributeName;
}

public T Value { get; set; }

protected string AttributeName { get; }

public override string ToString()
{
if (Value != null)
{
return $"{AttributeName}={Value}";
}

return string.Empty;
}

public virtual void Read(string content)
{
var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)",
RegexOptions.Multiline & RegexOptions.IgnoreCase);

var type = typeof(T);

if (match.Success)
{
var valueFounded = match.Groups[0].Value.Split('=')[1];

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
type = Nullable.GetUnderlyingType(type);
}

if (typeof(ICustomAttribute).IsAssignableFrom(type))
{
var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false);
Value = (T)instanceAttribute.ParseFromString(valueFounded);
}
else
{
Value = (T)Convert.ChangeType(valueFounded, type);
}
}
else
{
Value = default(T);
}
}
}
using System;
using System.Text.RegularExpressions;
using M3U8Parser.Interfaces;

public class CustomAttribute<T> : IAttribute
{
public CustomAttribute(string attributeName)
{
AttributeName = attributeName;
}

public T Value { get; set; }

protected string AttributeName { get; }

public override string ToString()
{
if (Value != null)
{
return $"{AttributeName}={Value}";
}

return string.Empty;
}

public virtual void Read(string content)
{
var match = Regex.Match(content.Trim(), $"[,|:](?={AttributeName})(.*?)(?=,|$)",
RegexOptions.Multiline & RegexOptions.IgnoreCase);

var type = typeof(T);

if (match.Success)
{
var valueFounded = match.Groups[0].Value.Split('=')[1];

if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
type = Nullable.GetUnderlyingType(type);
}

if (typeof(ICustomAttribute).IsAssignableFrom(type))
{
var instanceAttribute = (ICustomAttribute)Activator.CreateInstance(type, false);
Value = (T)instanceAttribute.ParseFromString(valueFounded);
}
else
{
Value = (T)Convert.ChangeType(valueFounded, type);
}
}
else
{
Value = default;
}
}
}
}
52 changes: 26 additions & 26 deletions src/M3U8Parser/Attributes/BaseAttribute/DecimalAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace M3U8Parser.Attributes.BaseAttribute
{
public class DecimalAttribute : CustomAttribute<decimal?>
{
public DecimalAttribute(string attributeName) : base(attributeName)
{
}
using System.Globalization;
using System.Text.RegularExpressions;

public class DecimalAttribute : CustomAttribute<decimal?>
{
public DecimalAttribute(string attributeName) : base(attributeName)
{
}

public override string ToString()
{
if (Value != null)
{
return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}";
}
public override string ToString()
{
if (Value != null)
{
return $"{AttributeName}={Value.Value.ToString(CultureInfo.InvariantCulture)}";
}

return string.Empty;
}

return string.Empty;
}

public override void Read(string content)
public override void Read(string content)
{
var pattern = $"(?={AttributeName})(.*?)(?=,|$)";
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase);
var match = Regex.Match(content.Trim(), pattern, RegexOptions.Multiline & RegexOptions.IgnoreCase);

if (match.Success)
{
var valueFounded = match.Groups[0].Value.Split('=')[1];
Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture);
}
}
}
{
var valueFounded = match.Groups[0].Value.Split('=')[1];
Value = decimal.Parse(valueFounded, CultureInfo.InvariantCulture);
}
}
}
}
Loading

0 comments on commit 9ff15c7

Please sign in to comment.