Skip to content

Commit 31194f3

Browse files
committed
fix: do not bake a zero duration into metadata created mid-recording
Stamping the match id at match end creates the video metadata while the recorder is still writing the file, and probing a fragmented mp4 before it is finalized yields no duration - the zero was cached forever and the video was listed as corrupted. Skip the probe while the file is the active recording, and re-probe any metadata whose duration is still zero once the file is finished.
1 parent e7d8a68 commit 31194f3

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Classes/Utils/Helpers.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,22 +435,37 @@ public static VideoMetadata GetMetadata(string videoPath) {
435435
}
436436
}
437437

438+
// Probing the file the recorder is still writing yields no duration (mp4 has
439+
// no moov atom yet) or a partial one, so it must wait until the recording stops.
440+
private static bool IsBeingRecorded(string videoPath) {
441+
return RecordingService.IsRecording &&
442+
string.Equals(RecordingService.GetCurrentSession()?.VideoSavePath, videoPath, StringComparison.OrdinalIgnoreCase);
443+
}
444+
438445
public static VideoMetadata GetOrCreateMetadata(string videoPath) {
439446
lock (_metafileLock) {
440447
string thumbsDir = Path.Combine(Path.GetDirectoryName(videoPath), ".thumbs/");
441448
string metadataPath = Path.Combine(thumbsDir, Path.GetFileNameWithoutExtension(videoPath) + ".metadata");
442449
var metadata = GetMetadata(videoPath);
443450

444451
if (metadata != null) {
452+
// a metadata created while its video was still recording has no
453+
// duration yet - fill it in once the file is finished
454+
if (metadata.duration == 0 && !IsBeingRecorded(videoPath)) {
455+
var probed = GetVideoDuration(videoPath);
456+
if (probed > 0) {
457+
metadata.duration = probed;
458+
File.WriteAllText(metadataPath, JsonSerializer.Serialize(metadata));
459+
}
460+
}
445461
return metadata;
446462
}
447463

448464
metadata = new VideoMetadata();
449465

450466
if (!Directory.Exists(thumbsDir)) Directory.CreateDirectory(thumbsDir);
451467

452-
var duration = GetVideoDuration(videoPath);
453-
metadata.duration = duration;
468+
metadata.duration = IsBeingRecorded(videoPath) ? 0 : GetVideoDuration(videoPath);
454469
metadata.filePath = metadataPath;
455470

456471
Logger.WriteLine($"Created video metadata for '{Path.GetFileName(videoPath)}'");

0 commit comments

Comments
 (0)