This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
/
HyperlapseWrapper.cs
124 lines (98 loc) · 5.53 KB
/
HyperlapseWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using Microsoft.Research.Hyperlapse.Desktop;
using Microsoft.Research.VisionTools.Toolkit;
using Microsoft.Research.Hyperlapse;
using System;
using System.IO;
using System.Linq;
using System.Threading;
// To resolve conflict between Microsoft.Research.Hyperlapse and Microsoft.Research.Hyperlapse.Desktop
using VideoBitrateEstimator = Microsoft.Research.Hyperlapse.VideoBitrateEstimator;
namespace HyperlapseBatchProcessor
{
class HyperlapseWrapper
{
private static AutoResetEvent processingEvent = new AutoResetEvent(false);
private static VideoReader videoReader = new VideoReader();
private static CalibrationProvider calibrationProvider = new CalibrationProvider();
private static CalibrationMatcher calibrationMatcher = new CalibrationMatcher(calibrationProvider);
private static VideoBitrateEstimator videoBitrateEstimator = new VideoBitrateEstimator();
public static void ProcessFiles(FileInfo[] files, int speedupFactor, Rational outputFramesPerSecond)
{
var i = 1;
var filesCount = files.Count();
var nativeEngine = new HyperlapseNative.HyperlapseEngine();
var productInfo = new ProductInfo();
var engine = new HyperlapseEngine(nativeEngine, productInfo);
engine.ProcessingCancelled += OnEngineProcessingCancelled;
engine.ProcessingFailed += OnEngineProcessingFailed;
engine.ProcessingFinished += OnEngineProcessingFinished;
engine.ProgressChanged += OnEngineProgressChanged;
engine.ActivationStatusChanged += OnEngineActivationStatusChanged;
foreach (var file in files)
{
var processingMsg = "[Processing file " + i++ + " of " + filesCount + "] - " + file.Name;
Console.Title = processingMsg;
Console.WriteLine(processingMsg);
var fileOutput = new FileInfo(Path.Combine(file.DirectoryName, "Output", file.Name));
if (!fileOutput.Directory.Exists)
{
fileOutput.Directory.Create(); // create output dir
}
Process(engine, file, fileOutput, speedupFactor, outputFramesPerSecond);
}
engine.Dispose();
}
private static void Process(HyperlapseEngine engine, FileInfo fileInput, FileInfo fileOutput, int speedupFactor, Rational outputFramesPerSecond)
{
var hyperlapseParameters = new HyperlapseParameters();
var videoInfo = videoReader.ReadInfoFromFile(fileInput.FullName);
var calibrationInfoForVideo = calibrationMatcher.FindCalibrationInfoForVideo(videoInfo);
hyperlapseParameters.CalibrationFile = calibrationInfoForVideo.Calibration;
hyperlapseParameters.StartFrame = 0;
hyperlapseParameters.EndFrame = int.MaxValue; // all video
hyperlapseParameters.VideoMode = calibrationInfoForVideo.VideoMode;
hyperlapseParameters.FrameRate = videoInfo.FramesPerSecond;
hyperlapseParameters.OutputHeight = videoInfo.Height;
hyperlapseParameters.OutputRotation = videoInfo.Rotation;
hyperlapseParameters.OutputWidth = videoInfo.Width;
hyperlapseParameters.OutputBitrate = (int)videoBitrateEstimator.EstimateBitsPerSecond(videoInfo.BitsPerSecond,
videoInfo.Width,
videoInfo.Height,
videoInfo.Width,
videoInfo.Height,
videoInfo.FramesPerSecond,
outputFramesPerSecond ?? videoInfo.FramesPerSecond);
hyperlapseParameters.SpeedupFactor = speedupFactor;
hyperlapseParameters.VideoUri = new Uri(fileInput.FullName);
hyperlapseParameters.VideoOutputFilePath = fileOutput.FullName;
hyperlapseParameters.TempOutputDirectory = fileOutput.DirectoryName;
hyperlapseParameters.CreditLength = 0;
hyperlapseParameters.UseAdvancedSmoothing = false;
hyperlapseParameters.ForceSoftwareRendering = false;
hyperlapseParameters.UseGeometryShaders = false;
hyperlapseParameters.UseHardwareVideoEncoder = false; // some videos fail when this is on
engine.Start(hyperlapseParameters);
processingEvent.WaitOne();
}
private static void OnEngineActivationStatusChanged(object sender, EventArgs e)
{
}
private static void OnEngineProgressChanged(object sender, EventArgs e)
{
}
private static void OnEngineProcessingFinished(object sender, ProcessingFinishedEventArgs e)
{
processingEvent.Set();
}
private static void OnEngineProcessingFailed(object sender, ProcessingFailedEventArgs e)
{
Console.WriteLine("Error: " + e.ErrorMessage);
processingEvent.Set();
}
private static void OnEngineProcessingCancelled(object sender, EventArgs e)
{
Console.WriteLine("[Cancelled]");
processingEvent.Set();
}
}
}