Skip to content
This repository has been archived by the owner on Feb 2, 2019. It is now read-only.

Commit

Permalink
OutputProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
CherryPerry committed Jun 27, 2015
1 parent 14a3333 commit 7199b91
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 12 deletions.
25 changes: 25 additions & 0 deletions VpxEncode/Output/OutputProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace VpxEncode.Output
{
public abstract class OutputProcessor
{
protected List<ProcessingUnit> units = new List<ProcessingUnit>();

public ProcessingUnit CreateOne()
{
ProcessingUnit pu = new ProcessingUnit();
pu.OnDataReceived += pu_OnDataReceived;
units.Add(pu);
return pu;
}

protected abstract void pu_OnDataReceived(object sender, ProcessingUnitDataReceivedEventArgs e);

public void Destroy(ProcessingUnit pu)
{
pu.OnDataReceived -= pu_OnDataReceived;
units.Remove(pu);
}
}
}
37 changes: 37 additions & 0 deletions VpxEncode/Output/PercentedProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace VpxEncode.Output
{
public sealed class PercentedProcessor : OutputProcessor
{
private Regex TimeFromLine = new Regex(@".*time=(\d{2,}:\d{2}:\d{2}.\d{2}).*");

private double TotalSeconds;

public PercentedProcessor(double totalSeconds)
{
TotalSeconds = totalSeconds;
}

protected override void pu_OnDataReceived(object sender, ProcessingUnitDataReceivedEventArgs e)
{
if (e.Data == null)
return;

Match match = TimeFromLine.Match(e.Data);
if (match.Success)
{
string value = match.Groups[1].Value;
double d = TimeSpan.ParseExact(value, "hh\\:mm\\:ss\\.ff", CultureInfo.InvariantCulture).TotalSeconds;
double perc = 100 * d / TotalSeconds;

ProcessingUnit pu = sender as ProcessingUnit;
Console.SetCursorPosition(0, units.IndexOf(pu));
string str = String.Format("{0:0.00}%", perc);
Console.WriteLine(str);
}
}
}
}
34 changes: 34 additions & 0 deletions VpxEncode/Output/ProcessingUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;

namespace VpxEncode.Output
{
public sealed class ProcessingUnit
{
public int Token { get; private set; }

public event EventHandler<ProcessingUnitDataReceivedEventArgs> OnDataReceived;

public ProcessingUnit()
{
Token = new Random().Next();
}

public void Write(string line)
{
RaiseEvent(line);
}

public void DataReceived(object sender, DataReceivedEventArgs data)
{
RaiseEvent(data.Data);
}

private void RaiseEvent(string text)
{
var handler = OnDataReceived;
if (handler != null)
handler(this, new ProcessingUnitDataReceivedEventArgs(Token, text));
}
}
}
17 changes: 17 additions & 0 deletions VpxEncode/Output/ProcessingUnitDataReceivedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace VpxEncode.Output
{
public sealed class ProcessingUnitDataReceivedEventArgs : EventArgs
{
public string Data { get; private set; }
public int Token { get; private set; }

public ProcessingUnitDataReceivedEventArgs(int token, string data)
: base()
{
Data = data;
Token = token;
}
}
}
16 changes: 16 additions & 0 deletions VpxEncode/Output/SimpleProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace VpxEncode.Output
{
public sealed class SimpleProcessor : OutputProcessor
{
protected override void pu_OnDataReceived(object sender, ProcessingUnitDataReceivedEventArgs e)
{
string data = e.Data;
if (data != null && data.Length == Console.WindowWidth)
Console.Write(data);
else
Console.WriteLine(data);
}
}
}
35 changes: 23 additions & 12 deletions VpxEncode/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using VpxEncode.Math;
using VpxEncode.Output;
using YoutubeExtractor;

namespace VpxEncode
Expand Down Expand Up @@ -305,6 +306,9 @@ static string Encode(long i, string file, string subs, TimeSpan start, TimeSpan
string startString = start.ToString("hh\\:mm\\:ss\\.fff"),
timeLengthString = timeLength.ToString("hh\\:mm\\:ss\\.fff");

OutputProcessor sp = new SimpleProcessor();
ProcessingUnit pu = sp.CreateOne();

// Audio settings
string mapAudio = String.Empty;
if (ArgList.Get(Arg.MAP_AUDIO))
Expand All @@ -314,7 +318,7 @@ static string Encode(long i, string file, string subs, TimeSpan start, TimeSpan

// Encode audio
ExecuteFFMPEG(String.Format("-y -ss {1} -i \"{0}\" {5} -ac 2 -c:a opus -b:a {6}K -vbr on -vn -sn -t {2} {4} \"{3}\"",
audioFile, startString, timeLengthString, oggPath, ArgList.Get(Arg.OTHER_AUDIO).AsString(), mapAudio, opusRate));
audioFile, startString, timeLengthString, oggPath, ArgList.Get(Arg.OTHER_AUDIO).AsString(), mapAudio, opusRate), pu);

// VideoFilter
const string vfDefault = "-vf ";
Expand All @@ -323,7 +327,7 @@ static string Encode(long i, string file, string subs, TimeSpan start, TimeSpan
if (subs != null)
{
string format = subs.EndsWith("ass") || subs.EndsWith("ssa") ? "ass=\"{0}\"{1}" : "subtitles=\"{0}\"{1}";
format = String.Format(format, subs.Replace("[", "\\[").Replace("]", "\\]"), ArgList.Get(Arg.SUBS_INDEX).Command);
format = String.Format(format, subs.Replace("[", "\\[").Replace("]", "\\]").Replace(";", "\\;"), ArgList.Get(Arg.SUBS_INDEX).Command);
format = String.Format(new CultureInfo("en"), "setpts=PTS+{0:0.######}/TB,{1},setpts=PTS-STARTPTS", start.TotalSeconds, format);
vf.AppendForPrev(format);
}
Expand All @@ -344,14 +348,14 @@ static string Encode(long i, string file, string subs, TimeSpan start, TimeSpan

ExecuteFFMPEG(
String.Format("-y -ss {3} -i \"{0}\" -c:v vp9 {1} -tile-columns 6 -frame-parallel 1 -speed 4 -threads 8 -an {2} -t {4} -sn {7} -lag-in-frames 25 -pass 1 -auto-alt-ref 1 -passlogfile temp_{5} \"{6}\"",
file, bitrateString, vf, startString, timeLengthString, code, webmPath, otherVideo));
file, bitrateString, vf, startString, timeLengthString, code, webmPath, otherVideo), pu);

ExecuteFFMPEG(
String.Format("-y -ss {3} -i \"{0}\" -c:v vp9 {1} -tile-columns 6 -frame-parallel 1 -speed 1 -threads 8 -an {2} -t {4} -sn {7} -lag-in-frames 25 -pass 2 -auto-alt-ref 1 -quality {8} -passlogfile temp_{5} \"{6}\"",
file, bitrateString, vf, startString, timeLengthString, code, webmPath, otherVideo, quality));
file, bitrateString, vf, startString, timeLengthString, code, webmPath, otherVideo, quality), pu);

// Concat
ExecuteFFMPEG(String.Format("-y -i \"{0}\" -i \"{1}\" -c copy \"{2}\"", webmPath, oggPath, finalPath));
ExecuteFFMPEG(String.Format("-y -i \"{0}\" -i \"{1}\" -c copy \"{2}\"", webmPath, oggPath, finalPath), pu);

// Delete
if (subsWereCopied)
Expand All @@ -360,6 +364,8 @@ static string Encode(long i, string file, string subs, TimeSpan start, TimeSpan
File.Delete(oggPath);
File.Delete(Path.Combine(filePath, String.Format("temp_{0}-0.log", code)));

sp.Destroy(pu);

return finalPath;
}

Expand Down Expand Up @@ -401,6 +407,9 @@ static void LookupEndTime(string filePath)

static void GeneratePreview(string filePath)
{
OutputProcessor sp = new SimpleProcessor();
ProcessingUnit pu = sp.CreateOne();

string fileName = Path.GetFileName(filePath),
output = filePath.Substring(0, filePath.LastIndexOf('.') + 1) + "preview.webm",
previewSource;
Expand Down Expand Up @@ -428,7 +437,7 @@ static void GeneratePreview(string filePath)
previewSource,
previewWebm,
scale);
ExecuteFFMPEG(args);
ExecuteFFMPEG(args, pu);

// concat
string concatedWebm = String.Format("concat_{0}.webm", time);
Expand All @@ -437,29 +446,31 @@ static void GeneratePreview(string filePath)
String.Format("file '{0}'\r\nfile '{1}'", previewWebm, filePath),
Encoding.ASCII);
args = String.Format("-f concat -i \"{0}\" -c copy \"{1}\"", concatFile, concatedWebm);
ExecuteFFMPEG(args);
ExecuteFFMPEG(args, pu);

// Audio
args = String.Format("-y -i \"{0}\" -itsoffset 00:00:00.05 -i \"{1}\" -map 0:0 -map 1:1 -c copy \"{2}\"", concatedWebm, filePath, output);
ExecuteFFMPEG(args);
ExecuteFFMPEG(args, pu);

// Delete
File.Delete(concatFile);
File.Delete(previewWebm);
File.Delete(concatedWebm);

sp.Destroy(pu);
}

static void ExecuteFFMPEG(string args)
static void ExecuteFFMPEG(string args, ProcessingUnit pu)
{
Process proc = new Process();
proc.StartInfo.FileName = "ffmpeg.exe";
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.ErrorDataReceived += DataReceived;
proc.OutputDataReceived += DataReceived;
Console.WriteLine("\n\n" + args + "\n\n");
proc.ErrorDataReceived += pu.DataReceived;
proc.OutputDataReceived += pu.DataReceived;
pu.Write("\n\n" + args + "\n\n");
proc.Start();
proc.PriorityClass = ProcessPriorityClass.Idle;
proc.BeginOutputReadLine();
Expand Down
5 changes: 5 additions & 0 deletions VpxEncode/VpxEncode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<Compile Include="Math\LagrangeBitrateLookup.cs" />
<Compile Include="Math\LinearBitrateLookup.cs" />
<Compile Include="Math\Point.cs" />
<Compile Include="Output\OutputProcessor.cs" />
<Compile Include="Output\PercentedProcessor.cs" />
<Compile Include="Output\ProcessingUnit.cs" />
<Compile Include="Output\ProcessingUnitDataReceivedEventArgs.cs" />
<Compile Include="Output\SimpleProcessor.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StringBuilderExtension.cs" />
Expand Down

0 comments on commit 7199b91

Please sign in to comment.