-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
149 lines (123 loc) · 4.36 KB
/
Program.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
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
/*
* Copyright (c) 2016 Primo Software. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
using System;
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 demuxResult = DemuxMP4(opt);
Library.Shutdown();
return demuxResult ? (int)ExitCodes.Success : (int)ExitCodes.DecodingError;
}
static bool DemuxMP4(Options opt)
{
using (var transcoder = GenerateOutputFileName(opt))
{
if (transcoder == null)
return false;
if (!transcoder.Open())
{
PrintError("Transcoder open", transcoder.Error);
return false;
}
if (!transcoder.Run())
{
PrintError("Transcoder run", transcoder.Error);
return false;
}
transcoder.Close();
return true;
}
}
static Transcoder GenerateOutputFileName(Options opt)
{
using (var info = new MediaInfo())
{
info.Inputs[0].File = opt.InputFile;
if (!info.Open())
{
PrintError("MediaInfo open", info.Error);
return null;
}
MediaSocket inSocket = MediaSocket.FromMediaInfo(info);
info.Close();
Transcoder transcoder = new Transcoder();
transcoder.AllowDemoMode = true;
transcoder.Inputs.Add(inSocket);
bool audio = false;
bool video = false;
for (int i = 0; i < inSocket.Pins.Count; ++i)
{
string fileName;
if (inSocket.Pins[i].StreamInfo.MediaType == MediaType.Audio && !audio)
{
audio = true;
fileName = opt.OutputFile + ".aud.mp4";
}
else if (inSocket.Pins[i].StreamInfo.MediaType == MediaType.Video && !video)
{
video = true;
fileName = opt.OutputFile + ".vid.mp4";
}
else
{
inSocket.Pins[i].Connection = PinConnection.Disabled;
continue;
}
MediaSocket outSocket = new MediaSocket();
outSocket.Pins.Add(inSocket.Pins[i]);
DeleteFile(fileName);
outSocket.File = fileName;
transcoder.Outputs.Add(outSocket);
Console.WriteLine("Output file: {0}", fileName);
}
return transcoder;
}
}
static void DeleteFile(string filename)
{
try
{
if (System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
}
catch { }
}
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 ?? "");
}
}
enum ExitCodes : int
{
Success = 0,
OptionsError = 1,
DecodingError = 2,
}
}
}