From 8ee9a0adf9f4b1ea72a4bbe322ef7941975a4b70 Mon Sep 17 00:00:00 2001 From: Vincent Lark Date: Sat, 21 Oct 2023 23:57:05 +0200 Subject: [PATCH 1/2] Forward user_agent config to ffprobe --- .../Encoder/MediaEncoder.cs | 6 +++ .../Probing/ProbeExternalSourcesTests.cs | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 4668b8bbb..e8041f198 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -418,6 +418,7 @@ namespace MediaBrowser.MediaEncoding.Encoder public Task GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken) { var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters; + var requiredHeaders = request.MediaSource.RequiredHttpHeaders; var analyzeDuration = string.Empty; var ffmpegAnalyzeDuration = _config.GetFFmpegAnalyzeDuration() ?? string.Empty; var ffmpegProbeSize = _config.GetFFmpegProbeSize() ?? string.Empty; @@ -442,6 +443,11 @@ namespace MediaBrowser.MediaEncoding.Encoder extraArgs += " -probesize " + ffmpegProbeSize; } + if (requiredHeaders.ContainsKey("user_agent")) + { + extraArgs += " -user_agent " + requiredHeaders["user_agent"]; + } + return GetMediaInfoInternal( GetInputArgument(request.MediaSource.Path, request.MediaSource), request.MediaSource.Path, diff --git a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs new file mode 100644 index 000000000..17f8ec163 --- /dev/null +++ b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Threading; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.MediaEncoding.Encoder; +using MediaBrowser.Model.Globalization; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.MediaInfo; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Moq; +using Xunit; + +namespace Jellyfin.MediaEncoding.Tests.Probing +{ + public class ProbeExternalSourcesTests + { + [Fact] + public void GetMediaInfo_Uses_UserAgent() + { + var encoder = new MediaEncoder( + Mock.Of>(), + Mock.Of(), + Mock.Of(), + Mock.Of(), + Mock.Of(), + new ConfigurationBuilder().Build(), + Mock.Of()); + + var req = new MediaBrowser.Controller.MediaEncoding.MediaInfoRequest() + { + MediaSource = new MediaBrowser.Model.Dto.MediaSourceInfo + { + Path = "/path/to/stream", + Protocol = MediaProtocol.Http, + RequiredHttpHeaders = new Dictionary() + { + { "user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/530.35 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/530.35" }, + } + }, + ExtractChapters = false, + MediaType = MediaBrowser.Model.Dlna.DlnaProfileType.Video, + }; + + encoder.GetMediaInfo(req, CancellationToken.None); + } + } +} From 123c6e7d1bc3a9459eb54697c293763b753b7295 Mon Sep 17 00:00:00 2001 From: Vincent Lark Date: Thu, 26 Oct 2023 20:06:45 +0200 Subject: [PATCH 2/2] Extract the MediaEncoder probing command arguments builder --- .../Encoder/MediaEncoder.cs | 32 +++++++++++-------- .../Probing/ProbeExternalSourcesTests.cs | 11 ++++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index e8041f198..4dbefca4b 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -418,10 +418,24 @@ namespace MediaBrowser.MediaEncoding.Encoder public Task GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken) { var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters; - var requiredHeaders = request.MediaSource.RequiredHttpHeaders; - var analyzeDuration = string.Empty; + var extraArgs = GetExtraArguments(request); + + return GetMediaInfoInternal( + GetInputArgument(request.MediaSource.Path, request.MediaSource), + request.MediaSource.Path, + request.MediaSource.Protocol, + extractChapters, + extraArgs, + request.MediaType == DlnaProfileType.Audio, + request.MediaSource.VideoType, + cancellationToken); + } + + internal string GetExtraArguments(MediaInfoRequest request) + { var ffmpegAnalyzeDuration = _config.GetFFmpegAnalyzeDuration() ?? string.Empty; var ffmpegProbeSize = _config.GetFFmpegProbeSize() ?? string.Empty; + var analyzeDuration = string.Empty; var extraArgs = string.Empty; if (request.MediaSource.AnalyzeDurationMs > 0) @@ -443,20 +457,12 @@ namespace MediaBrowser.MediaEncoding.Encoder extraArgs += " -probesize " + ffmpegProbeSize; } - if (requiredHeaders.ContainsKey("user_agent")) + if (request.MediaSource.RequiredHttpHeaders.TryGetValue("user_agent", out var userAgent)) { - extraArgs += " -user_agent " + requiredHeaders["user_agent"]; + extraArgs += " -user_agent " + userAgent; } - return GetMediaInfoInternal( - GetInputArgument(request.MediaSource.Path, request.MediaSource), - request.MediaSource.Path, - request.MediaSource.Protocol, - extractChapters, - extraArgs, - request.MediaType == DlnaProfileType.Audio, - request.MediaSource.VideoType, - cancellationToken); + return extraArgs; } /// diff --git a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs index 17f8ec163..263f74c90 100644 --- a/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs +++ b/tests/Jellyfin.MediaEncoding.Tests/Probing/ProbeExternalSourcesTests.cs @@ -1,5 +1,5 @@ +using System; using System.Collections.Generic; -using System.Threading; using MediaBrowser.Controller.Configuration; using MediaBrowser.MediaEncoding.Encoder; using MediaBrowser.Model.Globalization; @@ -15,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Tests.Probing public class ProbeExternalSourcesTests { [Fact] - public void GetMediaInfo_Uses_UserAgent() + public void GetExtraArguments_Forwards_UserAgent() { var encoder = new MediaEncoder( Mock.Of>(), @@ -26,6 +26,7 @@ namespace Jellyfin.MediaEncoding.Tests.Probing new ConfigurationBuilder().Build(), Mock.Of()); + var userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"; var req = new MediaBrowser.Controller.MediaEncoding.MediaInfoRequest() { MediaSource = new MediaBrowser.Model.Dto.MediaSourceInfo @@ -34,14 +35,16 @@ namespace Jellyfin.MediaEncoding.Tests.Probing Protocol = MediaProtocol.Http, RequiredHttpHeaders = new Dictionary() { - { "user_agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/530.35 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/530.35" }, + { "user_agent", userAgent }, } }, ExtractChapters = false, MediaType = MediaBrowser.Model.Dlna.DlnaProfileType.Video, }; - encoder.GetMediaInfo(req, CancellationToken.None); + var extraArg = encoder.GetExtraArguments(req); + + Assert.Contains(userAgent, extraArg, StringComparison.InvariantCulture); } } }