Skip to content

Commit

Permalink
Added discrete subtitle formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcclive committed Apr 12, 2021
1 parent 8b02f0a commit 09a528e
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Tricycle.Media.FFmpeg.Tests/MediaInspectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ public async Task TestInspect()
Assert.AreEqual(3, stream.Index);
Assert.IsInstanceOfType(stream, typeof(SubtitleStreamInfo));
Assert.AreEqual(SubtitleType.Graphic, ((SubtitleStreamInfo)stream).SubtitleType);
Assert.AreEqual(SubtitleFormat.Pgs, ((SubtitleStreamInfo)stream).Format);

stream = info.Streams[4];

Expand All @@ -342,6 +343,7 @@ public async Task TestInspect()
Assert.AreEqual(4, stream.Index);
Assert.IsInstanceOfType(stream, typeof(SubtitleStreamInfo));
Assert.AreEqual(SubtitleType.Text, ((SubtitleStreamInfo)stream).SubtitleType);
Assert.AreEqual(SubtitleFormat.Subrip, ((SubtitleStreamInfo)stream).Format);

#endregion

Expand Down
26 changes: 25 additions & 1 deletion Tricycle.Media.FFmpeg/MediaInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ StreamInfo Map(Stream stream)
case "subtitle":
result = new SubtitleStreamInfo()
{
SubtitleType = GetSubtitleType(stream.CodecName)
SubtitleType = GetSubtitleType(stream.CodecName),
Format = GetSubtitleFormat(stream.CodecName)
};
break;
case "video":
Expand Down Expand Up @@ -311,6 +312,29 @@ SubtitleType GetSubtitleType(string codecName)
}
}

SubtitleFormat? GetSubtitleFormat(string codecName)
{
switch (codecName)
{
case "ass":
case "ssa":
return SubtitleFormat.Ssa;
case "dvd_subtitle":
return SubtitleFormat.Dvb;
case "hdmv_pgs_subtitle":
return SubtitleFormat.Pgs;
case "mov_text":
return SubtitleFormat.TimedText;
case "srt":
case "subrip":
return SubtitleFormat.Subrip;
case "webvtt":
return SubtitleFormat.WebVtt;
default:
return null;
}
}

AudioFormat? GetAudioFormat(string codecName, string profile)
{
if (string.IsNullOrWhiteSpace(codecName))
Expand Down
15 changes: 15 additions & 0 deletions Tricycle.Models/Jobs/SubtitleOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
namespace Tricycle.Models.Jobs
{
public class SubtitleOutputStream : TranscodedOutputStream<SubtitleFormat>
{
public SubtitleOutputStream()
{
}

public SubtitleOutputStream(SubtitleFormat format)
{
Format = format;
}
}
}
14 changes: 14 additions & 0 deletions Tricycle.Models/SubtitleFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
namespace Tricycle.Models
{
public enum SubtitleFormat
{
Dvb,
Dvd,
Pgs,
Ssa,
Subrip,
TimedText,
WebVtt
}
}
25 changes: 25 additions & 0 deletions Tricycle.Utilities.Tests/SubtitleUtilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tricycle.Models;

namespace Tricycle.Utilities.Tests
{
[TestClass]
public class SubtitleUtilityTests
{
[TestMethod]
public void TestIsSupportedByContainer()
{
foreach (SubtitleFormat subtitleFormat in Enum.GetValues(typeof(SubtitleFormat)))
{
var isSupported = SubtitleUtility.IsSupportedByContainer(ContainerFormat.Mkv, subtitleFormat);

Assert.AreEqual(subtitleFormat != SubtitleFormat.TimedText, isSupported);

isSupported = SubtitleUtility.IsSupportedByContainer(ContainerFormat.Mp4, subtitleFormat);

Assert.AreEqual(subtitleFormat == SubtitleFormat.TimedText, isSupported);
}
}
}
}
38 changes: 38 additions & 0 deletions Tricycle.Utilities/SubtitleUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Tricycle.Models;

namespace Tricycle.Utilities
{
public static class SubtitleUtility
{
public static bool IsSupportedByContainer(ContainerFormat containerFormat, SubtitleFormat subtitleFormat)
{
switch (containerFormat)
{
case ContainerFormat.Mkv:
switch (subtitleFormat)
{
case SubtitleFormat.Dvb:
case SubtitleFormat.Dvd:
case SubtitleFormat.Pgs:
case SubtitleFormat.Ssa:
case SubtitleFormat.Subrip:
case SubtitleFormat.WebVtt:
return true;
default:
return false;
}
case ContainerFormat.Mp4:
switch (subtitleFormat)
{
case SubtitleFormat.TimedText:
return true;
default:
return false;
}
default:
return false;
}
}
}
}

0 comments on commit 09a528e

Please sign in to comment.