Skip to content

Commit

Permalink
Support changing stream IDs depending on container type
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Nov 11, 2024
1 parent d42d70b commit 73157cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
28 changes: 21 additions & 7 deletions TwitchDownloaderCore/Tools/FfmpegConcatList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -12,7 +13,7 @@ public static class FfmpegConcatList
{
private const string LINE_FEED = "\u000A";

public static async Task SerializeAsync(string filePath, M3U8 playlist, Range videoListCrop, CancellationToken cancellationToken = default)
public static async Task SerializeAsync(string filePath, M3U8 playlist, Range videoListCrop, StreamIds streamIds, CancellationToken cancellationToken = default)
{
await using var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read);
await using var sw = new StreamWriter(fs) { NewLine = LINE_FEED };
Expand All @@ -27,16 +28,29 @@ public static async Task SerializeAsync(string filePath, M3U8 playlist, Range vi
await sw.WriteAsync(DownloadTools.RemoveQueryString(stream.Path));
await sw.WriteLineAsync('\'');

await sw.WriteLineAsync("stream");
await sw.WriteLineAsync("exact_stream_id 0x100"); // Audio
await sw.WriteLineAsync("stream");
await sw.WriteLineAsync("exact_stream_id 0x101"); // Video
await sw.WriteLineAsync("stream");
await sw.WriteLineAsync("exact_stream_id 0x102"); // Subtitle
foreach (var id in streamIds.Ids)
{
await sw.WriteLineAsync("stream");
await sw.WriteLineAsync($"exact_stream_id {id}");
}

await sw.WriteAsync("duration ");
await sw.WriteLineAsync(stream.PartInfo.Duration.ToString(CultureInfo.InvariantCulture));
}
}

public record StreamIds
{
public static readonly StreamIds TransportStream = new("0x100", "0x101", "0x102");
public static readonly StreamIds Mp4 = new("0x1", "0x2");
public static readonly StreamIds None = new();

private StreamIds(params string[] ids)
{
Ids = ids;
}

public IEnumerable<string> Ids { get; }
}
}
}
17 changes: 16 additions & 1 deletion TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ await FfmpegMetadata.SerializeAsync(metadataPath, downloadOptions.Id.ToString(),
videoChapterResponse.data.video.moments.edges);

var concatListPath = Path.Combine(downloadFolder, "concat.txt");
await FfmpegConcatList.SerializeAsync(concatListPath, playlist, videoListCrop, cancellationToken);
var streamIds = GetStreamIds(playlist);
await FfmpegConcatList.SerializeAsync(concatListPath, playlist, videoListCrop, streamIds, cancellationToken);

outputFs.Close();

Expand Down Expand Up @@ -318,6 +319,20 @@ private async Task VerifyDownloadedParts(ICollection<M3U8.Stream> playlist, Rang
}
}

private FfmpegConcatList.StreamIds GetStreamIds(M3U8 playlist)
{
var path = DownloadTools.RemoveQueryString(playlist.Streams.FirstOrDefault()?.Path ?? "");
var extension = Path.GetExtension(path);
if (extension is ".mp4")
return FfmpegConcatList.StreamIds.Mp4;

if (extension is ".ts")
return FfmpegConcatList.StreamIds.TransportStream;

_progress.LogWarning("No file extension was found! Assuming TS.");
return FfmpegConcatList.StreamIds.TransportStream;
}

private async Task<int> RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength, bool disableAudioCopy, CancellationToken cancellationToken)
{
using var process = new Process
Expand Down

0 comments on commit 73157cf

Please sign in to comment.