Skip to content

Instantly share code, notes, and snippets.

@feedsbrain
Forked from jakkaj/run.csx
Created April 28, 2017 15:30
Show Gist options
  • Save feedsbrain/a622743b61a79f9340e78e4ea975630a to your computer and use it in GitHub Desktop.
Save feedsbrain/a622743b61a79f9340e78e4ea975630a to your computer and use it in GitHub Desktop.
FFMPEG inside an Azure function.
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;
using System.Diagnostics;
using System.IO;
public static async Task Run(Stream myBlob, string name, Binder binder, TraceWriter log)
{
log.Info($"Jordan C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
byte[] bytes = null;
using(var ms = new MemoryStream()){
myBlob.CopyTo(ms);
bytes = ms.ToArray();
}
var f = @"D:\home\site\wwwroot\Timelapser\ffmpeg.exe";
var temp = Path.GetTempFileName();
var tempOut = Path.GetTempFileName() + ".mpg";
var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
File.WriteAllBytes(temp, bytes);
log.Info($"Exists: {File.Exists(temp)}");
var readBack = File.ReadAllBytes(temp);
log.Info($"ReadBack: {readBack.Length}, {temp}");
var psi = new ProcessStartInfo();
psi.FileName = f;
psi.Arguments = $"-i \"{temp}\" -y -s 640x360 -b:v 1024k -r 29.7 -movflags faststart -pix_fmt yuv420p \"{tempOut}\"";
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
log.Info($"Args: {psi.Arguments}");
var process = Process.Start(psi);
//string output = process.StandardOutput.ReadToEnd();
//string err = process.StandardError.ReadToEnd();
process.WaitForExit((int)TimeSpan.FromSeconds(60).TotalMilliseconds);
// log.Info(output);
// log.Info(err);
log.Info($"Output: {process.ExitCode}");
log.Info($"Temp Out Exists: {File.Exists(tempOut)}");
var attributes = new Attribute[]
{
new BlobAttribute("renc/" + name + "_reenc.mpg"),
new StorageAccountAttribute("jortana_STORAGE")
};
var renc = File.ReadAllBytes(tempOut);
log.Info($"Renc Length: {renc.Length}");
var writer = await binder.BindAsync<CloudBlockBlob>(attributes);
// await writer.UploadFromByteArrayAsync(renc, 0, renc.Length);
File.Delete(tempOut);
File.Delete(temp);
Directory.Delete(tempPath, true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment