@@ -154,8 +154,13 @@ private async IAsyncEnumerable<string> BuildArgumentsForEpisode(IWasariEpisode e
154
154
{
155
155
yield return "-c:v copy" ;
156
156
}
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
+
159
164
yield return "-y" ;
160
165
yield return $ "\" { filePath } \" ";
161
166
}
@@ -166,15 +171,49 @@ private static Command CreateCommand()
166
171
. WithWorkingDirectory ( Environment . CurrentDirectory ) ;
167
172
}
168
173
169
- private string ? GetTemporaryFile ( )
174
+ private string ? GetTemporaryFile ( string ? baseFilePath = null )
170
175
{
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
+
171
185
if ( ! Options . Value . UseTemporaryEncodingPath )
172
186
return null ;
173
187
174
188
var tempFileName = Path . GetFileNameWithoutExtension ( Path . GetTempFileName ( ) ) ;
175
- return Path . Combine ( Path . GetTempPath ( ) , $ "{ tempFileName } .mkv ") ;
189
+ return Path . Combine ( Path . GetTempPath ( ) , $ "{ tempFileName } .mp4 ") ;
176
190
}
177
191
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
+
178
217
public async Task < bool > CheckIfVideoStreamIsValid ( string filePath )
179
218
{
180
219
try
@@ -184,25 +223,10 @@ public async Task<bool> CheckIfVideoStreamIsValid(string filePath)
184
223
if ( fileAnalysis . ErrorData . Count > 0 )
185
224
return false ;
186
225
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 ) ;
195
227
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 ;
206
230
207
231
var delta = fileAnalysis . Duration - videoDuration ;
208
232
var isValid = videoDuration >= fileAnalysis . Duration || delta < TimeSpan . FromSeconds ( 10 ) ;
@@ -230,7 +254,7 @@ public async Task DownloadEpisode<T>(T episode, string filePath, IProgress<FFmpe
230
254
. WithArguments ( arguments , false ) ;
231
255
232
256
await foreach ( var commandEvent in ffmpegCommand . ListenAsync ( ) ) ProcessEvent ( episode , progress , commandEvent , ffmpegCommand ) ;
233
-
257
+
234
258
if ( tempFileName != null )
235
259
{
236
260
var destFileTempName = $ "{ filePath } .wasari_tmp";
0 commit comments