VP9/ProRes/AVI export, support for custom ffmpeg enc args

This commit is contained in:
N00MKRAD
2020-12-23 16:13:04 +01:00
parent a1b08b71ba
commit 006706ee73
13 changed files with 219 additions and 98 deletions

View File

@@ -8,6 +8,7 @@ using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Utils = Flowframes.AudioVideo.FFmpegUtils;
namespace Flowframes
{
@@ -87,15 +88,30 @@ namespace Flowframes
DeleteSource(inputFile);
}
public static async Task FramesToMp4Vfr(string framesFile, string outPath, bool useH265, int crf, float fps, AvProcess.LogMode logMode = AvProcess.LogMode.OnlyLastLine)
public static async Task FramesToMp4Vfr(string framesFile, string outPath, Utils.Codec codec, int crf, float fps, AvProcess.LogMode logMode = AvProcess.LogMode.OnlyLastLine)
{
if(logMode != AvProcess.LogMode.Hidden)
Logger.Log($"Encoding MP4 video with CRF {crf}...");
string enc = useH265 ? "libx265" : "libx264";
string presetStr = $"-preset {Config.Get("ffEncPreset")}";
string enc = Utils.GetEnc(codec);
string preset = $"-preset {Config.Get("ffEncPreset")}";
string vfrFilename = Path.GetFileName(framesFile);
string vsync = (Interpolate.current.interpFactor == 2) ? "-vsync 1" : "-vsync 2";
string args = $"{vsync} -f concat -i {vfrFilename} -r {fps.ToString().Replace(",", ".")} -c:v {enc} -crf {crf} {presetStr} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
string rate = fps.ToString().Replace(",", ".");
string extraArgs = Config.Get("ffEncArgs");
string args = $"{vsync} -f concat -i {vfrFilename} -r {rate} -c:v {enc} -crf {crf} {preset} {extraArgs} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
await AvProcess.RunFfmpeg(args, framesFile.GetParentDir(), logMode);
}
public static async Task FramesToVideoVfr(string framesFile, string outPath, Interpolate.OutMode outMode, float fps, AvProcess.LogMode logMode = AvProcess.LogMode.OnlyLastLine)
{
if (logMode != AvProcess.LogMode.Hidden)
Logger.Log($"Encoding video...");
string encArgs = Utils.GetEncArgs(Utils.GetCodec(outMode));
string vfrFilename = Path.GetFileName(framesFile);
string vsync = (Interpolate.current.interpFactor == 2) ? "-vsync 1" : "-vsync 2";
string rate = fps.ToString().Replace(",", ".");
string extraArgs = Config.Get("ffEncArgs");
string args = $"{vsync} -f concat -i {vfrFilename} -r {rate} {encArgs} {extraArgs} {videoEncArgs} -threads {Config.GetInt("ffEncThreads")} -c:a copy {outPath.Wrap()}";
await AvProcess.RunFfmpeg(args, framesFile.GetParentDir(), logMode);
}

View File

@@ -0,0 +1,79 @@
using Flowframes.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Flowframes.AudioVideo
{
class FFmpegUtils
{
public enum Codec { H264, H265, VP9, ProRes, AviRaw }
public static Codec GetCodec(Interpolate.OutMode mode)
{
if (mode == Interpolate.OutMode.VidMp4)
{
bool h265 = Config.GetInt("mp4Enc") == 1;
return h265 ? Codec.H265 : Codec.H264;
}
if (mode == Interpolate.OutMode.VidWebm)
return Codec.VP9;
if (mode == Interpolate.OutMode.VidProRes)
return Codec.ProRes;
if (mode == Interpolate.OutMode.VidAviRaw)
return Codec.AviRaw;
return Codec.H264;
}
public static string GetEnc(Codec codec)
{
switch (codec)
{
case Codec.H264: return "libx264";
case Codec.H265: return "libx265";
case Codec.VP9: return "libvpx-vp9";
case Codec.ProRes: return "prores_ks";
case Codec.AviRaw: return "rawvideo";
}
return "libx264";
}
public static string GetEncArgs (Codec codec)
{
string args = $"-c:v { GetEnc(codec)} ";
switch (codec)
{
case Codec.H264: args += $"-crf {Config.GetInt("h264Crf")} -preset {Config.Get("ffEncPreset")}"; break;
case Codec.H265: args += $"-crf {Config.GetInt("h265Crf")} -preset {Config.Get("ffEncPreset")}"; break;
case Codec.VP9: args += $"-crf {Config.GetInt("vp9Crf")} -preset {Config.Get("ffEncPreset")}"; break;
case Codec.ProRes: args += $"-profile:v {Config.GetInt("proResProfile")}"; break;
}
return args;
}
public static string GetExt(Interpolate.OutMode outMode, bool dot = true)
{
string ext = dot ? "." : "";
switch (outMode)
{
case Interpolate.OutMode.VidMp4: ext += "mp4"; break;
case Interpolate.OutMode.VidWebm: ext += "webm"; break;
case Interpolate.OutMode.VidProRes: ext += "mov"; break;
case Interpolate.OutMode.VidAviRaw: ext += "avi"; break;
case Interpolate.OutMode.VidGif: ext += "gif"; break;
}
return ext;
}
}
}