/
MfEncoder.cpp
325 lines (239 loc) · 7.1 KB
/
MfEncoder.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// MfEncoder.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <mfapi.h>
#include <Shlwapi.h>
#include "CommandLineParser.h"
#include "Parameters.h"
#include "MediaSource.h"
#include "MediaSink.h"
#include "AudioEncoder.h"
#include "AsfContentInfoBuilder.h"
static void SetMediaSinkContentInfoMetadata(AsfContentInfoBuilder* contentInfo, MediaSource* mediaSource, Parameters* commandLineParameters)
{
// please work
PCWSTR value;
value = commandLineParameters->Album;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/AlbumTitle");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/AlbumTitle", value);
}
value = commandLineParameters->Artist;
if (!value)
{
value = mediaSource->GetMetadataValue(L"Author");
}
if (value)
{
contentInfo->SetMetadataAsString(L"Author", value);
}
value = commandLineParameters->AlbumArtist;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/AlbumArtist");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/AlbumArtist", value);
}
value = commandLineParameters->Genre;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/Genre");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/Genre", value);
}
value = commandLineParameters->Title;
if (!value)
{
value = mediaSource->GetMetadataValue(L"Title");
}
if (value)
{
contentInfo->SetMetadataAsString(L"Title", value);
}
value = commandLineParameters->TrackNumber;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/TrackNumber");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/TrackNumber", value);
}
value = commandLineParameters->Year;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/Year");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/Year", value);
}
value = commandLineParameters->Comment;
if (!value)
{
value = mediaSource->GetMetadataValue(L"Description");
}
if (value)
{
contentInfo->SetMetadataAsString(L"Description", value);
}
value = commandLineParameters->Composer;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/Composer");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/Composer", value);
}
value = commandLineParameters->SubTitle;
if (!value)
{
value = mediaSource->GetMetadataValue(L"WM/SubTitle");
}
if (value)
{
contentInfo->SetMetadataAsString(L"WM/SubTitle", value);
}
// contentInfo->SetMetadataAsString(L"Author", L"_Author_");
// contentInfo->SetMetadataAsString(L"WM/AlbumArtist", L"_WM/AlbumArtist_");
}
int wmain(int argc, wchar_t *argv[])
{
Parameters commandLineParameters;
if (argc < 1)
{
printf("Command line help goes here...");
return 0;
}
CommandLineParser::Parse(argc, argv, &commandLineParameters);
AudioEncoderParameters* encoderParameters = nullptr;
if (commandLineParameters.Quality == 100)
{
encoderParameters = AudioEncoderParameters::CreateLosslessEncoderParameters(2, 44100, 16);
}
else
{
encoderParameters = AudioEncoderParameters::CreateQualityBasedVbrParameters(commandLineParameters.Quality, 2, 44100, 16);
}
// Verify that output folder exists, if specified
// (and add a '\' to it if it doesn't exist)
if (*commandLineParameters.OutputFolder)
{
WIN32_FILE_ATTRIBUTE_DATA fileData;
BOOL success = GetFileAttributesEx(commandLineParameters.OutputFolder, GET_FILEEX_INFO_LEVELS::GetFileExInfoStandard, &fileData);
// check if the file system object exists, but it's not a directory...
if (success && ((fileData.dwFileAttributes & 0x10) == 0))
{
printf("Specified output directory is not a directory");
return 0;
}
if (!success)
{
printf("Specified output directory does not exist");
return 0;
}
size_t outputFolderLength = wcslen(commandLineParameters.OutputFolder);
if (outputFolderLength < MAX_PATH - 1)
{
if (*(commandLineParameters.OutputFolder + outputFolderLength - 1) != '\\')
{
*(commandLineParameters.OutputFolder + outputFolderLength) = '\\';
*(commandLineParameters.OutputFolder + outputFolderLength + 1) = '\0';
}
}
}
// Initialize COM & Media Foundation
if (!SUCCEEDED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED)))
{
wprintf_s(L"Unable to initialize COM");
return -1;
}
if (!SUCCEEDED(MFStartup(MF_VERSION)))
{
CoUninitialize();
wprintf_s(L"Unable to initialize MediaFoundation");
return -1;
}
try
{
// Use the Windows shell API to extract the path component from the input filename
WCHAR srcFileFolder[MAX_PATH];
WCHAR srcFileName[MAX_PATH];
wcscpy_s(srcFileFolder, commandLineParameters.InputFilename);
BOOL ret = PathRemoveFileSpec(srcFileFolder);
size_t srcFolderLength = wcslen(srcFileFolder);
if (srcFolderLength < MAX_PATH - 1)
{
if (srcFolderLength > 0)
{
if (*(srcFileFolder + srcFolderLength - 1) != '\\')
{
*(srcFileFolder + srcFolderLength) = '\\';
*(srcFileFolder + srcFolderLength + 1) = '\0';
}
}
}
// do some basic parsing of input filename, as FirstFirstFile / FindNext
// does not return the full path so we'll have to prepend
// any directory info specified
WIN32_FIND_DATA findData;
HANDLE hFindFile = FindFirstFile(commandLineParameters.InputFilename, &findData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// skip directories
continue;
}
wcscpy_s(srcFileName, srcFileFolder);
wcscat_s(srcFileName, findData.cFileName);
MediaSource* mediaSource = MediaSource::Open(srcFileName);
// if an output folder is specified, use that
WCHAR outputFilename[MAX_PATH];
if (*commandLineParameters.OutputFolder)
{
wcscpy_s(outputFilename, commandLineParameters.OutputFolder);
wcscat_s(outputFilename, findData.cFileName);
PathRenameExtension(outputFilename, L".wma");
}
else
{
wcscpy_s(outputFilename, commandLineParameters.OutputFilename);
}
AsfContentInfoBuilder *contentInfo = new AsfContentInfoBuilder();
contentInfo->AddStreamSink(1, encoderParameters);
SetMediaSinkContentInfoMetadata(contentInfo, mediaSource, &commandLineParameters);
MediaSink* mediaSink = MediaSink::Create(outputFilename, contentInfo->ConstructMfAsfContentInfo());
wprintf_s(L"Encoding %s\n", findData.cFileName);
AudioEncoder::Encode(mediaSource, mediaSink, encoderParameters);
delete mediaSink;
delete mediaSource;
} while (FindNextFile(hFindFile, &findData));
FindClose(hFindFile);
}
else
{
// input file does not exit
throw std::invalid_argument("Input filename does not exist");
}
MFShutdown();
CoUninitialize();
}
catch (std::exception &ex)
{
printf("ERROR: %s\n", ex.what());
}
return 0;
}