add aac_adtstoasc bitstream filter for mpegts to mp4 conversion

This commit is contained in:
nyanmisaka 2020-11-14 14:22:15 +08:00
parent 536b054873
commit 32bb73acbb
3 changed files with 66 additions and 7 deletions

View File

@ -1444,7 +1444,19 @@ namespace Jellyfin.Api.Controllers
{
if (EncodingHelper.IsCopyCodec(audioCodec))
{
return "-acodec copy -strict -2";
var segmentFormat = HlsHelpers.GetSegmentFileExtension(state.Request.SegmentContainer).TrimStart('.');
var bitStreamArgs = string.Empty;
// Apply aac_adtstoasc bitstream filter when media source is in mpegts.
if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase)
&& (string.Equals(state.MediaSource.Container, "mpegts", StringComparison.OrdinalIgnoreCase)
|| string.Equals(state.MediaSource.Container, "hls", StringComparison.OrdinalIgnoreCase)))
{
bitStreamArgs = _encodingHelper.GetBitStreamArgs(state.AudioStream);
bitStreamArgs = !string.IsNullOrEmpty(bitStreamArgs) ? " " + bitStreamArgs : string.Empty;
}
return "-acodec copy -strict -2" + bitStreamArgs;
}
var audioTranscodeParams = new List<string>();
@ -1473,13 +1485,24 @@ namespace Jellyfin.Api.Controllers
if (EncodingHelper.IsCopyCodec(audioCodec))
{
var videoCodec = _encodingHelper.GetVideoEncoder(state, _encodingOptions);
var segmentFormat = HlsHelpers.GetSegmentFileExtension(state.Request.SegmentContainer).TrimStart('.');
var bitStreamArgs = string.Empty;
// Apply aac_adtstoasc bitstream filter when media source is in mpegts.
if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase)
&& (string.Equals(state.MediaSource.Container, "mpegts", StringComparison.OrdinalIgnoreCase)
|| string.Equals(state.MediaSource.Container, "hls", StringComparison.OrdinalIgnoreCase)))
{
bitStreamArgs = _encodingHelper.GetBitStreamArgs(state.AudioStream);
bitStreamArgs = !string.IsNullOrEmpty(bitStreamArgs) ? " " + bitStreamArgs : string.Empty;
}
if (EncodingHelper.IsCopyCodec(videoCodec) && state.EnableBreakOnNonKeyFrames(videoCodec))
{
return "-codec:a:0 copy -strict -2 -copypriorss:a:0 0";
return "-codec:a:0 copy -strict -2 -copypriorss:a:0 0" + bitStreamArgs;
}
return "-codec:a:0 copy -strict -2";
return "-codec:a:0 copy -strict -2" + bitStreamArgs;
}
var args = "-codec:a:0 " + audioCodec;

View File

@ -439,7 +439,19 @@ namespace Jellyfin.Api.Controllers
{
if (EncodingHelper.IsCopyCodec(audioCodec))
{
return "-acodec copy -strict -2";
var segmentFormat = HlsHelpers.GetSegmentFileExtension(state.Request.SegmentContainer).TrimStart('.');
var bitStreamArgs = string.Empty;
// Apply aac_adtstoasc bitstream filter when media source is in mpegts.
if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase)
&& (string.Equals(state.MediaSource.Container, "mpegts", StringComparison.OrdinalIgnoreCase)
|| string.Equals(state.MediaSource.Container, "hls", StringComparison.OrdinalIgnoreCase)))
{
bitStreamArgs = _encodingHelper.GetBitStreamArgs(state.AudioStream);
bitStreamArgs = !string.IsNullOrEmpty(bitStreamArgs) ? " " + bitStreamArgs : string.Empty;
}
return "-acodec copy -strict -2" + bitStreamArgs;
}
var audioTranscodeParams = new List<string>();
@ -467,7 +479,19 @@ namespace Jellyfin.Api.Controllers
if (EncodingHelper.IsCopyCodec(audioCodec))
{
return "-codec:a:0 copy -strict -2";
var segmentFormat = HlsHelpers.GetSegmentFileExtension(state.Request.SegmentContainer).TrimStart('.');
var bitStreamArgs = string.Empty;
// Apply aac_adtstoasc bitstream filter when media source is in mpegts.
if (string.Equals(segmentFormat, "mp4", StringComparison.OrdinalIgnoreCase)
&& (string.Equals(state.MediaSource.Container, "mpegts", StringComparison.OrdinalIgnoreCase)
|| string.Equals(state.MediaSource.Container, "hls", StringComparison.OrdinalIgnoreCase)))
{
bitStreamArgs = _encodingHelper.GetBitStreamArgs(state.AudioStream);
bitStreamArgs = !string.IsNullOrEmpty(bitStreamArgs) ? " " + bitStreamArgs : string.Empty;
}
return "-acodec copy -strict -2" + bitStreamArgs;
}
var args = "-codec:a:0 " + audioCodec;

View File

@ -596,10 +596,17 @@ namespace MediaBrowser.Controller.MediaEncoding
|| codec.IndexOf("hevc", StringComparison.OrdinalIgnoreCase) != -1;
}
// TODO This is auto inserted into the mpegts mux so it might not be needed
// https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmp4toannexb
public bool IsAAC(MediaStream stream)
{
var codec = stream.Codec ?? string.Empty;
return codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1;
}
public string GetBitStreamArgs(MediaStream stream)
{
// TODO This is auto inserted into the mpegts mux so it might not be needed
// https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmp4toannexb
if (IsH264(stream))
{
return "-bsf:v h264_mp4toannexb";
@ -608,6 +615,11 @@ namespace MediaBrowser.Controller.MediaEncoding
{
return "-bsf:v hevc_mp4toannexb";
}
else if (IsAAC(stream))
{
// convert adts header(mpegts) to asc header(mp4)
return "-bsf:a aac_adtstoasc";
}
else
{
return null;