Created
May 8, 2014 21:25
-
-
Save davideicardi/131b93a8c5631a334ef7 to your computer and use it in GitHub Desktop.
C# console application to convert video files inside a directory using HandBrakeCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("\\", "/"); | |
} | |
} | |
} |
What do lines 43 and 44 do ?
Are they putting a timestamp into the encoded file itself ?
Yes, the idea is just to reset creation and last update time to the one of the original file. This is useful for some tool that organize video by these fields...
@davideicardi
I understand.
Thank you for clearing that up.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
What do lines 43 and 44 do ?
Are they putting a timestamp into the encoded file itself ?
Thank You