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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using Tango.Core;
using Tango.Core.Helpers;
using Tango.Core.IO;
namespace Tango.FSE.Common.RemoteDesktop
{
public class Mp4VideoEncoder : ExtendedObject, IDisposable
{
private class QueueItem
{
public BitmapSource Frame { get; set; }
public bool IsComplete { get; set; }
}
private Thread _queueThread;
private ProducerConsumerQueue<QueueItem> _framesQueue;
private TemporaryFolder _tempFolder;
private int _totalFrames;
private String _ffmpegPath;
public event EventHandler<TangoProgressChangedEventArgs<double>> Progress;
public bool IsStarted { get; set; }
public Mp4VideoEncoder()
{
_ffmpegPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), "ffmpeg.exe");
}
private void QueueThreadMethod()
{
_tempFolder = TemporaryManager.CreateFolder();
while (true)
{
var item = _framesQueue.BlockDequeue();
if (!item.IsComplete)
{
String file = Path.Combine(_tempFolder, "image" + (_totalFrames++) + ".png");
using (FileStream fs = new FileStream(file, FileMode.Create))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(item.Frame));
enc.Save(fs);
}
}
else
{
while (_framesQueue.Count > 0)
{
_framesQueue.BlockDequeue();
}
break;
}
}
}
public void Start()
{
if (!IsStarted)
{
_framesQueue = new ProducerConsumerQueue<QueueItem>();
IsStarted = true;
_totalFrames = 0;
_queueThread = new Thread(QueueThreadMethod);
_queueThread.IsBackground = true;
_queueThread.Start();
}
}
public void Cancel()
{
Stop();
ClearTemporaryFiles();
}
public void Stop()
{
if (IsStarted)
{
_framesQueue.BlockEnqueue(new QueueItem() { IsComplete = true });
IsStarted = false;
}
}
public Task Save(VideoConfiguration config, String outputFile)
{
if (IsStarted)
{
throw new InvalidOperationException("Stop before save!");
}
return Task.Factory.StartNew(() =>
{
try
{
//Wait for queue to complete.
while (_framesQueue.Count > 0) { }
String args = String.Empty;
int width = config.FrameWidth % 2 == 0 ? config.FrameWidth : config.FrameWidth + 1;
int height = config.FrameHeight % 2 == 0 ? config.FrameHeight : config.FrameHeight + 1;
args = $"-y -loglevel info -framerate {Math.Round(config.FrameRate, 1)} -i \"{Path.Combine(_tempFolder, "image")}%d.png\" -f mp4 -c:v libx264 -crf {(int)config.Quality} -vf scale={width}:{height} \"{outputFile}\"";
var startInfo = new ProcessStartInfo(_ffmpegPath, args);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
Regex progressRegEx = new Regex("frame= (\\d+) ");
var process = Process.Start(startInfo);
process.ErrorDataReceived += (x, e) =>
{
if (e.Data != null)
{
var match = progressRegEx.Match(e.Data);
if (match.Success && match.Groups.Count == 2)
{
int frame = int.Parse(match.Groups[1].Value);
OnProgress(frame, _totalFrames);
}
}
};
process.OutputDataReceived += (x, e) =>
{
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
throw ex;
}
finally
{
ClearTemporaryFiles();
}
});
}
public void PushFrame(BitmapSource source)
{
if (IsStarted)
{
var cloned = source.Clone();
cloned.Freeze();
_framesQueue.BlockEnqueue(new QueueItem() { Frame = cloned });
}
}
private void ClearTemporaryFiles()
{
_tempFolder?.DeleteAsync();
}
protected virtual void OnProgress(int frame, int totalFrames)
{
Progress?.Invoke(this, new TangoProgressChangedEventArgs<double>()
{
Progress = new TangoProgress<double>()
{
IsIndeterminate = false,
Maximum = totalFrames,
Value = frame,
},
});
}
public void Dispose()
{
Stop();
ClearTemporaryFiles();
}
}
}
|