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 CommandLine;
using CommandLine.Text;
using System.IO;
namespace TaskRunner
{
@ -21,9 +22,10 @@ namespace TaskRunner
}
public class Program
{
public static void Main(string[] args)
public static int Main(string[] args)
{
MainAsync(args).Wait();
return 0;
}
static async Task MainAsync(string[] args)
@ -35,6 +37,7 @@ namespace TaskRunner
var server = new ServerApi(new Uri(options.server));
try
{
var taskManager = new TaskManager();
var task = await server.GetTask();
var src = await server.GetStream("asdf", task.SourceStreamId);
var dest = await server.GetStream("asdf", task.DestinationStreamId);
@ -45,17 +48,30 @@ namespace TaskRunner
foreach (var strUri in src.Uris)
{
var uri = new Uri(strUri["Uri"]);
var fs = new FileStorage(@"\Users\Arti.Zirk\Documents\files");
var path = await fs.DownloadFile(uri);
Console.WriteLine(path);
var fs = new FileStorage();// @"\Users\Arti.Zirk\Documents\files");
Console.WriteLine($"From location: {uri}");
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) {
Console.WriteLine("No tasks available");
return;
}
var taskManager = new TaskManager();
await taskManager.RunFfmpeg();
//var taskManager = new TaskManager();
//await taskManager.RunFfmpeg();
} else {
//Console.WriteLine(options.GetUsage());
}

View File

@ -6,6 +6,7 @@ using System.Net.Http;
using TaskRunner.DTOs;
using Newtonsoft.Json;
using System.IO;
using System.Globalization;
namespace TaskRunner
{
@ -42,6 +43,17 @@ namespace TaskRunner
}
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
@ -60,7 +72,6 @@ namespace TaskRunner
}
var fileName = uri.Segments.Last();
var fullPath = Path.Combine(baseDir, fileName);
Console.WriteLine(fileName);
Directory.CreateDirectory(baseDir);
//File.Create(fullPath);
using (var fos = File.OpenWrite(fullPath)) {
@ -68,5 +79,7 @@ namespace TaskRunner
}
return fullPath;
}
}
}

View File

@ -8,15 +8,64 @@ namespace TaskRunner
{
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 process = new Process
{
StartInfo =
{
FileName = "ping",
Arguments = "google.com -t",
FileName = "avconv",
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,
UseShellExecute = false
},