Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit e8183a5

Browse files
author
Marcos Cordeiro
committed
Prefer mp4 over mkv if no re-encoding is done
This also removes -shortest that was used earlier
1 parent cde2074 commit e8183a5

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

Wasari.App/GenericDownloadService.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ namespace Wasari.App;
1111

1212
public class GenericDownloadService : IDownloadService
1313
{
14-
public GenericDownloadService(ILogger<GenericDownloadService> logger, FFmpegService fFmpegService, IOptions<DownloadOptions> options, YoutubeDlpService youtubeDlpService)
14+
public GenericDownloadService(ILogger<GenericDownloadService> logger, FFmpegService fFmpegService, IOptions<DownloadOptions> options, YoutubeDlpService youtubeDlpService, IOptions<FFmpegOptions> fFmpegOptions)
1515
{
1616
Logger = logger;
1717
FFmpegService = fFmpegService;
1818
Options = options;
1919
YoutubeDlpService = youtubeDlpService;
20+
FFmpegOptions = fFmpegOptions;
2021
}
2122

2223
protected ILogger<GenericDownloadService> Logger { get; }
2324

25+
2426
private IOptions<DownloadOptions> Options { get; }
27+
28+
private IOptions<FFmpegOptions> FFmpegOptions { get; }
2529

2630
private FFmpegService FFmpegService { get; }
2731

@@ -107,7 +111,7 @@ private async Task<DownloadedEpisode> DownloadEpisode(WasariEpisode episode, Dow
107111
return new DownloadedEpisode(filepath, sucess ? DownloadedEpisodeStatus.Downloaded : DownloadedEpisodeStatus.Failed, episode);
108112
}
109113

110-
private static StringBuilder BuildEpisodeName(IWasariEpisode episode)
114+
private StringBuilder BuildEpisodeName(IWasariEpisode episode)
111115
{
112116
var episodeNameBuilder = new StringBuilder();
113117

@@ -118,7 +122,7 @@ private static StringBuilder BuildEpisodeName(IWasariEpisode episode)
118122
if (episode.Number.HasValue || episode.SeasonNumber.HasValue)
119123
episodeNameBuilder.Append(" - ");
120124
episodeNameBuilder.Append(episode.Title);
121-
episodeNameBuilder.Append(".mkv");
125+
episodeNameBuilder.Append(FFmpegOptions.Value.UseHevc ? ".mkv" : ".mp4");
122126
return episodeNameBuilder;
123127
}
124128
}

Wasari.Crunchyroll/CrunchyrollDownloadService.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ namespace Wasari.Crunchyroll;
1818
internal class CrunchyrollDownloadService : GenericDownloadService
1919
{
2020
public CrunchyrollDownloadService(ILogger<CrunchyrollDownloadService> logger, FFmpegService fFmpegService, IOptions<DownloadOptions> options, YoutubeDlpService youtubeDlpService, CrunchyrollApiService crunchyrollApiService, IOptions<DownloadOptions> downloadOptions,
21-
IServiceProvider serviceProvider, IOptions<AuthenticationOptions> authenticationOptions) : base(logger, fFmpegService,
21+
IServiceProvider serviceProvider, IOptions<AuthenticationOptions> authenticationOptions, IOptions<FFmpegOptions> fFmpegOptions) : base(logger, fFmpegService,
2222
options,
23-
youtubeDlpService)
23+
youtubeDlpService, fFmpegOptions)
2424
{
2525
CrunchyrollApiService = crunchyrollApiService;
2626
DownloadOptions = downloadOptions;

Wasari.FFmpeg/FFmpegService.cs

+47-23
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,13 @@ private async IAsyncEnumerable<string> BuildArgumentsForEpisode(IWasariEpisode e
154154
{
155155
yield return "-c:v copy";
156156
}
157-
158-
yield return "-shortest -fflags shortest -max_interleave_delta 100M";
157+
158+
var fileExtension = Path.GetExtension(filePath);
159+
var isMp4 = fileExtension == ".mp4";
160+
161+
if (isMp4)
162+
yield return "-c:s mov_text";
163+
159164
yield return "-y";
160165
yield return $"\"{filePath}\"";
161166
}
@@ -166,15 +171,49 @@ private static Command CreateCommand()
166171
.WithWorkingDirectory(Environment.CurrentDirectory);
167172
}
168173

169-
private string? GetTemporaryFile()
174+
private string? GetTemporaryFile(string? baseFilePath = null)
170175
{
176+
if (baseFilePath != null)
177+
{
178+
var fileName = Path.GetFileNameWithoutExtension(baseFilePath);
179+
var fileExtension = Path.GetExtension(baseFilePath);
180+
var fileDirectory = Path.GetDirectoryName(baseFilePath);
181+
182+
return $"{fileDirectory}{Path.DirectorySeparatorChar}{fileName}_wasari_tmp.{fileExtension}";
183+
}
184+
171185
if (!Options.Value.UseTemporaryEncodingPath)
172186
return null;
173187

174188
var tempFileName = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
175-
return Path.Combine(Path.GetTempPath(), $"{tempFileName}.mkv");
189+
return Path.Combine(Path.GetTempPath(), $"{tempFileName}.mp4");
176190
}
177191

192+
public TimeSpan? GetVideoDuration(IMediaAnalysis mediaAnalysis)
193+
{
194+
var videoDuration = mediaAnalysis.VideoStreams.Max(o => o.Duration);
195+
196+
if (videoDuration == TimeSpan.Zero && (mediaAnalysis.PrimaryVideoStream?.Tags?.TryGetValue("DURATION", out var durationStr) ?? false))
197+
{
198+
var match = DurationRegex().Match(durationStr);
199+
var originalTrail = match.Groups["trail"].Value;
200+
var correctedTrail = originalTrail.TrimEnd('0');
201+
durationStr = durationStr.Replace(originalTrail, correctedTrail);
202+
203+
if (TimeSpan.TryParse(durationStr, out var newVideoDuratio))
204+
{
205+
videoDuration = newVideoDuratio;
206+
}
207+
else
208+
{
209+
Logger.LogWarning("Failed to parse duration string: {DurationStr}", durationStr);
210+
return null;
211+
}
212+
}
213+
214+
return videoDuration == TimeSpan.Zero ? null : videoDuration;
215+
}
216+
178217
public async Task<bool> CheckIfVideoStreamIsValid(string filePath)
179218
{
180219
try
@@ -184,25 +223,10 @@ public async Task<bool> CheckIfVideoStreamIsValid(string filePath)
184223
if (fileAnalysis.ErrorData.Count > 0)
185224
return false;
186225

187-
var videoDuration = fileAnalysis.VideoStreams.Max(o => o.Duration);
188-
189-
if (videoDuration == TimeSpan.Zero && (fileAnalysis.PrimaryVideoStream?.Tags?.TryGetValue("DURATION", out var durationStr) ?? false))
190-
{
191-
var match = DurationRegex().Match(durationStr);
192-
var originalTrail = match.Groups["trail"].Value;
193-
var correctedTrail = originalTrail.TrimEnd('0');
194-
durationStr = durationStr.Replace(originalTrail, correctedTrail);
226+
var videoDuration = GetVideoDuration(fileAnalysis);
195227

196-
if (TimeSpan.TryParse(durationStr, out var newVideoDuratio))
197-
{
198-
videoDuration = newVideoDuratio;
199-
}
200-
else
201-
{
202-
Logger.LogWarning("Failed to parse duration string: {DurationStr}", durationStr);
203-
return false;
204-
}
205-
}
228+
if(videoDuration == null)
229+
return false;
206230

207231
var delta = fileAnalysis.Duration - videoDuration;
208232
var isValid = videoDuration >= fileAnalysis.Duration || delta < TimeSpan.FromSeconds(10);
@@ -230,7 +254,7 @@ public async Task DownloadEpisode<T>(T episode, string filePath, IProgress<FFmpe
230254
.WithArguments(arguments, false);
231255

232256
await foreach (var commandEvent in ffmpegCommand.ListenAsync()) ProcessEvent(episode, progress, commandEvent, ffmpegCommand);
233-
257+
234258
if (tempFileName != null)
235259
{
236260
var destFileTempName = $"{filePath}.wasari_tmp";

0 commit comments

Comments
 (0)