Skip to content

Commit

Permalink
Added subtitles as passthru stream when overlay is false.
Browse files Browse the repository at this point in the history
  • Loading branch information
kmcclive committed Mar 16, 2021
1 parent 0127f85 commit 8278a45
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
9 changes: 6 additions & 3 deletions Tricycle.Media.FFmpeg.Tests/FFmpegJobRunnerBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ public void MapAssignsCanvasSize()
_videoSource.Dimensions = new Dimensions(1920, 1080);
_transcodeJob.Subtitles = new SubtitlesConfig()
{
SourceStreamIndex = subtitleStream.Index
SourceStreamIndex = subtitleStream.Index,
Overlay = true
};

var ffmpegJob = _jobRunner.CallMap(_transcodeJob, null);
Expand Down Expand Up @@ -362,7 +363,8 @@ public void MapAddsGraphicSubtitleFilters()
_transcodeJob.SourceInfo.Streams.Add(subtitleStream);
_transcodeJob.Subtitles = new SubtitlesConfig()
{
SourceStreamIndex = subtitleStream.Index
SourceStreamIndex = subtitleStream.Index,
Overlay = true
};

var ffmpegJob = _jobRunner.CallMap(_transcodeJob, null);
Expand Down Expand Up @@ -426,7 +428,8 @@ public void MapAddsTextSubtitleFilter()
});
_transcodeJob.Subtitles = new SubtitlesConfig()
{
SourceStreamIndex = subtitleStream.Index
SourceStreamIndex = subtitleStream.Index,
Overlay = true
};

var ffmpegJob = _jobRunner.CallMap(_transcodeJob, null);
Expand Down
26 changes: 26 additions & 0 deletions Tricycle.Media.FFmpeg.Tests/MediaTranscoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ public void StartAddsPassthruStreamOnJob()
Assert.AreEqual("copy", stream.Codec?.Name);
}

[TestMethod]
public void StartAddsPassthruStreamForSubtitlesOnJob()
{
int index = 1;

_transcodeJob.SourceInfo.Streams.Add(new SubtitleStreamInfo()
{
Index = index
});
_transcodeJob.Subtitles = new SubtitlesConfig()
{
SourceStreamIndex = index,
Overlay = false
};

_transcoder.Start(_transcodeJob);

Assert.AreEqual(2, _ffmpegJob.Streams?.Count);

var stream = _ffmpegJob.Streams[1];

Assert.AreEqual(0, stream.Input?.FileIndex);
Assert.AreEqual(index, stream.Input?.StreamIndex);
Assert.AreEqual("copy", stream.Codec?.Name);
}

[TestMethod]
public void StartAddsAacAudioStreamOnJob()
{
Expand Down
31 changes: 23 additions & 8 deletions Tricycle.Media.FFmpeg/FFmpegJobRunnerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected class SubtitleInfo
public int RelativeIndex { get; set; }
public SubtitleType SubtitleType { get; set; }
public string FileName { get; set; }
public bool Overlay { get; set; }
}

protected IConfigManager<FFmpegConfig> _configManager;
Expand Down Expand Up @@ -100,7 +101,8 @@ protected virtual FFmpegJob Map(TranscodeJob job, FFmpegConfig config)
AbsoluteIndex = s.Index,
RelativeIndex = i++,
SubtitleType = s.SubtitleType,
FileName = job.SourceInfo.FileName
FileName = job.SourceInfo.FileName,
Overlay = job.Subtitles.Overlay
})
.FirstOrDefault(s =>
s.AbsoluteIndex == job.Subtitles.SourceStreamIndex);
Expand All @@ -116,7 +118,10 @@ protected virtual FFmpegJob Map(TranscodeJob job, FFmpegConfig config)
result.ForcedSubtitlesOnly = true;
}

result.CanvasSize = videoSource.Dimensions;
if (job.Subtitles.Overlay)
{
result.CanvasSize = videoSource.Dimensions;
}
}

result.Streams = MapStreams(config, job);
Expand All @@ -136,20 +141,30 @@ protected virtual IList<MappedStream> MapStreams(FFmpegConfig config, TranscodeJ
{
IList<MappedStream> result = new List<MappedStream>();
IDictionary<int, StreamInfo> sourceStreamsByIndex = job.SourceInfo.Streams.ToDictionary(s => s.Index);
IEnumerable<OutputStream> jobStreams = job.Streams;

if (job.Subtitles?.Overlay == false)
{
var subtitleStream = new OutputStream()
{
SourceStreamIndex = job.Subtitles.SourceStreamIndex
};

jobStreams = jobStreams.Concat(new OutputStream[]{ subtitleStream });
}

for (int i = 0; i < job.Streams.Count; i++)
foreach (var jobStream in jobStreams)
{
OutputStream outputStream = job.Streams[i];
StreamInfo sourceStream;

if (!sourceStreamsByIndex.TryGetValue(outputStream.SourceStreamIndex, out sourceStream))
if (!sourceStreamsByIndex.TryGetValue(jobStream.SourceStreamIndex, out sourceStream))
{
throw new ArgumentException(
$"{nameof(job.SourceInfo)} does not contain a stream with index {outputStream.SourceStreamIndex}.",
$"{nameof(job.SourceInfo)} does not contain a stream with index {jobStream.SourceStreamIndex}.",
nameof(job.SourceInfo));
}

MappedStream mappedStream = MapStream(config, sourceStream, outputStream);
MappedStream mappedStream = MapStream(config, sourceStream, jobStream);

if (mappedStream != null)
{
Expand Down Expand Up @@ -205,7 +220,7 @@ protected virtual IList<IFilter> GetVideoFilters(FFmpegConfig config,
{
var result = new List<IFilter>();

if (subtitleInfo != null)
if (subtitleInfo?.Overlay == true)
{
if (subtitleInfo.SubtitleType == SubtitleType.Graphic)
{
Expand Down
1 change: 1 addition & 0 deletions Tricycle.Models/Config/TricycleConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class TricycleConfig
public VideoConfig Video { get; set; }
public AudioConfig Audio { get; set; }
public bool ForcedSubtitlesOnly { get; set; }
public bool OverlaySubtitles { get; set; } = true;
public IDictionary<ContainerFormat, string> DefaultFileExtensions { get; set; }
public bool CompletionAlert { get; set; }
public bool DeleteIncompleteFiles { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Tricycle.Models/Jobs/SubtitlesConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class SubtitlesConfig
{
public int SourceStreamIndex { get; set; }
public bool ForcedOnly { get; set; }
public bool Overlay { get; set; }
}
}

0 comments on commit 8278a45

Please sign in to comment.