-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.csx
107 lines (94 loc) · 3.8 KB
/
run.csx
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
// referencing added packages via portal.
#r "Newtonsoft.Json"
#r "Microsoft.AspNetCore.Mvc"
#r "Microsoft.AspNetCore.Http"
#r "Microsoft.AspNetCore.Http.Extensions"
#r "Microsoft.Azure.WebJobs"
#r "Microsoft.Azure.WebJobs.Extensions"
#r "Microsoft.Azure.WebJobs.Extensions.Http"
// using packages
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Net.Http;
using System.Net;
using System.Net.Http.Headers;
// Start function
public static async Task<IActionResult> Run(
HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Generate temporary input file and output file
var temp = Path.GetTempPath() + Path.GetRandomFileName() + ".webm";
var tempOut = Path.GetTempPath() + Path.GetRandomFileName() + ".wav";
try
{
// Get body and read byte information from it.
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
int commaStart = requestBody.IndexOf(",") + 1;
string webmstrdata = requestBody.Substring(commaStart, requestBody.Length - commaStart);
byte[] webmbytesdata = Convert.FromBase64String(webmstrdata);
File.WriteAllBytes(temp, webmbytesdata);
log.LogInformation( "Try 1 succeeded");
}
catch (Exception e)
{
log.LogInformation(e.Message);
return new BadRequestObjectResult(new ProblemDetails
{
Status = 400,
Title = "Exception: " + e.Message
});
}
try
{
// Start new process
var psi = new ProcessStartInfo();
psi.FileName = @"C:\home\site\wwwroot\ConvertAudioFormatUsingFFmpeg\ffmpeg.exe";
psi.Arguments = $"-i \"{temp}\" \"{tempOut}\"";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
log.LogInformation($"Args: {psi.Arguments}");
var process = Process.Start(psi);
process.WaitForExit((int)TimeSpan.FromSeconds(60).TotalMilliseconds);
string stdoutput = process.StandardOutput.ReadToEnd();
string stderror = process.StandardError.ReadToEnd();
log.LogInformation("FFMPEG: exitcode: " + process.ExitCode + "\r\n" + stdoutput + "\r\n" + stderror);
log.LogInformation("FFMPEG: stdoutput: " + stdoutput);
log.LogInformation("FFMPEG: stderror: " + stderror);
if (process.ExitCode != 0)
{
return new BadRequestObjectResult(new ProblemDetails
{
Status = 400,
Title = stderror
}
);
}
}
catch (Exception e)
{
log.LogInformation(e.Message);
return new BadRequestObjectResult(new ProblemDetails
{
Status = 400,
Title = "Exception2: " + e.Message
});
}
var bytes = File.ReadAllBytes(tempOut);
// Delete temp files
File.Delete(tempOut);
File.Delete(temp);
await Task.Run(() => { });
return new FileContentResult(bytes, "audio/wav");
}