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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
/* PlistPresetHandler.cs $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
namespace Handbrake.Presets
{
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Functions;
using Model;
/// <summary>
/// Plist Preset Converter
/// </summary>
public class PlistPresetHandler
{
#region Import
public static QueryParser Import(string filename)
{
XmlNode root = loadFile(filename);
if (root == null) return null;
// We'll query a query parser object and use it's public var structures to store all the data.
// This will allow the preset loader logic to be used instead of writing custom logic just for this file.
QueryParser queryParsed = new QueryParser();
string qualityMode = string.Empty;
#region Get a List of Audio Track Objects
XmlNode audioListDict = root.ChildNodes[2].ChildNodes[0].FirstChild.ChildNodes[1];
ArrayList audioTracks = new ArrayList();
for (int i = 0; i < audioListDict.ChildNodes.Count; i++)
{
XmlNode audioChannel = audioListDict.ChildNodes[i];
AudioTrack track = new AudioTrack();
for (int subi = 0; subi < audioChannel.ChildNodes.Count; subi += 2)
{
// Audio Channel Information is here.
string key = audioChannel.ChildNodes[subi].InnerText;
string value = audioChannel.ChildNodes[subi + 1].InnerText;
switch (key)
{
case "AudioBitrate":
track.Bitrate = value;
break;
case "AudioEncoder":
track.Encoder = value.Replace("AAC (CoreAudio)", "AAC (faac)");
break;
case "AudioMixdown":
track.MixDown = value;
break;
case "AudioSamplerate":
track.SampleRate = value;
break;
case "AudioTrack":
track.Track = value;
break;
case "AudioTrackDRCSlider":
track.DRC = value;
break;
}
}
audioTracks.Add(track);
queryParsed.AudioInformation = audioTracks;
}
#endregion
#region Parse the reset of the plist keys into local variables
XmlNode presetSettings = root.ChildNodes[2].ChildNodes[0].FirstChild;
// Start from 2 to avoid the audio settings which we don't need.
for (int i = 2; i < presetSettings.ChildNodes.Count; i += 2)
{
string key = presetSettings.ChildNodes[i].InnerText;
string value = presetSettings.ChildNodes[i + 1].InnerText;
switch (key)
{
// Output Settings
case "FileFormat":
queryParsed.Format = value;
break;
case "Mp4HttpOptimize":
queryParsed.OptimizeMP4 = value == "1";
break;
case "Mp4LargeFile":
queryParsed.LargeMP4 = value == "1";
break;
case "Mp4iPodCompatible":
queryParsed.IpodAtom = value == "1";
break;
// Picture Settings
case "PictureAutoCrop":
// Not used
break;
case "PictureTopCrop":
queryParsed.CropTop = value;
break;
case "PictureBottomCrop":
queryParsed.CropBottom = value;
break;
case "PictureLeftCrop":
queryParsed.CropLeft = value;
break;
case "PictureRightCrop":
queryParsed.CropRight = value;
break;
case "PictureHeight":
queryParsed.Height = int.Parse(value);
break;
case "PictureWidth":
queryParsed.Width = int.Parse(value);
break;
case "PictureKeepRatio":
queryParsed.KeepDisplayAsect = value == "1";
break;
case "PicturePAR":
queryParsed.AnamorphicMode = int.Parse(value);
break;
// Filters
case "PictureDeblock":
queryParsed.DeBlock = int.Parse(value);
break;
case "PictureDecomb":
queryParsed.Decomb = "Off";
// Don't place custom here as it's handled in the filter panel
if (value == "2") queryParsed.Decomb = "Default";
break;
case "PictureDecombCustom":
if (value != string.Empty)
queryParsed.Decomb = value;
break;
case "PictureDecombDeinterlace":
// Not Used
break;
case "PictureDeinterlace":
switch (value)
{
case "0":
queryParsed.DeInterlace = "Off";
break;
// Don't place custom here as it's handled in the filter panel
case "2":
queryParsed.DeInterlace = "Fast";
break;
case "3":
queryParsed.DeInterlace = "Slow";
break;
case "4":
queryParsed.DeInterlace = "Slowest";
break;
}
break;
case "PictureDeinterlaceCustom":
if (value != string.Empty)
queryParsed.DeInterlace = value;
break;
case "PictureDenoise":
switch (value)
{
case "0":
queryParsed.DeNoise = "Off";
break;
// Don't place custom here as it's handled in the filter panel
case "2":
queryParsed.DeNoise = "Weak";
break;
case "3":
queryParsed.DeNoise = "Medium";
break;
case "4":
queryParsed.DeNoise = "Strong";
break;
}
break;
case "PictureDenoiseCustom":
if (value != string.Empty)
queryParsed.DeNoise = value;
break;
case "PictureDetelecine":
queryParsed.DeTelecine = "Off";
if (value == "1") queryParsed.DeTelecine = "Default";
break;
case "PictureDetelecineCustom":
if (value != string.Empty)
queryParsed.DeTelecine = value;
break;
// Video Tab
case "VideoAvgBitrate":
queryParsed.AverageVideoBitrate = value;
break;
case "VideoEncoder":
queryParsed.VideoEncoder = value;
break;
case "VideoFramerate":
queryParsed.VideoFramerate = value;
break;
case "VideoGrayScale":
queryParsed.Grayscale = value == "1";
break;
case "VideoQualitySlider":
queryParsed.VideoQuality = float.Parse(value);
break;
case "VideoQualityType": // The Type of Quality Mode used
qualityMode = value;
break;
case "VideoTargetSize":
queryParsed.VideoTargetSize = value;
break;
case "VideoTurboTwoPass":
queryParsed.TurboFirstPass = value == "1";
break;
case "VideoTwoPass":
queryParsed.TwoPass = value == "1";
break;
// Chapter Markers Tab
case "ChapterMarkers":
queryParsed.ChapterMarkers = value == "1";
break;
// Advanced x264 tab
case "x264Option":
queryParsed.H264Query = value;
break;
// Preset Information
case "PresetBuildNumber":
queryParsed.PresetBuildNumber = int.Parse(value);
break;
case "PresetDescription":
queryParsed.PresetDescription = value;
break;
case "PresetName":
queryParsed.PresetName = value;
break;
case "Type":
queryParsed.Type = value;
break;
case "UsesMaxPictureSettings":
queryParsed.UsesMaxPictureSettings = value == "1";
break;
case "UsesPictureFilters":
queryParsed.UsesPictureFilters = value == "1";
break;
case "UsesPictureSettings":
queryParsed.UsesPictureSettings = value == "1";
break;
}
}
// Kill any Quality values we don't need.
switch (qualityMode)
{
case "0": // FileSize
queryParsed.VideoQuality = -1;
queryParsed.AverageVideoBitrate = null;
break;
case "1": // Avg Bitrate
queryParsed.VideoQuality = -1;
queryParsed.VideoTargetSize = null;
break;
case "2": // CQ
queryParsed.AverageVideoBitrate = null;
queryParsed.VideoTargetSize = null;
break;
}
#endregion
return queryParsed;
}
private static XmlNode loadFile(string filename)
{
try
{
XmlNode root;
if (!File.Exists(filename))
return null;
StreamReader sr = File.OpenText(filename);
string fromfile = string.Empty;
int fileChar;
while ((fileChar = sr.Read()) != -1)
fromfile += Convert.ToChar(fileChar);
XmlDocument doc = new XmlDocument();
doc.LoadXml(fromfile);
root = doc;
if (!root.HasChildNodes)
{
MessageBox.Show(
"The Preset file you selected appears to be invlaid or from an older version of HandBrake",
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
return root;
}
catch (Exception)
{
MessageBox.Show(
"The Preset file you selected appears to be invlaid or from an older version of HandBrake.\n\n Please note, if you are exporting from the MacGui you may need to rebuild your preset so that it uses the current preset plist format.\n The MacGui does not currently update user presets automatically.",
"Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
#endregion
#region Export
public static void Export(string path, QueryParser parsed)
{
XmlTextWriter xmlWriter;
xmlWriter = new XmlTextWriter(path, Encoding.UTF8);
// Header
xmlWriter.WriteStartDocument();
xmlWriter.WriteDocType("plist", "-//Apple//DTD PLIST 1.0//EN",
@"http://www.apple.com/DTDs/PropertyList-1.0.dtd", null);
xmlWriter.WriteStartElement("plist");
xmlWriter.WriteStartElement("array");
// Add New Preset Here. Can write multiple presets here if required in future.
WritePreset(xmlWriter, parsed);
// Footer
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
// Closeout
xmlWriter.Close();
}
private static void WritePreset(XmlTextWriter xmlWriter, QueryParser parsed)
{
xmlWriter.WriteStartElement("dict");
AudioListArrayDict(xmlWriter, parsed);
AddEncodeSettings(xmlWriter, parsed);
xmlWriter.WriteEndElement();
}
private static void AddEncodeSettings(XmlTextWriter xmlWriter, QueryParser parsed)
{
AddEncodeElement(xmlWriter, "ChapterMarkers", "integer", parsed.ChapterMarkers ? "1" : "0");
AddEncodeElement(xmlWriter, "Default", "integer", string.Empty); // TODO
AddEncodeElement(xmlWriter, "FileFormat", "string", parsed.Format);
AddBooleanElement(xmlWriter, "Folder", true); // TODO
AddEncodeElement(xmlWriter, "Mp4HttpOptimize", "integer", parsed.OptimizeMP4 ? "1" : "0");
AddEncodeElement(xmlWriter, "Mp4LargeFile", "integer", parsed.LargeMP4 ? "1" : "0");
AddEncodeElement(xmlWriter, "Mp4iPodCompatible", "integer", parsed.IpodAtom ? "1" : "0");
AddEncodeElement(xmlWriter, "PictureAutoCrop", "integer", string.Empty); // TODO
AddEncodeElement(xmlWriter, "PictureBottomCrop", "integer", parsed.CropBottom);
// Filters - TODO Requires Some Logic to convert modes
AddEncodeElement(xmlWriter, "PictureDeblock", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureDecomb", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureDecombCustom", "string", string.Empty);
AddEncodeElement(xmlWriter, "PictureDecombDeinterlace", "integer", string.Empty); // TODO Requires Logic
AddEncodeElement(xmlWriter, "PictureDeinterlace", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureDeinterlaceCustom", "string", string.Empty);
AddEncodeElement(xmlWriter, "PictureDenoise", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureDenoiseCustom", "string", string.Empty);
AddEncodeElement(xmlWriter, "PictureDetelecine", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureDetelecineCustom", "string", string.Empty);
AddEncodeElement(xmlWriter, "PictureHeight", "integer", parsed.Height.ToString());
AddEncodeElement(xmlWriter, "PictureKeepRatio", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureLeftCrop", "integer", parsed.CropLeft);
AddEncodeElement(xmlWriter, "PicturePAR", "integer", string.Empty);
AddEncodeElement(xmlWriter, "PictureRightCrop", "integer", parsed.CropRight);
AddEncodeElement(xmlWriter, "PictureTopCrop", "integer", parsed.CropTop);
AddEncodeElement(xmlWriter, "PictureWidth", "integer", parsed.Width.ToString());
AddEncodeElement(xmlWriter, "PresetBuildNumber", "string", string.Empty);
AddEncodeElement(xmlWriter, "PresetDescription", "string", string.Empty);
AddEncodeElement(xmlWriter, "PresetName", "string", string.Empty);
AddEncodeElement(xmlWriter, "Type", "integer", string.Empty);
AddEncodeElement(xmlWriter, "UsesMaxPictureSettings", "integer", string.Empty);
AddEncodeElement(xmlWriter, "UsesPictureFilters", "integer", string.Empty);
AddEncodeElement(xmlWriter, "UsesPictureSettings", "integer", string.Empty);
AddEncodeElement(xmlWriter, "VideoAvgBitrate", "string", string.Empty);
AddEncodeElement(xmlWriter, "VideoEncoder", "string", string.Empty);
AddEncodeElement(xmlWriter, "VideoFramerate", "string", string.Empty);
AddEncodeElement(xmlWriter, "VideoGrayScale", "integer", parsed.Grayscale ? "1" : "0");
AddEncodeElement(xmlWriter, "VideoQualitySlider", "real", parsed.VideoQuality.ToString());
AddEncodeElement(xmlWriter, "VideoQualityType", "integer", string.Empty); // TODO Requires Logic
AddEncodeElement(xmlWriter, "VideoTargetSize", "string", parsed.VideoTargetSize);
AddEncodeElement(xmlWriter, "VideoTurboTwoPass", "integer", parsed.TurboFirstPass ? "1" : "0");
AddEncodeElement(xmlWriter, "VideoTwoPass", "integer", parsed.TwoPass ? "1" : "0");
AddEncodeElement(xmlWriter, "x264Option", "string", parsed.H264Query);
}
private static void AddBooleanElement(XmlTextWriter xmlWriter, string keyName, bool value)
{
xmlWriter.WriteStartElement("key");
xmlWriter.WriteString(keyName);
xmlWriter.WriteEndElement();
if (value)
{
xmlWriter.WriteStartElement("true");
xmlWriter.WriteEndElement();
}
else
{
xmlWriter.WriteStartElement("false");
xmlWriter.WriteEndElement();
}
}
private static void AddEncodeElement(XmlTextWriter xmlWriter, string keyName, string type, string value)
{
xmlWriter.WriteElementString("key", keyName);
xmlWriter.WriteElementString(type, value);
}
private static void AudioListArrayDict(XmlTextWriter xmlWriter, QueryParser parsed)
{
xmlWriter.WriteStartElement("key");
xmlWriter.WriteString("AudioList");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("array");
foreach (AudioTrack track in parsed.AudioInformation)
{
AddAudioItem(xmlWriter, track);
}
xmlWriter.WriteEndElement();
}
private static void AddAudioItem(XmlTextWriter xmlWriter, AudioTrack audioTrack)
{
string bitrate = audioTrack.Bitrate;
string encoder = audioTrack.Encoder;
string mixdown = audioTrack.MixDown;
string sample = audioTrack.SampleRate;
int track;
int.TryParse(audioTrack.Track, out track);
string drc = audioTrack.DRC;
string desc = "Automatic";
xmlWriter.WriteStartElement("dict");
xmlWriter.WriteElementString("key", "AudioBitrate");
xmlWriter.WriteElementString("string", bitrate);
xmlWriter.WriteElementString("key", "AudioEncoder");
xmlWriter.WriteElementString("string", encoder);
xmlWriter.WriteElementString("key", "AudioMixdown");
xmlWriter.WriteElementString("string", mixdown);
xmlWriter.WriteElementString("key", "AudioSamplerate");
xmlWriter.WriteElementString("string", sample);
xmlWriter.WriteElementString("key", "AudioTrack");
xmlWriter.WriteElementString("integer", track.ToString());
xmlWriter.WriteElementString("key", "AudioTrackDRCSlider");
xmlWriter.WriteElementString("real", drc.ToString());
xmlWriter.WriteElementString("key", "AudioTrackDescription");
xmlWriter.WriteElementString("string", desc);
xmlWriter.WriteEndElement();
}
#endregion
}
}
|