-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
496 lines (445 loc) · 20.4 KB
/
Program.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using Accord.Math;
using Accord.Video.FFMPEG;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
namespace QtmVideoGridRender
{
class Program
{
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
enum TimecodePosition
{
TopLeft,
Center
}
static string[] FindAviFilesForDifferentCameras(string directory, string filenameWithoutExtension)
{
var aviFilesOqus = Directory.GetFiles(directory, filenameWithoutExtension + "_Oqus*.avi");
var aviFilesMiqus = Directory.GetFiles(directory, filenameWithoutExtension + "_Miqus*.avi");
var aviFilesArqus = Directory.GetFiles(directory, filenameWithoutExtension + "_Arqus*.avi");
return aviFilesOqus.Concat(aviFilesMiqus).Concat(aviFilesArqus).ToArray();
}
static void Main(string[] args)
{
try
{
// Do a search of all subdirectories
foreach (var filename in Directory.EnumerateFiles(".", "*.qtm", SearchOption.AllDirectories))
{
var fullpath = Path.GetFullPath(filename);
var directory = Path.GetDirectoryName(fullpath);
var filenameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
var outputfilename = filenameWithoutExtension + ".avi";
var outputfilepath = Path.Combine(directory, outputfilename);
if (!File.Exists(outputfilepath))
{
var aviFiles = FindAviFilesForDifferentCameras(directory, filenameWithoutExtension);
if (aviFiles.Length == 0)
{
Console.WriteLine("No avi files available for: " + filename);
continue;
}
Console.WriteLine("Starting to process: " + filename);
ProcessAViFiles(outputfilepath, aviFiles);
}
else
{
Console.WriteLine("Already existing avi file for: " + filename);
}
}
Console.WriteLine("Finished processing. Press key to close");
Console.ReadKey();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
class AviFileInfo : IDisposable
{
public int hour;
public int minute;
public int second;
public int frame;
public VideoFileReader reader;
public string filename;
public DateTime time;
public double framecounter;
public double timecodeFrequency;
private bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// Dispose managed state (managed objects)
}
if (reader != null)
{
reader.Dispose();
reader = null;
}
disposedValue = true;
}
}
~AviFileInfo()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
private static void ProcessAViFiles(string outputfilename, string[] aviFiles)
{
Size largestResolution = new Size();
long maxLength = 0;
double maxFrameRate = double.MinValue;
double minFrameRate = double.MaxValue;
int minBitRate = int.MaxValue;
int maxBitRate = int.MinValue;
List<AviFileInfo> afis = new List<AviFileInfo>();
int timecodeFoundIndex = -1;
Font timecodeTextFont = new Font("Tahoma", 70);
TimecodePosition timecodePosition = TimecodePosition.Center;
foreach (var file in aviFiles)
{
try
{
AviFileInfo afi = new AviFileInfo();
using (var tagFile = TagLib.File.Create(file))
{
if (tagFile != null)
{
int hour = 0;
int minute = 0;
int second = 0;
int frame = 0;
var tags = tagFile.Tag;
if (!string.IsNullOrEmpty(tags.Timecode))
{
try
{
Console.WriteLine("Reading timecode (" + tags.Timecode + ") tag in: " + file);
var parts = tags.Timecode.Split(':');
if (parts.Length > 0)
hour = int.Parse(parts[0]);
if (parts.Length > 1)
minute = int.Parse(parts[1]);
if (parts.Length > 2)
second = int.Parse(parts[2]);
if (parts.Length > 3)
frame = int.Parse(parts[3]);
timecodeFoundIndex = afis.Count;
}
catch (Exception)
{
hour = 0;
minute = 0;
second = 0;
frame = 0;
}
}
else
{
Console.WriteLine("No timecode found in: " + file);
}
afi.hour = hour;
afi.minute = minute;
afi.second = second;
afi.frame = frame;
afi.time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, afi.hour, afi.minute, afi.second);
double timecodeFrequency = 30;
if (!string.IsNullOrEmpty(tags.TimecodeFrequency))
{
Console.WriteLine("Reading timecode frequency (" + tags.TimecodeFrequency + ") tag in: " + file);
try
{
timecodeFrequency = double.Parse(tags.TimecodeFrequency);
}
catch (Exception)
{
}
}
else
{
Console.WriteLine("No timecode frequency found in: " + file);
}
afi.timecodeFrequency = timecodeFrequency;
afi.filename = file;
}
}
var reader = new VideoFileReader();
reader.Open(file);
largestResolution.Width = Math.Max(largestResolution.Width, reader.Width);
largestResolution.Height = Math.Max(largestResolution.Height, reader.Height);
minBitRate = Math.Min(minBitRate, reader.BitRate);
maxBitRate = Math.Max(maxBitRate, reader.BitRate);
maxLength = Math.Max(maxLength, reader.FrameCount);
minFrameRate = Math.Min(minFrameRate, reader.FrameRate.ToDouble());
maxFrameRate = Math.Max(maxFrameRate, reader.FrameRate.ToDouble());
afi.reader = reader;
afis.Add(afi);
}
catch (Exception e)
{
Console.WriteLine("Could not open avi file (" + e.Message + ") : " + file);
}
}
try
{
// timestamp from first found timestamp video (or read from qtm file?), fixed timestamp frequency...
double timestampFrequency = Math.Min(maxFrameRate, 30);
DateTime timestamp = afis[0].time;
int timestampFrame = afis[0].frame;
if (timecodeFoundIndex >= 0)
{
timestampFrequency = afis[timecodeFoundIndex].timecodeFrequency;
timestamp = afis[timecodeFoundIndex].time;
timestampFrame = afis[timecodeFoundIndex].frame;
}
double timestampCounter = 0;
// Determine how many rows and columns it should contain
var gridSizeAndScale = GetOutputGridSizes(aviFiles.Length);
// TODO::: Be able to specify output size and resize all bitmaps accordingly.
Size outputSize = new Size((int)(largestResolution.Width * gridSizeAndScale.X * gridSizeAndScale.Scale), (int)(largestResolution.Height * gridSizeAndScale.Y * gridSizeAndScale.Scale));
Console.WriteLine("Writing file: " + outputfilename + " " + outputSize.Width.ToString() + "x" + outputSize.Height.ToString() + " Frequency: " + maxFrameRate + " Bitrate: " + minBitRate.ToString());
var writer = new VideoFileWriter();
writer.Width = outputSize.Width;
writer.Height = outputSize.Height;
writer.FrameRate = new Rational(maxFrameRate);
writer.BitRate = minBitRate;
writer.VideoCodec = VideoCodec.Mjpeg;
writer.PixelFormat = AVPixelFormat.FormatYuvj422P;
writer.AudioCodec = AudioCodec.None;
writer.Open(outputfilename);
Bitmap[] bitmaps = new Bitmap[afis.Count];
var bigBitmap = new Bitmap(outputSize.Width, outputSize.Height);
Graphics graphicsContext = Graphics.FromImage(bigBitmap);
int frameNumberToWrite = 0;
while (frameNumberToWrite++ < maxLength)
{
Console.WriteLine("Writing frame: " + frameNumberToWrite.ToString() + "/" + maxLength.ToString());
int afiIndex = 0;
foreach (var afi in afis)
{
// If readers have different framerates we need to interpolate lower framerate to the highest.
var framestep = afi.reader.FrameRate.ToDouble() / maxFrameRate;
afi.framecounter += framestep;
Bitmap bitmap = null;
if (afi.framecounter >= 1)
{
if (bitmaps[afiIndex] != null)
bitmaps[afiIndex].Dispose();
try
{
bitmap = afi.reader.ReadVideoFrame();
bitmap = (gridSizeAndScale.Scale != 1.0) ? ResizeImage(bitmap, (int)(bitmap.Width * gridSizeAndScale.Scale), (int)(bitmap.Height * gridSizeAndScale.Scale)) : bitmap;
//bitmap.Save(afi.filename + "_" + frameNumberToWrite.ToString() + ".bmp");
}
catch (Exception)
{
// No frames left?
}
afi.framecounter -= 1;
}
else
{
if (bitmaps[afiIndex] == null)
{
try
{
bitmap = afi.reader.ReadVideoFrame();
bitmap = (gridSizeAndScale.Scale != 1.0) ? ResizeImage(bitmap, (int)(bitmap.Width * gridSizeAndScale.Scale), (int)(bitmap.Height * gridSizeAndScale.Scale)) : bitmap;
}
catch (Exception)
{
// No frames left?
}
}
else
bitmap = bitmaps[afiIndex];
}
bitmaps[afiIndex] = bitmap;
afiIndex++;
}
{
int coordx = 0;
int coordy = 0;
int indexOnLine = 1;
for (int index = 0; index < afis.Count; index++)
{
if (bitmaps[index] != null)
{
graphicsContext.DrawImage(bitmaps[index], coordx, coordy);
}
coordx += bitmaps[index].Width;
if (indexOnLine++ >= gridSizeAndScale.X)
{
indexOnLine = 1;
coordx = 0;
coordy += bitmaps[index].Height;
}
}
graphicsContext.SmoothingMode = SmoothingMode.AntiAlias;
graphicsContext.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsContext.PixelOffsetMode = PixelOffsetMode.HighQuality;
string timeToWrite = (timecodeFoundIndex >= 0) ? string.Format($"{timestamp.ToString("HH:mm:ss")}.{timestampFrame:D2}") : string.Format($"{timestamp.ToString("HH:mm:ss")}");
var size = graphicsContext.MeasureString(timeToWrite, timecodeTextFont);
RectangleF rect = new RectangleF(10, 10, size.Width, size.Height);
if (timecodePosition == TimecodePosition.Center)
{
rect.X = (outputSize.Width / 2) - (size.Width / 2);
rect.Y = (outputSize.Height / 2) - (size.Height / 2);
rect.Width = size.Width;
rect.Height = size.Height;
}
graphicsContext.FillRectangle(Brushes.Black, rect);
graphicsContext.DrawString(timeToWrite, timecodeTextFont, Brushes.White, rect);
graphicsContext.Flush();
}
var timestampStep = timestampFrequency / maxFrameRate;
timestampCounter += timestampStep;
if (timestampCounter >= 1)
{
timestampFrame++;
timestampCounter -= 1;
}
if (timestampFrame >= timestampFrequency)
{
timestampFrame = 0;
timestamp = timestamp.AddSeconds(1);
}
writer.WriteVideoFrame(bigBitmap);
//bigBitmap.Save(outputfilename + "_" + frameNumberToWrite.ToString() + ".bmp");
}
writer.Close();
graphicsContext.Dispose();
bigBitmap.Dispose();
writer.Dispose();
foreach (var afi in afis)
{
afi.Dispose();
}
if (timecodeFoundIndex >= 0)
{
timestampFrequency = afis[timecodeFoundIndex].timecodeFrequency;
timestamp = afis[timecodeFoundIndex].time;
timestampFrame = afis[timecodeFoundIndex].frame;
using (var tagFile = TagLib.File.Create(outputfilename))
{
if (tagFile != null)
{
tagFile.Tag.Timecode = string.Format($"{timestamp.ToString("HH:mm:ss")}.{timestampFrame:D2}");
// tagFile.Tag.TimecodeFrequency = ((int)timestampFrequency).ToString();
tagFile.Save();
}
}
}
afis.Clear();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
class GridSize
{
public GridSize(int x, int y, double scale = 1.0)
{
X = x;
Y = y;
Scale = scale;
}
public int X;
public int Y;
public double Scale;
}
/// <summary>
/// Returns a grid size depending on number of files
/// </summary>
/// <param name="numberOfFiles"></param>
/// <returns></returns>
private static GridSize GetOutputGridSizes(int numberOfFiles)
{
switch (numberOfFiles)
{
case 1:
return new GridSize(1, 1);
case 2:
return new GridSize(2, 1);
case 3:
return new GridSize(3, 1);
case 4:
return new GridSize(2, 2);
case 5:
case 6:
return new GridSize(3, 2);
case 7:
case 8:
return new GridSize(4, 2);
case 9:
return new GridSize(3, 3);
case 10:
return new GridSize(5, 2, 0.5);
case 11:
case 12:
return new GridSize(4, 2, 0.5);
case 15:
return new GridSize(5, 3, 0.5);
case 13:
case 14:
case 16:
return new GridSize(4, 4, 0.5);
case 17:
case 18:
case 19:
case 20:
return new GridSize(5, 4, 0.5);
}
var size = (int)Math.Ceiling(numberOfFiles / 2.0);
return new GridSize(size, size, 0.5);
}
}
}