From cf4113bc33f99219bb56914d6248ffdc3869a3ba Mon Sep 17 00:00:00 2001 From: reven Date: Mon, 3 Jan 2022 17:29:14 +1300 Subject: [PATCH] added subtitle extractor --- VideoNodes/VideoInfoHelper.cs | 4 + VideoNodes/VideoNodes.csproj | Bin 3922 -> 3922 bytes VideoNodes/VideoNodes.en.json | 11 ++- VideoNodes/VideoNodes/SubtitleExtractor.cs | 87 +++++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 VideoNodes/VideoNodes/SubtitleExtractor.cs diff --git a/VideoNodes/VideoInfoHelper.cs b/VideoNodes/VideoInfoHelper.cs index 50526110..8f26a3f1 100644 --- a/VideoNodes/VideoInfoHelper.cs +++ b/VideoNodes/VideoInfoHelper.cs @@ -63,6 +63,8 @@ namespace FileFlows.VideoNodes var rgxStreams = new Regex(@"Stream\s#[\d]+:[\d]+(.*?)(?=(Stream\s#[\d]|$))", RegexOptions.Singleline); var streamMatches = rgxStreams.Matches(output); int streamIndex = 0; + + int subtitleIndex = 1; foreach (Match sm in streamMatches) { if (sm.Value.Contains(" Video: ")) @@ -95,11 +97,13 @@ namespace FileFlows.VideoNodes if (sub != null) { sub.Index = streamIndex; + sub.TypeIndex = subtitleIndex; var match = Regex.Match(sm.Value, @"(?<=(Stream #))[\d]+:[\d]+"); if (match.Success) sub.IndexString = match.Value; vi.SubtitleStreams.Add(sub); } + ++subtitleIndex; } ++streamIndex; } diff --git a/VideoNodes/VideoNodes.csproj b/VideoNodes/VideoNodes.csproj index 9cdae0edaccc6023396f305c8183728cd99ee107..e2b6688791d56c0f14214cf5568f90142baa3cb1 100644 GIT binary patch delta 24 dcmca4cS&x;Jtk%|2E)k@nL 2; + + public override string Icon => "fas fa-comment-dots"; + + [Text(1)] + public string Language { get; set; } + + [File(2)] + public string OutputFile { get; set; } + + public override int Execute(NodeParameters args) + { + try + { + VideoInfo videoInfo = GetVideoInfo(args); + if (videoInfo == null) + return -1; + + string ffmpegExe = GetFFMpegExe(args); + if (string.IsNullOrEmpty(ffmpegExe)) + return -1; + + List ffArgs = new List(); + + // ffmpeg -i input.mkv -map "0:m:language:eng" -map "-0:v" -map "-0:a" output.srt + var subTrack = videoInfo.SubtitleStreams?.Where(x => string.IsNullOrEmpty(Language) || x.Language?.ToLower() == Language.ToLower()).FirstOrDefault(); + if (subTrack == null) + { + args.Logger?.ILog("No subtitles found to extract"); + return 2; + } + + if (string.IsNullOrEmpty(OutputFile) == false) + { + OutputFile = args.ReplaceVariables(OutputFile, true); + } + else + { + var file = new FileInfo(args.FileName); + OutputFile = file.FullName.Substring(0, file.FullName.LastIndexOf(file.Extension)) + ".srt"; + } + OutputFile = args.MapPath(OutputFile); + + + if (File.Exists(OutputFile)) + { + args.Logger?.ILog("File already exists, deleting file: " + OutputFile); + File.Delete(OutputFile); + } + + // -y means it will overwrite a file if output already exists + var result = args.Process.ExecuteShellCommand(new ExecuteArgs + { + Command = ffmpegExe, + Arguments = $"-i \"{args.WorkingFile}\" -map \"0:s:{subTrack.TypeIndex}\" -map \"-0:v\" -map \"-0:a\" \"{OutputFile}\"" + }).Result; + + if (result.ExitCode == 0) + { + return 1; + } + + args.Logger?.ELog("FFMPEG process failed to extract subtitles"); + args.Logger?.ILog("Unexpected exit code: " + result.ExitCode); + args.Logger?.ILog(result.StandardOutput ?? String.Empty); + args.Logger?.ILog(result.StandardError ?? String.Empty); + return -1; + } + catch (Exception ex) + { + args.Logger?.ELog("Failed processing VideoFile: " + ex.Message); + return -1; + } + } + } +}