1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using ImageMagick;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MovieBarcodeGenerator {
- public class BarcodeGenerator {
- public static string imagickPath;
- private static log4net.ILog log = log4net.LogManager.GetLogger("Generator");
- public static string ffmpegPath {
- get {
- return SharpFF.ffmpegPath;
- }
- set {
- SharpFF.ffmpegPath = value;
- }
- }
- public static void Generate(string inputFile, string outputFile, int height, int width) {
- Generate(inputFile, outputFile, height, 1, width);
- }
- public static void Generate(string inputFile, string outputFile, int height, int barWidth, int iterations) {
- log.Debug("Generate()");
- if (File.Exists(outputFile)) {
- log.InfoFormat("Output file '{0}' exists. Deleting file.", outputFile);
- File.Delete(outputFile);
- }
- // set the path because .Net uses the "convert" utility on Windows by default
- System.Environment.SetEnvironmentVariable("Path", imagickPath);
- decimal videoLength = SharpFF.GetDuration(inputFile);
- // run these in parallel to save time
- // TODO: do all this work in a temp folder, then move the finished file to the destination
- Parallel.For(0, iterations, i => {
- string timecodeAt = SharpFF.SecondsToTimecode(i * (videoLength / iterations));
- SharpFF.ExecuteCommand(string.Format("-hide_banner -loglevel panic -nostats -y -ss {1} -i \"{0}\" -vframes 1 -an -f rawvideo -vcodec png -vf scale={4}:{5} \"{2}\\out_{3:000}.png\"", inputFile, timecodeAt, Path.GetDirectoryName(outputFile), i, barWidth, height));
- });
- log.Debug("Scrunching PNGs together.");
- // use ImageMagick to crush the generated PNGs together
- using (MagickImageCollection images = new MagickImageCollection()) {
- foreach (var file in Directory.GetFiles(Path.GetDirectoryName(outputFile), "out_???.png")) {
- images.Add(new MagickImage(file));
- }
- // create a strip from all those images
- using (IMagickImage result = images.AppendHorizontally()) {
- // Save the result
- result.Write(outputFile);
- }
- }
- log.Debug("Deleting temporary work files.");
- // clean up the work files
- string[] files = Directory.GetFiles(Path.GetDirectoryName(outputFile), "out_???.png");
- foreach (string file in files) {
- File.Delete(file);
- }
- }
- }
- }
|