Working video conversion

This commit is contained in:
Arti Zirk 2017-05-25 16:36:25 +03:00
parent 54472c5f0a
commit 770d6cf264
3 changed files with 88 additions and 10 deletions

View File

@ -5,6 +5,7 @@ using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommandLine; using CommandLine;
using CommandLine.Text; using CommandLine.Text;
using System.IO;
namespace TaskRunner namespace TaskRunner
{ {
@ -21,9 +22,10 @@ namespace TaskRunner
} }
public class Program public class Program
{ {
public static void Main(string[] args) public static int Main(string[] args)
{ {
MainAsync(args).Wait(); MainAsync(args).Wait();
return 0;
} }
static async Task MainAsync(string[] args) static async Task MainAsync(string[] args)
@ -35,6 +37,7 @@ namespace TaskRunner
var server = new ServerApi(new Uri(options.server)); var server = new ServerApi(new Uri(options.server));
try try
{ {
var taskManager = new TaskManager();
var task = await server.GetTask(); var task = await server.GetTask();
var src = await server.GetStream("asdf", task.SourceStreamId); var src = await server.GetStream("asdf", task.SourceStreamId);
var dest = await server.GetStream("asdf", task.DestinationStreamId); var dest = await server.GetStream("asdf", task.DestinationStreamId);
@ -45,17 +48,30 @@ namespace TaskRunner
foreach (var strUri in src.Uris) foreach (var strUri in src.Uris)
{ {
var uri = new Uri(strUri["Uri"]); var uri = new Uri(strUri["Uri"]);
var fs = new FileStorage(@"\Users\Arti.Zirk\Documents\files"); var fs = new FileStorage();// @"\Users\Arti.Zirk\Documents\files");
var path = await fs.DownloadFile(uri); Console.WriteLine($"From location: {uri}");
Console.WriteLine(path); var srcSwf = await fs.DownloadFile(uri);
Console.WriteLine($"Local scrSwf: {srcSwf}");
var justName = Path.GetFileNameWithoutExtension(srcSwf);
var destSwf = $"/tmp/{justName}.raw";
var destMp4 = $"/tmp/{justName}.mp4";
Console.WriteLine("RunGnash");
await taskManager.RunGnash(srcSwf, destSwf);
File.Delete(srcSwf);
Console.WriteLine("RunFfmpeg");
await taskManager.swfRawToMp4(destSwf, destMp4);
File.Delete(destSwf);
var location = await server.UploadFile(destMp4);
//File.Delete(destMp4);
Console.WriteLine($"New location is {location}");
} }
} catch (FailedToGetTaskException) { } catch (FailedToGetTaskException) {
Console.WriteLine("No tasks available"); Console.WriteLine("No tasks available");
return; return;
} }
var taskManager = new TaskManager(); //var taskManager = new TaskManager();
await taskManager.RunFfmpeg(); //await taskManager.RunFfmpeg();
} else { } else {
//Console.WriteLine(options.GetUsage()); //Console.WriteLine(options.GetUsage());
} }

View File

@ -6,6 +6,7 @@ using System.Net.Http;
using TaskRunner.DTOs; using TaskRunner.DTOs;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.IO; using System.IO;
using System.Globalization;
namespace TaskRunner namespace TaskRunner
{ {
@ -42,6 +43,17 @@ namespace TaskRunner
} }
return JsonConvert.DeserializeObject<StreamDTO>(await resp.Content.ReadAsStringAsync()); return JsonConvert.DeserializeObject<StreamDTO>(await resp.Content.ReadAsStringAsync());
} }
public async Task<Uri> UploadFile(string file)
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StreamContent(File.OpenRead(file)), "file", Path.GetFileName(file));
var resp = await client.PostAsync("/Files", content);
return resp.Content.Headers.ContentLocation;
}
}
} }
public class FileStorage public class FileStorage
@ -60,7 +72,6 @@ namespace TaskRunner
} }
var fileName = uri.Segments.Last(); var fileName = uri.Segments.Last();
var fullPath = Path.Combine(baseDir, fileName); var fullPath = Path.Combine(baseDir, fileName);
Console.WriteLine(fileName);
Directory.CreateDirectory(baseDir); Directory.CreateDirectory(baseDir);
//File.Create(fullPath); //File.Create(fullPath);
using (var fos = File.OpenWrite(fullPath)) { using (var fos = File.OpenWrite(fullPath)) {
@ -68,5 +79,7 @@ namespace TaskRunner
} }
return fullPath; return fullPath;
} }
} }
} }

View File

@ -8,15 +8,64 @@ namespace TaskRunner
{ {
public class TaskManager public class TaskManager
{ {
public Task RunFfmpeg() public Task RunGnash(string src, string dest)
{
var task = new TaskCompletionSource<bool>();
var process = new Process
{
StartInfo =
{
FileName = "dump-gnash",
Arguments = $"-1 -D \"{dest}\" \"{src}\"",
RedirectStandardOutput = true,
UseShellExecute = false
},
EnableRaisingEvents = true
};
process.OutputDataReceived += (sender, a) => Console.WriteLine(a.Data);
process.Start();
process.Exited += (sender, args) =>
{
task.SetResult(true);
process.Dispose();
};
return task.Task;
}
public Task swfRawToMp4(string src, string dest)
{ {
var task = new TaskCompletionSource<bool>(); var task = new TaskCompletionSource<bool>();
var process = new Process var process = new Process
{ {
StartInfo = StartInfo =
{ {
FileName = "ping", FileName = "avconv",
Arguments = "google.com -t", Arguments = $"-f rawvideo -pix_fmt rgb32 -s:v 1024x576 -r 24 -i \"{src}\" -c:v libx264 -pix_fmt yuv420p \"{dest}\"",
RedirectStandardOutput = true,
UseShellExecute = false
},
EnableRaisingEvents = true
};
process.OutputDataReceived += (sender, a) => Console.WriteLine(a.Data);
process.Start();
process.BeginOutputReadLine();
process.Exited += (sender, args) =>
{
task.SetResult(true);
process.Dispose();
};
return task.Task;
}
public Task Mp4Join(IList<string> src, string dest)
{
var task = new TaskCompletionSource<bool>();
var process = new Process
{
StartInfo =
{
FileName = "avconv",
Arguments = $"-f rawvideo -pix_fmt rgb32 -s:v 1024x576 -r 24 -i \"{src}\" -c:v libx264 -pix_fmt yuv420p \"{dest}\"",
RedirectStandardOutput = true, RedirectStandardOutput = true,
UseShellExecute = false UseShellExecute = false
}, },