-
Notifications
You must be signed in to change notification settings - Fork 4
/
OGGEncoder.cs
101 lines (84 loc) · 3.16 KB
/
OGGEncoder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
namespace OGGEncoder
{
class Program
{
static void Main(string[] args)
{
// Requires ffmpeg.exe
// Download from https://www.ffmpeg.org/ and place the ffmpeg.exe file in the root directory
// Drag and drop media file to convert
string workingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string fileName = Path.GetFileNameWithoutExtension(args[0]);
string filePath = args[1];
string extension = ".ogg";
string bitrate = "48k";
if (!Directory.Exists(Path.Combine(workingDirectory, filePath)))
{
Directory.CreateDirectory(Path.Combine(workingDirectory, filePath));
}
if (File.Exists(fileName + extension))
{
fileName += "NEW";
}
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = Path.Combine(workingDirectory, "ffmpeg");
startInfo.WorkingDirectory = workingDirectory;
startInfo.Arguments = String.Format("-i \"{0}\" -vn -c:a libvorbis -b:a {1} -map_metadata -1 \"{2}\"", args[0], bitrate, Path.Combine(filePath, fileName + extension));
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
RemoveTags(args[0], Path.Combine(workingDirectory, filePath, fileName + extension));
File.Delete(args[0]);
}
private static void RemoveTags(string oldfile, string newfile)
{
string title = null;
string[] performers = null;
string[] artists = null;
// Extract title and performer from old song file
using (var file = TagLib.File.Create(oldfile))
{
title = file.Tag.Title;
if (file.Tag.Performers.Length >= 1)
{
performers = file.Tag.Performers;
}
if (file.Tag.AlbumArtists.Length >= 1)
{
artists = file.Tag.AlbumArtists;
}
}
// Insert title and performer in new song file
using (var file = TagLib.File.Create(newfile))
{
file.RemoveTags(TagLib.TagTypes.AllTags);
if (performers != null)
{
file.Tag.Performers = performers;
}
if (artists != null)
{
file.Tag.AlbumArtists = artists;
}
if (title == null)
{
file.Tag.Title = Path.GetFileNameWithoutExtension(newfile);
}
else
{
file.Tag.Title = title;
}
file.Save();
}
}
}
}