Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit c449ff4

Browse files
committed
Cleaned console output, redirecting output, parsing and stripping percentage and framerate to single console window.
1 parent f56e002 commit c449ff4

11 files changed

Lines changed: 267 additions & 80 deletions

File tree

Context Menu Setup/SetupContextMenu.reg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Windows Registry Editor Version 5.00
1010
@="TV Shows - Kids"
1111

1212
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\tv-kids\command]
13-
@="\"C:\\Utilities\\HandBrakeCLI\\BatchEncode.exe\" \"%1\" --preset-import-file \"C:\\Utilities\\HandBrakeCLI\\presets\\tv-kids-preset.json\" \"TV Shows - Kids\" 128"
13+
@="\"C:\\Utilities\\HandBrakeCLI\\BatchEncode.exe\" \"%1\" --preset-import-file \"C:\\Utilities\\HandBrakeCLI\\presets\\tv-kids-preset.json\" 128"
1414

1515
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\tv-mobile]
1616
@="TV Shows - Mobile"
1717

1818
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\tv-mobile\command]
19-
@="\"C:\\Utilities\\HandBrakeCLI\\BatchEncode.exe\" \"%1\" --preset-import-file \"C:\\Utilities\\HandBrakeCLI\\presets\\tv-mobile-preset.json\" \"TV Shows - Mobile\" 64"
19+
@="\"C:\\Utilities\\HandBrakeCLI\\BatchEncode.exe\" \"%1\" --preset-import-file \"C:\\Utilities\\HandBrakeCLI\\presets\\tv-mobile-preset.json\" 64"
1.39 MB
Binary file not shown.
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Collections.Generic;
6+
using System.Text.RegularExpressions;
7+
8+
namespace BatchEncode
9+
{
10+
public class Business
11+
{
12+
private static string PercentageRegEx = @"(\d+)(\.\d{1,2})? %";
13+
private static string FPSRegEx = @"(\d+)(\.\d{1,2})? fps";
14+
15+
public static string[] AcceptedFileTypes
16+
{
17+
get
18+
{
19+
return new string[] { ".mp4", ".avi", ".mov", ".mkv", ".wmv", ".mpv", ".mpeg", "mpg", ".m4v", ".3gp", ".3g2", "ts", "mts", "m2ts", " 4xm", "mtv", "roq", "avm2", "avm2", "flv", "flv", "mj2", "mj2" };
20+
}
21+
}
22+
23+
public static void EncodeVideos(string root, string presetPath, string audioByteRate)
24+
{
25+
string[] filesList = Directory.GetFiles(root, "*.*", SearchOption.AllDirectories);
26+
List<string> acceptedFileList = new List<string>();
27+
28+
foreach (string file in filesList)
29+
{
30+
FileInfo info = new FileInfo(file);
31+
32+
if (info.Extension.ToLower().In(AcceptedFileTypes))
33+
acceptedFileList.Add(file);
34+
}
35+
36+
Console.Out.WriteLine("\nEncoding videos: " + acceptedFileList.Count + " found...\n");
37+
38+
int i = 1;
39+
40+
string lastDir = string.Empty;
41+
42+
foreach (string file in acceptedFileList)
43+
{
44+
FileInfo info = new FileInfo(file);
45+
46+
string tempFileName = info.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(file) + "_" + info.Extension;
47+
string newFileName = info.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(file) + Global.DefaultOutputExtension;
48+
49+
#region Rename file
50+
51+
try
52+
{
53+
File.Move(file, tempFileName);
54+
}
55+
catch (Exception e)
56+
{
57+
if (e.Message.Contains("already exists"))
58+
{
59+
try
60+
{
61+
File.Delete(tempFileName);
62+
}
63+
catch
64+
{
65+
Thread.Sleep(1000);
66+
67+
try
68+
{
69+
File.Delete(tempFileName);
70+
}
71+
catch
72+
{
73+
Console.Out.Write("... FAIL");
74+
continue;
75+
}
76+
}
77+
78+
try
79+
{
80+
File.Move(file, tempFileName);
81+
}
82+
catch
83+
{
84+
Console.Out.Write("... FAIL");
85+
86+
continue;
87+
}
88+
}
89+
}
90+
91+
#endregion
92+
93+
if (info.Directory.FullName != lastDir)
94+
{
95+
Console.ForegroundColor = ConsoleColor.Cyan;
96+
Console.Out.WriteLine("\nFolder: " + info.Directory.FullName + "\n");
97+
Console.ResetColor();
98+
}
99+
100+
lastDir = info.Directory.FullName;
101+
102+
Console.ForegroundColor = ConsoleColor.Green;
103+
Console.Out.Write(string.Format("[{0}/{1}]: ", i, acceptedFileList.Count));
104+
Console.ResetColor();
105+
Console.Out.Write(info.Name);
106+
107+
EncodeVideo(tempFileName, newFileName, presetPath, audioByteRate);
108+
109+
i++;
110+
}
111+
112+
Console.Out.WriteLine("\nEncoding Complete... Exiting\n");
113+
Thread.Sleep(5000);
114+
}
115+
116+
private static bool EncodeVideo(string inputFile, string outputFile, string presetPath, string audioByteRate)
117+
{
118+
string arguments = @"-i """ + inputFile + @""" -o """ + outputFile + @""" --preset-import-file """ + presetPath + @""" -B " + audioByteRate;
119+
120+
using (Process process = new Process())
121+
{
122+
process.StartInfo.FileName = Global.HandrakeCLIPath;
123+
process.StartInfo.Arguments = arguments;
124+
125+
process.StartInfo.UseShellExecute = false;
126+
process.StartInfo.RedirectStandardOutput = true;
127+
process.StartInfo.RedirectStandardError = true;
128+
process.EnableRaisingEvents = true;
129+
process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(Process_OutputDataReceived);
130+
process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(Process_ErrorDataReceived);
131+
process.Exited += new System.EventHandler(Process_Exited);
132+
133+
process.Start();
134+
135+
process.BeginErrorReadLine();
136+
process.BeginOutputReadLine();
137+
138+
process.WaitForExit();
139+
}
140+
141+
#region Delete temp file
142+
143+
try
144+
{
145+
File.Delete(inputFile);
146+
}
147+
catch
148+
{
149+
Thread.Sleep(1000);
150+
151+
try
152+
{
153+
File.Delete(inputFile);
154+
}
155+
catch
156+
{
157+
// Forget it
158+
}
159+
}
160+
161+
#endregion
162+
163+
return true;
164+
}
165+
166+
private static void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
167+
{
168+
WriteOutput(e.Data);
169+
}
170+
171+
private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
172+
{
173+
WriteOutput(e.Data);
174+
}
175+
176+
private static string _lastOutput = string.Empty;
177+
private static void WriteOutput(string output)
178+
{
179+
try
180+
{
181+
MatchCollection pMc = Regex.Matches(output, PercentageRegEx);
182+
MatchCollection fpsMc = Regex.Matches(output, FPSRegEx);
183+
184+
if (pMc.Count > 0)
185+
{
186+
string textP = " " + pMc[pMc.Count - 1].Value.ToString().Replace(" ", "").Replace("%", "") + "%";
187+
string textFPS = (fpsMc.Count > 0 && fpsMc[fpsMc.Count - 1].Value.ToString().Length > 3 ? " (" + fpsMc[fpsMc.Count - 1].Value.ToString() + ")" : "") + " ";
188+
189+
if (!string.IsNullOrEmpty(_lastOutput))
190+
Console.SetCursorPosition(Console.CursorLeft - _lastOutput.Length, Console.CursorTop);
191+
192+
Console.ForegroundColor = ConsoleColor.Yellow;
193+
Console.Write(textP);
194+
Console.ForegroundColor = ConsoleColor.Red;
195+
Console.Write(textFPS);
196+
197+
Console.ResetColor();
198+
199+
_lastOutput = textP + textFPS;
200+
}
201+
}
202+
catch
203+
{
204+
// nevermind
205+
}
206+
}
207+
208+
private static void Process_Exited(object sender, EventArgs e)
209+
{
210+
try
211+
{
212+
if (!string.IsNullOrEmpty(_lastOutput))
213+
{
214+
Console.SetCursorPosition(Console.CursorLeft - _lastOutput.Length, Console.CursorTop);
215+
216+
Console.ForegroundColor = ConsoleColor.Yellow;
217+
Console.Write(" 100% \n");
218+
}
219+
}
220+
catch { }
221+
222+
_lastOutput = string.Empty;
223+
224+
}
225+
}
226+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Linq;
2+
3+
namespace System
4+
{
5+
public static class SystemExtensions
6+
{
7+
public static bool In<T>(this T needle, params T[] haystack)
8+
{
9+
return haystack.Contains(needle);
10+
}
11+
}
12+
}

HandBrakeCLIBatchEncode/Global.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BatchEncode
2+
{
3+
public static class Global
4+
{
5+
public static string DefaultOutputExtension { get; set; } = ".mp4";
6+
public static string HandrakeCLIPath { get; set; } = @"C:\Utilities\HandBrakeCLI\HandBrakeCLI.exe";
7+
}
8+
}

HandBrakeCLIBatchEncode/HandBrakeCLIBatchEncode.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2526
</PropertyGroup>
2627
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2728
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -31,6 +32,7 @@
3132
<DefineConstants>TRACE</DefineConstants>
3233
<ErrorReport>prompt</ErrorReport>
3334
<WarningLevel>4</WarningLevel>
35+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3436
</PropertyGroup>
3537
<ItemGroup>
3638
<Reference Include="System" />
@@ -43,6 +45,9 @@
4345
<Reference Include="System.Xml" />
4446
</ItemGroup>
4547
<ItemGroup>
48+
<Compile Include="Business.cs" />
49+
<Compile Include="Extensions.cs" />
50+
<Compile Include="Global.cs" />
4651
<Compile Include="Program.cs" />
4752
<Compile Include="Properties\AssemblyInfo.cs" />
4853
</ItemGroup>

HandBrakeCLIBatchEncode/Program.cs

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
2-
using System.Diagnostics;
3-
using System.IO;
4-
using System.Linq;
5-
using System.Threading;
62

73
namespace BatchEncode
84
{
95
class Program
106
{
117
static void Main(string[] args)
128
{
9+
Console.CursorVisible = false;
10+
Console.Title = "HandBrakeCLI Batch Encoder";
11+
1312
Console.Out.WriteLine("");
1413
Console.Out.WriteLine(" ____.__________ ");
1514
Console.Out.WriteLine(" | |\\______ \\");
@@ -19,75 +18,12 @@ static void Main(string[] args)
1918
Console.Out.WriteLine(" \\/");
2019
Console.Out.WriteLine("");
2120

22-
EncodeVideos(args[0], args[1], args[2]);
23-
}
24-
25-
26-
public static void EncodeVideos(string root, string presetPath, string audioByteRate)
27-
{
28-
string[] filesList = Directory.GetFiles(root);
29-
30-
foreach (string file in filesList)
31-
{
32-
FileInfo info = new FileInfo(file);
21+
#if DEBUG
22+
Business.EncodeVideos(@"E:\Downloads\Test", @"C:\Utilities\HandBrakeCLI\presets\tv-kids-preset.json", "128");
23+
#else
24+
Business.EncodeVideos(args[0], args[1], args[2]);
25+
#endif
3326

34-
if (info.Extension.ToLower().In(".mp4", ".avi", ".mov", ".mkv", ".flv", ".wmv", ".mpv", ".mpeg", ".m4v", ".3gp", ".3g2", ".f4v", ".f4a", ".f4b"))
35-
{
36-
string tempFileName = info.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(file) + "_" + info.Extension;
37-
string newFileName = info.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(file) + ".mp4";
38-
39-
File.Move(file, tempFileName);
40-
41-
Console.Out.Write("Encoding video: " + file);
42-
43-
EncodeVideo(tempFileName, newFileName, presetPath, audioByteRate);
44-
}
45-
}
46-
}
47-
48-
private static bool EncodeVideo(string inputFile, string outputFile, string presetPath, string audioByteRate)
49-
{
50-
string arguments = @"-i """ + inputFile + @""" -o """ + outputFile + @""" --preset-import-file """ + presetPath + @""" ""TV Shows - Kids"" -B " + audioByteRate;
51-
52-
using (Process proc = new Process())
53-
{
54-
proc.StartInfo.FileName = @"C:\Utilities\HandBrakeCLI\HandBrakeCLI.exe";
55-
proc.StartInfo.Arguments = arguments;
56-
proc.StartInfo.UseShellExecute = true;
57-
proc.StartInfo.RedirectStandardOutput = false;
58-
proc.Start();
59-
proc.WaitForExit();
60-
61-
Console.Out.Write(".. Done\n");
62-
}
63-
64-
try
65-
{
66-
File.Delete(inputFile);
67-
}
68-
catch
69-
{
70-
Thread.Sleep(1000);
71-
72-
try
73-
{
74-
File.Delete(inputFile);
75-
}
76-
catch
77-
{
78-
79-
}
80-
}
81-
82-
return true;
83-
}
84-
}
85-
86-
public static class SystemExtensions
87-
{
88-
public static bool In<T>(this T needle, params T[] haystack)
89-
{
90-
return haystack.Contains(needle);
9127
}
9228
}
9329
}

0 commit comments

Comments
 (0)