Skip to content

Commit

Permalink
Escape arrow sequences when writing closed captions to an SRT file
Browse files Browse the repository at this point in the history
Closes #755
  • Loading branch information
Tyrrrz committed Dec 27, 2023
1 parent 4784fb3 commit f08ac9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions YoutubeExplode.Converter.Tests/SubtitleSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public async Task I_can_download_a_video_as_a_single_mp4_file_with_subtitles()
using var dir = TempDir.Create();
var filePath = Path.Combine(dir.Path, "video.mp4");

var streamManifest = await youtube.Videos.Streams.GetManifestAsync("YltHGKX80Y8");
var streamManifest = await youtube.Videos.Streams.GetManifestAsync("NtQkz0aRDe8");
var streamInfos = streamManifest
.GetVideoStreams()
.Where(s => s.Container == Container.Mp4)
.OrderBy(s => s.Size)
.Take(1)
.ToArray();

var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync("YltHGKX80Y8");
var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync("NtQkz0aRDe8");
var trackInfos = trackManifest.Tracks;

// Act
Expand Down Expand Up @@ -61,15 +61,15 @@ public async Task I_can_download_a_video_as_a_single_webm_file_with_subtitles()
using var dir = TempDir.Create();
var filePath = Path.Combine(dir.Path, "video.webm");

var streamManifest = await youtube.Videos.Streams.GetManifestAsync("YltHGKX80Y8");
var streamManifest = await youtube.Videos.Streams.GetManifestAsync("NtQkz0aRDe8");
var streamInfos = streamManifest
.GetVideoStreams()
.Where(s => s.Container == Container.WebM)
.OrderBy(s => s.Size)
.Take(1)
.ToArray();

var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync("YltHGKX80Y8");
var trackManifest = await youtube.Videos.ClosedCaptions.GetManifestAsync("NtQkz0aRDe8");
var trackInfos = trackManifest.Tracks;

// Act
Expand Down
15 changes: 14 additions & 1 deletion YoutubeExplode/Videos/ClosedCaptions/ClosedCaptionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,20 @@ static string FormatTimestamp(TimeSpan value) =>
.Append(FormatTimestamp(caption.Offset + caption.Duration))
.AppendLine()
// Content
.AppendLine(caption.Text);
.AppendLine(
caption
.Text
// Caption text may contain valid SRT-formatted data in itself.
// This can happen, for example, if the subtitles for a YouTube video
// were imported from an SRT file, but something went wrong in the
// process, resulting in parts of the file being read as captions
// rather than control sequences.
// SRT file format does not provide any means of escaping special
// characters, so as a workaround we just replace the dashes in the
// arrow sequence with en-dashes, which look similar enough.
// https://github.com/Tyrrrz/YoutubeExplode/issues/755
.Replace("-->", "––>")
);

await writer.WriteLineAsync(buffer.ToString());
buffer.Clear();
Expand Down

0 comments on commit f08ac9d

Please sign in to comment.