123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using ImageMagick;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MovieBarcodeGenerator {
- public class BarcodeGenerator {
- 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.InfoFormat("Beginning barcode generation for {0} slices.", iterations);
- if (File.Exists(outputFile)) {
- log.InfoFormat("Output file '{0}' exists. Deleting file.", outputFile);
- File.Delete(outputFile);
- }
- 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
- log.Debug("Generating image slices.");
- 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("Appending image files.");
- // 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.Info("Cleaning up temporary files.");
- // clean up the work files
- string[] files = Directory.GetFiles(Path.GetDirectoryName(outputFile), "out_???.png");
- foreach (string file in files) {
- File.Delete(file);
- }
- }
- }
- }
|