-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
158 lines (130 loc) · 5.91 KB
/
Program.cs
File metadata and controls
158 lines (130 loc) · 5.91 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace VideoStitch
{
class Program
{
static string ffmpeg = @"d:\Video";
static string inputDirectory = @"d:\Video";
static string outputDirectory = @"d:\Video\output";
static int Main(string[] args)
{
var options = new Options();
var parser = new CommandLine.Parser();
if (parser.ParseArguments(args, options))
{
inputDirectory = options.InputDirectory;
outputDirectory = options.OutputDirectory;
ffmpeg = MakeFilename(options.FfmpegDirectory, "ffmpeg.exe");
}
// get top-level directories which represent cameras
string[] cameras = Directory.GetDirectories(inputDirectory);
Console.Out.WriteLine("Starting VideoStitch...");
// if there are no top-level directors, there are no cameras to process
if (cameras.Length < 1)
{
WriteErrorMessage("Unable to find any video camera directories");
return -1;
}
Console.Out.WriteLine("Found " + cameras.Length + " cameras.");
foreach (string camera in cameras)
{
Console.Out.WriteLine("Processing video for camera [" + camera + "]");
// get year directories
string[] years = Directory.GetDirectories(camera);
Console.Out.WriteLine("Found " + years.Length + " years");
foreach (string year in years)
{
// get month directories
string[] months = Directory.GetDirectories(year);
Console.Out.WriteLine("Found " + months.Length + " months");
foreach (string month in months)
{
// get day directories
string[] days = Directory.GetDirectories(month);
Console.Out.WriteLine("Found " + days.Length + " days");
foreach (string day in days)
{
// get individual clips
string[] clips = Directory.GetFiles(day);
Console.Out.WriteLine("Found " + clips.Length + " clips");
List<string> stitches = new List<string>();
DateTime lastCreateTime = DateTime.MaxValue;
foreach (string clip in clips) {
// make sure the file is an mp4 file
if (Path.GetExtension(clip).Equals(".mp4"))
{
DateTime createTime = File.GetLastWriteTime(clip);
TimeSpan interval = createTime - lastCreateTime;
// if there is a greater then 5 second gap, its a new video
if (interval.TotalSeconds > 3)
{
Console.Out.WriteLine("Stitching video with " + stitches.Count + " clips");
lastCreateTime = DateTime.MaxValue;
string configFile = WriteConfigFile(day, stitches.ToArray());
ProcessConfigFile(day, configFile);
DeleteConfigFile(day, configFile);
stitches.Clear();
}
else
{
lastCreateTime = createTime;
stitches.Add(clip);
}
}
}
}
}
}
}
Console.Out.WriteLine("VideoStitch completed.");
return 1;
}
static void WriteErrorMessage(string msg)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine(msg);
Console.ResetColor();
}
static string WriteConfigFile(string directory, string[] clips)
{
DateTime now = DateTime.Now;
string name = now.ToFileTimeUtc().ToString();
string fullPath = MakeFilename(directory, name, "txt");
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(fullPath))
{
foreach (string clip in clips)
{
file.WriteLine("file '" + clip + "'");
}
}
return name;
}
static void DeleteConfigFile(string fullPath, string file)
{
File.Delete(MakeFilename(fullPath, file, "txt"));
}
static void ProcessConfigFile(string fullPath, string baseFileName)
{
string paramString = "-f concat -safe 0 -i " + MakeFilename(fullPath, baseFileName, "txt") + " -c copy " + MakeFilename(outputDirectory, baseFileName, "mp4");
var processInfo = new ProcessStartInfo(ffmpeg, paramString);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = processInfo;
process.Start();
process.WaitForExit();
}
static string MakeFilename(string path, string name, string extension)
{
return Path.ChangeExtension(Path.Combine(path, name), "." + extension);
}
static string MakeFilename(string path, string name)
{
return Path.Combine(path, name);
}
}
}