/// <summary> /// Processes the media subtitles and finds and handles external subtitle files /// </summary> /// <param name="config">The <see cref="DashConfig"/></param> /// <param name="subtitleFiles">The subtitle stream files</param> /// <param name="startFileIndex">The index additional subtitles need to start at. This should be the max index of the ffmpeg pieces +1</param> private List <StreamSubtitleFile> ProcessSubtitles(DashConfig config, IEnumerable <StreamSubtitleFile> subtitleFiles, int startFileIndex) { // Move subtitles found in media List <StreamSubtitleFile> subtitles = new List <StreamSubtitleFile>(); foreach (var subFile in subtitleFiles) { string oldPath = subFile.Path; subFile.Path = Path.Combine(config.OutputDirectory, Path.GetFileName(subFile.Path)); subtitles.Add(subFile); if (oldPath != subFile.Path) { if (File.Exists(subFile.Path)) { File.Delete(subFile.Path); } File.Move(oldPath, subFile.Path); } } // Add external subtitles string baseFilename = Path.GetFileNameWithoutExtension(config.InputFilePath); foreach (var vttFile in Directory.EnumerateFiles(Path.GetDirectoryName(config.InputFilePath), baseFilename + "*", SearchOption.TopDirectoryOnly)) { if (vttFile.EndsWith(".vtt")) { string vttFilename = Path.GetFileName(vttFile); string vttName = GetSubtitleName(vttFilename); string vttOutputPath = Path.Combine(config.OutputDirectory, $"{config.OutputFileName}_subtitle_{vttName}_{startFileIndex}.vtt"); var subFile = new StreamSubtitleFile() { Type = StreamType.Subtitle, Index = startFileIndex, Path = vttOutputPath, Language = $"{vttName}_{startFileIndex}" }; startFileIndex++; File.Copy(vttFile, vttOutputPath, true); subtitles.Add(subFile); } } return(subtitles); }
public FFmpegCommandBuilder WithSubtitleCommands(IEnumerable <MediaStream> streams) { if (!streams.Any()) { return(this); } foreach (MediaStream subtitleStream in streams) { if (!Constants.SupportedSubtitleCodecs.Contains(subtitleStream.codec_name)) { continue; } string language = subtitleStream.tag .Where(x => x.key == "language") .Select(x => x.value) .FirstOrDefault(); if (language is null) { language = "und"; } string path = Path.Combine(outputDirectory, $"{outputBaseFilename}_subtitle_{language}_{subtitleStream.index}.vtt"); StreamSubtitleFile command = new StreamSubtitleFile() { Type = StreamType.Subtitle, Index = subtitleStream.index, Language = language, Path = path, Argument = string.Join(" ", new string[] { $"-map 0:{subtitleStream.index}", '"' + path + '"' }) }; subtitleFiles.Add(command); } return(this); }