Skip to content

Commit

Permalink
Added subtitles to FFmpeg config.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcclive committed Apr 12, 2021
1 parent 09a528e commit 1aa43fd
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 2 deletions.
102 changes: 102 additions & 0 deletions Tricycle.Media.FFmpeg.Tests/FFmpegConfigManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,108 @@ public void CoalesceDoesNotThrowExceptionWhenDefaultAudioCodecIsMissing()
_configManager.Load();
}

[TestMethod]
public void CoalesceCopiesSubtitlesWhenNull()
{
_defaultConfig.Subtitles = new SubtitleConfig();

_configManager.Load();

Assert.IsNotNull(_configManager.Config?.Subtitles);
}

[TestMethod]
public void CoalesceCopiesSubtitlesCodecsWhenEmpty()
{
_userConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
};
_defaultConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ SubtitleFormat.TimedText, new SubtitleCodec() }
}
};

_configManager.Load();

Assert.AreEqual(_defaultConfig.Subtitles.Codecs.Count, _configManager.Config?.Subtitles?.Codecs?.Count);
}

[TestMethod]
public void CoalesceCopiesSubtitlesCodecNameWhenEmpty()
{
var format = SubtitleFormat.TimedText;
var name = "mov_text";
_userConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ format, new SubtitleCodec(string.Empty) }
}
};
_defaultConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ format, new SubtitleCodec(name) },
{ SubtitleFormat.Subrip, new SubtitleCodec("subrip") }
}
};

_configManager.Load();

Assert.AreEqual(name, _configManager.Config?.Subtitles?.Codecs?.GetValueOrDefault(format)?.Name);
}

[TestMethod]
public void CoalesceDoesNotCopySubtitlesCodecNameWhenNotEmpty()
{
var format = SubtitleFormat.TimedText;
var name = "mov_text";
_userConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ format, new SubtitleCodec(name) }
}
};
_defaultConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ format, new SubtitleCodec("timed_text") }
}
};

_configManager.Load();

Assert.AreEqual(name, _configManager.Config?.Subtitles?.Codecs?.GetValueOrDefault(format)?.Name);
}

[TestMethod]
public void CoalesceDoesNotThrowExceptionWhenDefaultSubtitlesCodecIsMissing()
{
_userConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ SubtitleFormat.TimedText, new SubtitleCodec(string.Empty) }
}
};
_defaultConfig.Subtitles = new SubtitleConfig()
{
Codecs = new Dictionary<SubtitleFormat, SubtitleCodec>()
{
{ SubtitleFormat.Subrip, new SubtitleCodec("subrip") }
}
};

_configManager.Load();
}

[TestMethod]
public void CoalesceCopiesVideoWhenNull()
{
Expand Down
35 changes: 35 additions & 0 deletions Tricycle.Media.FFmpeg/FFmpegConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ protected override void Coalesce(FFmpegConfig userConfig, FFmpegConfig defaultCo
CoalesceAudio(userConfig.Audio, clone.Audio);
}

if (userConfig.Subtitles == null)
{
userConfig.Subtitles = clone.Subtitles;
}
else
{
CoalesceSubtitles(userConfig.Subtitles, clone.Subtitles);
}

if (userConfig.Video == null)
{
userConfig.Video = clone.Video;
Expand Down Expand Up @@ -67,6 +76,32 @@ void CoalesceAudio(AudioConfig userConfig, AudioConfig defaultConfig)
}
}

void CoalesceSubtitles(SubtitleConfig userConfig, SubtitleConfig defaultConfig)
{
if (userConfig.Codecs?.Any() != true)
{
userConfig.Codecs = defaultConfig.Codecs;
return;
}

if (defaultConfig.Codecs?.Any() != true)
{
return;
}

foreach (var pair in userConfig.Codecs)
{
var format = pair.Key;
var userCodec = pair.Value;
var defaultCodec = defaultConfig.Codecs.GetValueOrDefault(format);

if (string.IsNullOrWhiteSpace(userCodec.Name) && defaultCodec != null)
{
userCodec.Name = defaultCodec.Name;
}
}
}

void CoalesceVideo(VideoConfig userConfig, VideoConfig defaultConfig)
{
if (string.IsNullOrWhiteSpace(userConfig.CropDetectOptions))
Expand Down
4 changes: 3 additions & 1 deletion Tricycle.Media.FFmpeg/Models/Config/FFmpegConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ public class FFmpegConfig
{
public VideoConfig Video { get; set; }
public AudioConfig Audio { get; set; }
public SubtitleConfig Subtitles { get; set; }

public FFmpegConfig Clone()
{
return new FFmpegConfig()
{
Video = Video?.Clone(),
Audio = Audio?.Clone()
Audio = Audio?.Clone(),
Subtitles = Subtitles?.Clone()
};
}
}
Expand Down
22 changes: 22 additions & 0 deletions Tricycle.Media.FFmpeg/Models/Config/SubtitleCodec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
namespace Tricycle.Media.FFmpeg.Models.Config
{
public class SubtitleCodec
{
public string Name { get; set; }

public SubtitleCodec()
{
}

public SubtitleCodec(string name)
{
Name = name;
}

public SubtitleCodec Clone()
{
return new SubtitleCodec(Name);
}
}
}
20 changes: 20 additions & 0 deletions Tricycle.Media.FFmpeg/Models/Config/SubtitleConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Tricycle.Models;

namespace Tricycle.Media.FFmpeg.Models.Config
{
public class SubtitleConfig
{
public IDictionary<SubtitleFormat, SubtitleCodec> Codecs { get; set; }

public SubtitleConfig Clone()
{
return new SubtitleConfig()
{
Codecs = Codecs?.ToDictionary(p => p.Key, p => p.Value?.Clone())
};
}
}
}
9 changes: 8 additions & 1 deletion Tricycle.UI.macOS/Resources/Config/ffmpeg.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
"audio": {
"codecs": {
"aac": {
"name": "aac"
"name": "aac"
},
"ac3": {
"name": "ac3"
}
}
},
"subtitles": {
"codecs": {
"timedText": {
"name": "mov_text"
}
}
}
}

0 comments on commit 1aa43fd

Please sign in to comment.