Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# console application to convert video files inside a directory using HandBrakeCLI
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HBConvertVideoBatch
{
class Program
{
const string InputPath = @"E:\Pictures\";
const string InputExtension = ".mov";
const string OutputExtension = ".mp4";
const string OutputPath = @"E:\temp\outvideo\";
const string HandBrakeLocation = @"E:\temp\HandBrake-0.9.9-x86_64-Win_CLI\HandBrakeCLI.exe";
const string HandBrakeCommandLine = @"-i ""{in}"" -o ""{out}"" --preset=""davide_batch""";
static void Main(string[] args)
{
var files = Directory.GetFiles(InputPath, "*" + InputExtension, SearchOption.AllDirectories);
Console.WriteLine("Founded {0} files to convert, proceed? (Y)Yes or (N)No?", files.Length);
if (Console.ReadLine() != "Y")
return;
foreach (var inputFile in files)
{
var outputFile = Path.Combine(OutputPath, Path.GetFileName(inputFile.ToLowerInvariant()).Replace(InputExtension, OutputExtension));
if (File.Exists(outputFile))
continue;
Console.WriteLine("Converting file {0}", inputFile);
var arguments = HandBrakeCommandLine.Replace("{in}", SanitazeFileForHB(inputFile)).Replace("{out}", SanitazeFileForHB(outputFile));
var handbrake = Process.Start(HandBrakeLocation, arguments);
handbrake.WaitForExit();
File.SetCreationTimeUtc(outputFile, File.GetCreationTimeUtc(inputFile));
File.SetLastWriteTimeUtc(outputFile, File.GetLastWriteTimeUtc(inputFile));
}
Console.WriteLine("Conversion completed!!");
Console.ReadLine();
}
static string SanitazeFileForHB(string file)
{
return file.Replace("\\", "/");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment