-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (105 loc) · 4.01 KB
/
Program.cs
File metadata and controls
132 lines (105 loc) · 4.01 KB
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
using System;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
using PrimoSoftware.AVBlocks;
namespace CliSample
{
class Program
{
static int Main(string[] args)
{
var opt = new Options();
if (!opt.Prepare(args))
return opt.Error ? (int)ExitCodes.OptionsError : (int)ExitCodes.Success;
Library.Initialize();
// Set license information. To run AVBlocks in demo mode, comment the next line out
// Library.SetLicense("<license-string>");
bool encodeResult = ReEncode(opt);
Library.Shutdown();
return encodeResult ? (int)ExitCodes.Success : (int)ExitCodes.EncodeError;
}
enum ExitCodes : int
{
Success = 0,
OptionsError = 1,
EncodeError = 2,
}
static bool ReEncode(Options opt)
{
if (File.Exists(opt.OutputFile))
File.Delete(opt.OutputFile);
using (var transcoder = new Transcoder())
{
// In order to use the production release for testing (without a valid license),
// the transcoder demo mode must be enabled.
transcoder.AllowDemoMode = true;
using (var mediaInfo = new MediaInfo())
{
mediaInfo.Inputs[0].File = opt.InputFile;
if (!mediaInfo.Open())
{
PrintError("Open MediaInfo", mediaInfo.Error);
return false;
}
// Add Inputs
{
var socket = MediaSocket.FromMediaInfo(mediaInfo);
transcoder.Inputs.Add(socket);
}
}
// Add Outputs
{
// Create output socket
var socket = new MediaSocket();
var inSocket = transcoder.Inputs[0];
socket.StreamType = inSocket.StreamType;
socket.File = opt.OutputFile;
// Add pins with ReEncode parameter set to Use.On
foreach (var inPin in inSocket.Pins)
{
StreamInfo si = (StreamInfo)inPin.StreamInfo.Clone();
var pin = new MediaPin();
pin.StreamInfo = (StreamInfo)si.Clone();
if ((MediaType.Video == si.MediaType) && opt.ForceVideo)
{
pin.Params.Add(Param.ReEncode, Use.On);
}
if ((MediaType.Audio == si.MediaType) && opt.ForceAudio)
{
pin.Params.Add(Param.ReEncode, Use.On);
}
socket.Pins.Add(pin);
}
transcoder.Outputs.Add(socket);
}
bool result = transcoder.Open();
PrintError("Open Transcoder", transcoder.Error);
if (!result)
return false;
result = transcoder.Run();
PrintError("Run Transcoder", transcoder.Error);
if (!result)
return false;
transcoder.Close();
}
return true;
}
static void PrintError(string action, ErrorInfo e)
{
if (action != null)
{
Console.Write("{0}: ", action);
}
if (ErrorFacility.Success == e.Facility)
{
Console.WriteLine("Success");
return;
}
else
{
Console.WriteLine("{0}, facility:{1} code:{2} hint:{3}", e.Message ?? "", e.Facility, e.Code, e.Hint ?? "");
}
}
}
}