commit 8ada6bc0ac6c33097af4eb00df5cc0cd870ae1eb Author: Adam R. Grey Date: Tue Oct 12 00:46:12 2021 -0400 draft 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b6df02c --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,26 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/net5.0/videoRetailor.dll", + "args": [], + "cwd": "${workspaceFolder}/sample recording sess/", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..3fbc42b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/videoRetailor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/videoRetailor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/videoRetailor.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..6bbf3d9 --- /dev/null +++ b/Program.cs @@ -0,0 +1,109 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using FFMpegCore; + +namespace videoRetailor +{ + public class analyzedMedia + { + public List path { get; set; } = new List(); + public List ffprobed { get; set; } = new List(); + public double TotalSeconds { get { return ffprobed.Sum(ima => ima.Duration.TotalSeconds); } } + public double durationVariance { get; set; } + } + class Program + { + static async Task Main(string[] args) + { + Console.WriteLine(Directory.GetCurrentDirectory()); + var mkvFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.mkv"); + + var mediaInfos = new List(); + var totalTime = new TimeSpan(); + foreach (var file in mkvFiles.OrderBy(f => f)) + { + var thisMeta = new analyzedMedia(); + thisMeta.path.Add(file); + thisMeta.ffprobed.Add(await FFProbe.AnalyseAsync(file)); + mediaInfos.Add(thisMeta); + } + if (mediaInfos.Count < 8) + { + Console.WriteLine($"there's only {mkvFiles.Length}, can't recombine. just going to send them all as-is."); + } + else + { + var afterFirst = mediaInfos.Skip(1).ToList(); + foreach (var thisMeta in afterFirst) + { + totalTime += thisMeta.ffprobed[0].Duration; + } + var averageTime = totalTime.TotalSeconds / afterFirst.Count(); + Console.WriteLine($"average time: {averageTime}"); + foreach (var thisMeta in afterFirst) + { + thisMeta.durationVariance = thisMeta.TotalSeconds / averageTime; + Console.WriteLine($"{string.Join(", ", thisMeta.path)}: {thisMeta.TotalSeconds} seconds"); + Console.WriteLine($" duration ratio: {thisMeta.durationVariance}"); + } + Console.WriteLine($"...am I crashing? {afterFirst.Min(af => af.durationVariance)}"); + while (afterFirst.Count() > 6 && + (afterFirst.Min(af => af.durationVariance) < 0.9)) + { + Console.WriteLine("gluestick at the ready."); + var idx = 0; + var targetMinDuration = afterFirst.Min(af => af.durationVariance); + Console.WriteLine($"targetMinDuration = {targetMinDuration}"); + foreach (var af in afterFirst) + { + if (af.durationVariance == targetMinDuration) + { + break; + } + idx++; + } + var previousDuration = (idx == 0) ? Double.MaxValue : afterFirst[idx - 1].TotalSeconds; + var nextDuration = (idx > afterFirst.Count() - 2) ? Double.MaxValue : afterFirst[idx - 1].TotalSeconds; + analyzedMedia partner; + if (nextDuration > previousDuration) + { + partner = afterFirst[idx - 1]; + Console.WriteLine($"combining with previous, previously at {partner.TotalSeconds}"); + partner.ffprobed.AddRange(afterFirst[idx].ffprobed); + partner.path.AddRange(afterFirst[idx].path); + } + else + { + partner = afterFirst[idx + 1]; + Console.WriteLine($"combining with next, previously at {partner.TotalSeconds}"); + afterFirst[idx].ffprobed.AddRange(partner.ffprobed); + partner.ffprobed = afterFirst[idx].ffprobed; + afterFirst[idx].path.AddRange(partner.path); + partner.path = afterFirst[idx].path; + } + afterFirst.RemoveAt(idx); + averageTime = totalTime.TotalSeconds / afterFirst.Count(); + foreach (var thisMeta in afterFirst) + { + thisMeta.durationVariance = thisMeta.TotalSeconds / averageTime; + } + } + mediaInfos = mediaInfos.Take(1).Union(afterFirst).ToList(); + } + Console.WriteLine($"{mediaInfos.Count()} infos."); + var i = 1; + foreach (var mi in mediaInfos) + { + Console.WriteLine($"going to export {string.Join(", ", mi.path)} as part {i}"); + FFMpeg.Join($"./{DateTime.Today.ToShortDateString()} part{i}.mp4", + mi.path.ToArray() + ); + + i++; + } + } + } +} diff --git a/videoRetailor.csproj b/videoRetailor.csproj new file mode 100644 index 0000000..6577137 --- /dev/null +++ b/videoRetailor.csproj @@ -0,0 +1,12 @@ + + + + Exe + net5.0 + + + + + + +