@@ -15,10 +15,8 @@ namespace FFMpeg.Xamarin
15
15
{
16
16
public class FFMpegLibrary
17
17
{
18
-
19
18
public string CDNHost { get ; set ; } = "raw.githubusercontent.com" ;
20
-
21
-
19
+
22
20
public static FFMpegLibrary Instance = new FFMpegLibrary ( ) ;
23
21
24
22
private bool _initialized = false ;
@@ -30,16 +28,13 @@ public class FFMpegLibrary
30
28
/// </summary>
31
29
/// <param name="context"></param>
32
30
/// <returns></returns>
33
- public async Task Init (
34
- Context context ,
35
- string cdn = null ,
36
- string downloadTitle = null ,
37
- string downloadMessage = null )
31
+ public async Task Init ( Context context , string cdn = null , string downloadTitle = null , string downloadMessage = null )
38
32
{
39
33
if ( _initialized )
40
34
return ;
41
35
42
- if ( cdn != null ) {
36
+ if ( cdn != null )
37
+ {
43
38
CDNHost = cdn ;
44
39
}
45
40
@@ -48,7 +43,7 @@ public async Task Init(
48
43
49
44
ffmpegFile = new Java . IO . File ( filesDir + "/ffmpeg" ) ;
50
45
51
- FFMPEGSource source = FFMPEGSource . Get ( ) ;
46
+ FFMpegSource source = FFMpegSource . Get ( ) ;
52
47
53
48
await Task . Run ( ( ) =>
54
49
{
@@ -65,7 +60,8 @@ await Task.Run(() =>
65
60
return ;
66
61
}
67
62
}
68
- catch ( Exception ex ) {
63
+ catch ( Exception ex )
64
+ {
69
65
System . Diagnostics . Debug . WriteLine ( $ " Error validating file { ex } ") ;
70
66
}
71
67
@@ -86,7 +82,8 @@ await Task.Run(() =>
86
82
return ;
87
83
}
88
84
89
- if ( ffmpegFile . Exists ( ) ) {
85
+ if ( ffmpegFile . Exists ( ) )
86
+ {
90
87
ffmpegFile . Delete ( ) ;
91
88
}
92
89
@@ -97,53 +94,44 @@ await Task.Run(() =>
97
94
dlg . Indeterminate = false ;
98
95
dlg . SetProgressStyle ( ProgressDialogStyle . Horizontal ) ;
99
96
dlg . SetCancelable ( false ) ;
100
- dlg . CancelEvent += ( s , e ) => {
101
-
97
+ dlg . CancelEvent += ( s , e ) =>
98
+ {
99
+
102
100
} ;
103
101
104
102
dlg . SetCanceledOnTouchOutside ( false ) ;
105
103
dlg . Show ( ) ;
106
104
107
-
108
105
using ( var c = new System . Net . Http . HttpClient ( ) )
109
106
{
110
107
using ( var fout = System . IO . File . OpenWrite ( ffmpegFile . AbsolutePath ) )
111
108
{
112
-
113
109
string url = source . Url ;
114
110
var g = new System . Net . Http . HttpRequestMessage ( System . Net . Http . HttpMethod . Get , url ) ;
115
-
116
- var h = await c . SendAsync ( g , System . Net . Http . HttpCompletionOption . ResponseHeadersRead ) ;
117
-
118
111
119
-
120
-
121
-
122
-
112
+ var h = await c . SendAsync ( g , System . Net . Http . HttpCompletionOption . ResponseHeadersRead ) ;
123
113
124
114
var buffer = new byte [ 51200 ] ;
125
115
126
-
127
116
var s = await h . Content . ReadAsStreamAsync ( ) ;
128
117
long total = h . Content . Headers . ContentLength . GetValueOrDefault ( ) ;
129
118
130
119
IEnumerable < string > sl ;
131
120
if ( h . Headers . TryGetValues ( "Content-Length" , out sl ) )
132
121
{
133
- if ( total == 0 && sl . Any ( ) ) {
122
+ if ( total == 0 && sl . Any ( ) )
123
+ {
134
124
long . TryParse ( sl . FirstOrDefault ( ) , out total ) ;
135
125
}
136
126
}
137
127
138
-
139
128
int count = 0 ;
140
-
141
129
int progress = 0 ;
142
130
143
131
dlg . Max = ( int ) total ;
144
132
145
-
146
- while ( ( count = await s . ReadAsync ( buffer , 0 , buffer . Length ) ) > 0 ) {
133
+ while ( ( count = await s . ReadAsync ( buffer , 0 , buffer . Length ) ) > 0 )
134
+ {
147
135
148
136
await fout . WriteAsync ( buffer , 0 , count ) ;
149
137
@@ -155,10 +143,6 @@ await Task.Run(() =>
155
143
}
156
144
157
145
dlg . Hide ( ) ;
158
-
159
-
160
-
161
-
162
146
}
163
147
}
164
148
@@ -180,22 +164,18 @@ await Task.Run(() =>
180
164
/// <param name="cmd"></param>
181
165
/// <param name="logger"></param>
182
166
/// <returns></returns>
183
- public static async Task < int > Run ( Context context , string cmd , Action < string > logger = null ) {
184
-
167
+ public static async Task < int > Run ( Context context , string cmd , Action < string > logger = null )
168
+ {
185
169
try
186
170
{
187
171
TaskCompletionSource < int > source = new TaskCompletionSource < int > ( ) ;
188
172
189
-
190
-
191
173
await Instance . Init ( context ) ;
192
174
193
175
await Task . Run ( ( ) =>
194
176
{
195
177
try
196
178
{
197
-
198
-
199
179
int n = _Run ( context , cmd , logger ) ;
200
180
source . SetResult ( n ) ;
201
181
}
@@ -208,8 +188,8 @@ await Task.Run(() =>
208
188
209
189
return await source . Task ;
210
190
}
211
- catch ( Exception ex ) {
212
-
191
+ catch ( Exception ex )
192
+ {
213
193
System . Diagnostics . Debug . WriteLine ( ex ) ;
214
194
215
195
throw ex ;
@@ -218,16 +198,10 @@ await Task.Run(() =>
218
198
219
199
public static string EndOfFFMPEGLine = "final ratefactor:" ;
220
200
221
- private static int _Run (
222
- Context context ,
223
- string cmd ,
224
- Action < string > logger = null )
201
+ private static int _Run ( Context context , string cmd , Action < string > logger = null )
225
202
{
226
-
227
203
TaskCompletionSource < int > task = new TaskCompletionSource < int > ( ) ;
228
204
229
-
230
-
231
205
System . Diagnostics . Debug . WriteLine ( $ "ffmpeg initialized") ;
232
206
233
207
//var process = Java.Lang.Runtime.GetRuntime().Exec( Instance.ffmpegFile.CanonicalPath + " " + cmd );
@@ -248,8 +222,6 @@ private static int _Run(
248
222
249
223
process . Start ( ) ;
250
224
251
-
252
-
253
225
254
226
Task . Run ( ( ) =>
255
227
{
@@ -270,7 +242,8 @@ private static int _Run(
270
242
if ( line . StartsWith ( EndOfFFMPEGLine ) )
271
243
{
272
244
273
- Task . Run ( async ( ) => {
245
+ Task . Run ( async ( ) =>
246
+ {
274
247
await Task . Delay ( TimeSpan . FromMinutes ( 1 ) ) ;
275
248
finished = true ;
276
249
} ) ;
@@ -283,78 +256,22 @@ private static int _Run(
283
256
284
257
}
285
258
}
286
- catch ( Exception ex ) {
259
+ catch ( Exception ex )
260
+ {
287
261
System . Diagnostics . Debug . WriteLine ( ex ) ;
288
262
}
289
263
} ) ;
290
264
291
265
while ( ! finished )
292
266
{
293
267
process . WaitForExit ( 10000 ) ;
294
- if ( process . HasExited ) {
268
+ if ( process . HasExited )
269
+ {
295
270
break ;
296
271
}
297
272
}
298
273
299
274
return process . ExitCode ;
300
-
301
-
302
- }
303
-
304
-
305
- }
306
-
307
-
308
- public class FFMPEGSource {
309
-
310
-
311
- public static FFMPEGSource Get ( ) {
312
-
313
- string osArchitecture = Java . Lang . JavaSystem . GetProperty ( "os.arch" ) ;
314
-
315
- foreach ( var source in Sources ) {
316
- if ( source . IsArch ( osArchitecture ) )
317
- return source ;
318
- }
319
-
320
- throw new NotImplementedException ( ) ;
321
275
}
322
-
323
-
324
-
325
-
326
-
327
-
328
- public FFMPEGSource ( string arch , Func < string , bool > isArch , string hash )
329
- {
330
- this . Arch = arch ;
331
- this . IsArch = isArch ;
332
- this . Hash = hash ;
333
- }
334
-
335
- public static string FFMPEGVersion { get ; } = "3.0.1" ;
336
-
337
- public static FFMPEGSource [ ] Sources = new FFMPEGSource [ ] {
338
- new FFMPEGSource ( "arm" , x=> ! x . EndsWith ( "86" ) , "4nzzxDKxIYlvyK8tFH7/iNMHTdU=" ) ,
339
- new FFMPEGSource ( "x86" , x=> x . EndsWith ( "86" ) , "DdTbrTBf8Zeh6p5hWL0ggdIp5w4=" )
340
- } ;
341
-
342
- public string Arch { get ; }
343
-
344
- public string Hash { get ; }
345
-
346
-
347
- //https://cdn.rawgit.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
348
- //https://raw.githubusercontent.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
349
- public string Url => $ "https://{ FFMpegLibrary . Instance . CDNHost } /neurospeech/xamarin-android-ffmpeg/v1.0.7/binary/{ FFMPEGVersion } /{ Arch } /ffmpeg";
350
-
351
- public Func < string , bool > IsArch { get ; }
352
-
353
- public bool IsHashMatch ( byte [ ] data ) {
354
- var sha = System . Security . Cryptography . SHA1 . Create ( ) ;
355
- string h = Convert . ToBase64String ( sha . ComputeHash ( data ) ) ;
356
- return h == Hash ;
357
- }
358
-
359
276
}
360
277
}
0 commit comments