Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions AudioExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using NAudio.Wave;

namespace SubtitleCreator
Expand All @@ -28,6 +29,12 @@ public static string ExtractAudioFromVideoFile(string videoFilePath, bool attemp
string newVideoFilePath = videoFilePath;
bool tempFileCreated = false;

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Utilities.ConsoleWithLog("Running on non-Windows platform, using ffmpeg for audio extraction.");
return ExtractAudioWithFfmpeg(videoFilePath, ffmpegPath);
}

if (Path.GetExtension(videoFilePath).ToLower() == ".mkv")
{
Utilities.ConsoleWithLog("Input file is in a MKV container. Extracting the video.");
Expand Down Expand Up @@ -129,7 +136,7 @@ private static string RepairAudio(string videoFilePath, string ffmpegPath)
// Set up the process to run FFmpeg
using (Process ffmpeg = new Process())
{
ffmpeg.StartInfo.FileName = $"\"{ffmpegPath}\\ffmpeg\"";
ffmpeg.StartInfo.FileName = Path.Combine(ffmpegPath, "ffmpeg");
ffmpeg.StartInfo.Arguments = ffmpegArgs;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.UseShellExecute = false;
Expand All @@ -155,7 +162,7 @@ private static string ExtractVideoFromMKV(string videoFilePath, string ffmpegPat
// Set up the process to run FFmpeg
using (Process ffmpeg = new Process())
{
ffmpeg.StartInfo.FileName = $"\"{ffmpegPath}\\ffmpeg\"";
ffmpeg.StartInfo.FileName = Path.Combine(ffmpegPath, "ffmpeg");
ffmpeg.StartInfo.Arguments = ffmpegArgs;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.UseShellExecute = false;
Expand All @@ -171,5 +178,26 @@ private static string ExtractVideoFromMKV(string videoFilePath, string ffmpegPat

return intermediateFilePath;
}

private static string ExtractAudioWithFfmpeg(string videoFilePath, string ffmpegPath)
{
outputFilePath = Path.Combine(Path.GetDirectoryName(videoFilePath) ?? string.Empty, $"{Path.GetFileNameWithoutExtension(videoFilePath)}{fileNameIdentifier}.wav");
string ffmpegArgs = $"-i \"{videoFilePath}\" -vn -acodec pcm_s16le -ar 16000 -ac 1 \"{outputFilePath}\"";

using (Process ffmpeg = new Process())
{
ffmpeg.StartInfo.FileName = Path.Combine(ffmpegPath, "ffmpeg");
ffmpeg.StartInfo.Arguments = ffmpegArgs;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.CreateNoWindow = true;

ffmpeg.Start();
string output = ffmpeg.StandardError.ReadToEnd();
ffmpeg.WaitForExit();
}

return outputFilePath;
}
}
}
2 changes: 1 addition & 1 deletion CreateTheSubtitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public bool DoWorkMergeSubtitles(string srtFile, string inFile, string finalFile
// Set up the process to run FFmpeg
using (Process ffmpeg = new Process())
{
ffmpeg.StartInfo.FileName = $"\"{ffmpegPath}\\ffmpeg\"";
ffmpeg.StartInfo.FileName = Path.Combine(ffmpegPath, "ffmpeg");
ffmpeg.StartInfo.Arguments = ffmpegArgs;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.UseShellExecute = false;
Expand Down