From 7ced157f52f12abd2492e225be363bd36d684d65 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Wed, 23 Dec 2020 18:21:31 +0100 Subject: [PATCH] Fixed Audio support for non-MP4 formats --- Code/AudioVideo/FFmpegCommands.cs | 25 ++++++++++++++----------- Code/AudioVideo/FFmpegUtils.cs | 19 +++++++++++++++++++ Code/Main/Interpolate.cs | 2 +- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Code/AudioVideo/FFmpegCommands.cs b/Code/AudioVideo/FFmpegCommands.cs index b41e36f..05cb78a 100644 --- a/Code/AudioVideo/FFmpegCommands.cs +++ b/Code/AudioVideo/FFmpegCommands.cs @@ -1,4 +1,5 @@ -using Flowframes.Data; +using Flowframes.AudioVideo; +using Flowframes.Data; using Flowframes.IO; using Flowframes.Main; using System; @@ -204,9 +205,9 @@ namespace Flowframes public static async Task ExtractAudio(string inputFile, string outFile) // https://stackoverflow.com/a/27413824/14274419 { Logger.Log($"[FFCmds] Extracting audio from {inputFile} to {outFile}", true); - string ext = GetAudioExt(inputFile); - outFile = Path.ChangeExtension(outFile, ext); - string args = $" -loglevel panic -i {inputFile.Wrap()} -vn -acodec copy {outFile.Wrap()}"; + //string ext = GetAudioExt(inputFile); + outFile = Path.ChangeExtension(outFile, ".ogg"); + string args = $" -loglevel panic -i {inputFile.Wrap()} -vn -acodec libopus -b:a 320k {outFile.Wrap()}"; await AvProcess.RunFfmpeg(args, AvProcess.LogMode.Hidden); if (AvProcess.lastOutputFfmpeg.ToLower().Contains("error") && File.Exists(outFile)) // If broken file was written File.Delete(outFile); @@ -227,13 +228,15 @@ namespace Flowframes public static async Task MergeAudio(string inputFile, string audioPath, int looptimes = -1) // https://superuser.com/a/277667 { Logger.Log($"[FFCmds] Merging audio from {audioPath} into {inputFile}", true); - string tempPath = inputFile + "-temp.mp4"; - if (Path.GetExtension(audioPath) == ".wav") - { - Logger.Log("Using MKV instead of MP4 to enable support for raw audio."); - tempPath = Path.ChangeExtension(tempPath, "mkv"); - } - string args = $" -i {inputFile.Wrap()} -stream_loop {looptimes} -i {audioPath.Wrap()} -shortest -c copy {tempPath.Wrap()}"; + string tempPath = inputFile + "-temp" + Path.GetExtension(inputFile); + // if (Path.GetExtension(audioPath) == ".wav") + // { + // Logger.Log("Using MKV instead of MP4 to enable support for raw audio."); + // tempPath = Path.ChangeExtension(tempPath, "mkv"); + // } + string aCodec = Utils.GetAudioEnc(Utils.GetCodec(Interpolate.current.outMode)); + int aKbits = Utils.GetAudioBitrate(aCodec); + string args = $" -i {inputFile.Wrap()} -stream_loop {looptimes} -i {audioPath.Wrap()} -shortest -c:v copy -c:a {aCodec} -b:a {aKbits}k {tempPath.Wrap()}"; await AvProcess.RunFfmpeg(args, AvProcess.LogMode.Hidden); if (AvProcess.lastOutputFfmpeg.Contains("Invalid data")) { diff --git a/Code/AudioVideo/FFmpegUtils.cs b/Code/AudioVideo/FFmpegUtils.cs index 4107e68..96bed09 100644 --- a/Code/AudioVideo/FFmpegUtils.cs +++ b/Code/AudioVideo/FFmpegUtils.cs @@ -60,6 +60,25 @@ namespace Flowframes.AudioVideo return args; } + public static string GetAudioEnc(Codec codec) + { + switch (codec) + { + case Codec.VP9: return "libopus"; + } + return "aac"; + } + + public static int GetAudioBitrate (string codec) + { + if (codec.Trim().ToLower() == "aac") + return 128; + if (codec.Trim().ToLower().Contains("opus")) + return 112; + + return 160; + } + public static string GetExt(Interpolate.OutMode outMode, bool dot = true) { string ext = dot ? "." : ""; diff --git a/Code/Main/Interpolate.cs b/Code/Main/Interpolate.cs index 1b2e651..52a3535 100644 --- a/Code/Main/Interpolate.cs +++ b/Code/Main/Interpolate.cs @@ -87,7 +87,7 @@ namespace Flowframes if (extractAudio) { - string audioFile = Path.Combine(current.tempFolder, "audio.m4a"); + string audioFile = Path.Combine(current.tempFolder, "audio"); if (audioFile != null && !File.Exists(audioFile)) await FFmpegCommands.ExtractAudio(inPath, audioFile); }