Skip to content

Instantly share code, notes, and snippets.

@tatupesonen
Created June 20, 2018 22:28
Show Gist options
  • Save tatupesonen/4a122c2185d4dee71192a15b70715fa3 to your computer and use it in GitHub Desktop.
Save tatupesonen/4a122c2185d4dee71192a15b70715fa3 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
namespace SongEncoder
{
class Program
{
static void Main(string[] args)
{
//Encode all FLACs to Opus, copy jackets and retain folder structure.
string startPath = @"D:\Music\";
//Where to encode/copy
string destinationPath = @"D\MusicEncoded\";
//recursively encode, copy files and recreate folder structure
void Do(string sDir)
{
foreach (string d in Directory.GetDirectories(sDir))
{
Directory.CreateDirectory(d.Replace("Music", "MusicEncoded"));
foreach (string file in Directory.GetFiles(d))
{
if (Path.GetExtension(file) == ".ogg" || Path.GetExtension(file) == ".mp3")
{
string newFile = d.Replace("Music", "MusicEncoded") + "\\" + Path.GetFileNameWithoutExtension(file);
var testString = String.Format("-i \"{0}\" -vn -acodec libopus -b:a 320K -vbr on -compression_level 10 \"{1}.opus\"", file, newFile);
DoEncode(file, testString);
}
else {
File.Copy(file, file.Replace("Music", "MusicEncoded"));
}
}
Do(d);
}
}
Do(startPath);
void DoEncode(string path, string encodeArgs)
{
var encodingProcess = new Process
{
StartInfo = new ProcessStartInfo
{
//Change this to your ffmpeg install location
FileName = @"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = encodeArgs,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = false
}
};
Console.WriteLine(encodingProcess.StartInfo.Arguments);
encodingProcess.EnableRaisingEvents = true;
encodingProcess.ErrorDataReceived += (s, e) => Console.WriteLine($@"{e.Data}");
encodingProcess.Start();
encodingProcess.BeginOutputReadLine();
encodingProcess.BeginErrorReadLine();
encodingProcess.WaitForExit();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment