Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Mar 25, 2024
1 parent 63e2984 commit d566754
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 3 additions & 10 deletions TwitchDownloaderCore/Tools/FfmpegMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static class FfmpegMetadata
private const string LINE_FEED = "\u000A";

public static async Task SerializeAsync(string filePath, string streamerName, string videoId, string videoTitle, DateTime videoCreation, int viewCount, string videoDescription = null,
TimeSpan startOffset = default, TimeSpan videoLength = default, IEnumerable<VideoMomentEdge> videoMomentEdges = null, CancellationToken cancellationToken = default)
TimeSpan startOffset = default, IEnumerable<VideoMomentEdge> videoMomentEdges = null, CancellationToken cancellationToken = default)
{
await using var fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read);
await using var sw = new StreamWriter(fs) { NewLine = LINE_FEED };

await SerializeGlobalMetadata(sw, streamerName, videoId, videoTitle, videoCreation, viewCount, videoDescription);
await fs.FlushAsync(cancellationToken);

await SerializeChapters(sw, videoMomentEdges, startOffset, videoLength);
await SerializeChapters(sw, videoMomentEdges, startOffset);
await fs.FlushAsync(cancellationToken);
}

Expand All @@ -43,7 +43,7 @@ private static async Task SerializeGlobalMetadata(StreamWriter sw, string stream
await sw.WriteLineAsync(@$"Views: {viewCount}");
}

private static async Task SerializeChapters(StreamWriter sw, IEnumerable<VideoMomentEdge> videoMomentEdges, TimeSpan startOffset, TimeSpan videoLength)
private static async Task SerializeChapters(StreamWriter sw, IEnumerable<VideoMomentEdge> videoMomentEdges, TimeSpan startOffset)
{
if (videoMomentEdges is null)
{
Expand All @@ -60,13 +60,6 @@ private static async Task SerializeChapters(StreamWriter sw, IEnumerable<VideoMo
}

var startMillis = momentEdge.node.positionMilliseconds - startOffsetMillis;
if (startMillis >= videoLength.TotalMilliseconds)
{
// Because of floating point error, if the video ends on the same second as a new chapter beings, FFmpeg will keep the chapter
// The kept chapter is less than a second long, but a user complained.
continue;
}

var lengthMillis = momentEdge.node.durationMilliseconds;
var gameName = momentEdge.node.details.game?.displayName ?? momentEdge.node.description;

Expand Down
2 changes: 1 addition & 1 deletion TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
string metadataPath = Path.Combine(downloadFolder, "metadata.txt");
VideoInfo videoInfo = videoInfoResponse.data.video;
await FfmpegMetadata.SerializeAsync(metadataPath, videoInfo.owner.displayName, downloadOptions.Id.ToString(), videoInfo.title, videoInfo.createdAt, videoInfo.viewCount,
videoInfo.description?.Replace(" \n", "\n").Replace("\n\n", "\n").TrimEnd(), downloadOptions.CropBeginningTime, seekDuration > TimeSpan.Zero ? seekDuration : videoLength,
videoInfo.description?.Replace(" \n", "\n").Replace("\n\n", "\n").TrimEnd(), downloadOptions.CropBeginningTime,
videoChapterResponse.data.video.moments.edges, cancellationToken);

var finalizedFileDirectory = Directory.GetParent(Path.GetFullPath(downloadOptions.Filename))!;
Expand Down

0 comments on commit d566754

Please sign in to comment.