-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix filenames using single quotes (#3)
* refactor * support filenames that use single quotes
- Loading branch information
1 parent
72cad3f
commit 330e9bc
Showing
11 changed files
with
257 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace MkChap; | ||
|
||
public class BlackDetect : FFprobeBase | ||
{ | ||
public static async Task<string> Detect(string? inputFile, double minBlackSeconds, double ratioBlackPixels, | ||
double blackPixelThreshold) | ||
{ | ||
if (string.IsNullOrWhiteSpace(inputFile)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
inputFile = FixFileName(inputFile); | ||
return await GetFFprobeOutput(new List<string> | ||
{ | ||
"-f", "lavfi", | ||
"-i", | ||
$"movie={inputFile},blackdetect=d={minBlackSeconds}:pic_th={ratioBlackPixels}:pix_th={blackPixelThreshold}[out0]", | ||
"-show_entries", "frame_tags=lavfi.black_start,lavfi.black_end", | ||
"-of", "default=nw=1", | ||
"-v", "panic" | ||
}); | ||
} | ||
|
||
private static string FixFileName(string inputFile) | ||
{ | ||
// rework filename in a format that works on windows | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
// \ is escape, so use / for directory separators | ||
inputFile = inputFile.Replace(@"\", "/"); | ||
|
||
// colon after drive letter needs to be escaped | ||
inputFile = inputFile.Replace(":/", @"\:/"); | ||
} | ||
|
||
// escape apostrophes | ||
inputFile = inputFile.Replace("'", @"\\\'"); | ||
|
||
return inputFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
using MkChap.Models; | ||
|
||
namespace MkChap; | ||
|
||
public class ChapterWriter | ||
{ | ||
public static async Task WriteToFile(string? inputFile, string outputFile, List<Chapter> chapters) | ||
{ | ||
if (string.IsNullOrWhiteSpace(inputFile)) | ||
{ | ||
return; | ||
} | ||
|
||
var ffMetadata = GetFFMetadata(chapters); | ||
var metadataFile = string.Empty; | ||
|
||
try | ||
{ | ||
metadataFile = await WriteFFMetadata(ffMetadata); | ||
await WriteMetadataToFile(inputFile, outputFile, metadataFile); | ||
} | ||
finally | ||
{ | ||
try | ||
{ | ||
if (File.Exists(metadataFile)) | ||
{ | ||
File.Delete(metadataFile); | ||
} | ||
} | ||
catch | ||
{ | ||
// do nothing | ||
} | ||
} | ||
} | ||
|
||
private static string GetFFMetadata(List<Chapter> chapters) | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
sb.Append(";FFMETADATA1\n"); | ||
sb.Append('\n'); | ||
|
||
for (var i = 0; i < chapters.Count; i++) | ||
{ | ||
sb.Append(chapters[i].GetMetadata(i + 1)); | ||
} | ||
|
||
return sb.ToString(); | ||
} | ||
|
||
private static async Task<string> WriteFFMetadata(string ffMetadata) | ||
{ | ||
var file = Path.GetTempFileName(); | ||
await File.WriteAllTextAsync(file, ffMetadata); | ||
return file; | ||
} | ||
|
||
private static async Task WriteMetadataToFile(string inputFile, string outputFile, string metadataFile) | ||
{ | ||
if (inputFile == outputFile) | ||
{ | ||
var extension = Path.GetExtension(inputFile); | ||
var tempFile = Path.ChangeExtension(Path.GetTempFileName(), extension); | ||
await PerformWrite(inputFile, tempFile, metadataFile); | ||
File.Move(tempFile, outputFile, true); | ||
} | ||
else | ||
{ | ||
await PerformWrite(inputFile, outputFile, metadataFile); | ||
} | ||
} | ||
|
||
private static async Task PerformWrite(string inputFile, string outputFile, string metadataFile) | ||
{ | ||
var process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = "ffmpeg", | ||
UseShellExecute = false, | ||
RedirectStandardError = false, | ||
RedirectStandardOutput = false | ||
} | ||
}; | ||
|
||
var arguments = new List<string> | ||
{ | ||
"-hide_banner", | ||
"-v", "error", | ||
"-i", inputFile, | ||
"-i", metadataFile, | ||
"-map_metadata", "1", | ||
"-map_chapters", "1", | ||
"-codec", "copy", | ||
"-y", outputFile | ||
}; | ||
|
||
foreach (var arg in arguments) | ||
{ | ||
process.StartInfo.ArgumentList.Add(arg); | ||
} | ||
|
||
process.Start(); | ||
|
||
await process.WaitForExitAsync(); | ||
|
||
// ReSharper disable once MethodHasAsyncOverload | ||
process.WaitForExit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Globalization; | ||
using LanguageExt; | ||
|
||
namespace MkChap; | ||
|
||
public class Duration : FFprobeBase | ||
{ | ||
public static async Task<Option<TimeSpan>> GetDuration(string? inputFile) | ||
{ | ||
if (string.IsNullOrWhiteSpace(inputFile)) | ||
{ | ||
return Option<TimeSpan>.None; | ||
} | ||
|
||
var output = await GetFFprobeOutput(new List<string> | ||
{ | ||
"-v", "panic", | ||
"-show_entries", "format=duration", | ||
"-of", "default=nw=1:nokey=1", | ||
inputFile | ||
}); | ||
|
||
if (double.TryParse(output, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out var value)) | ||
{ | ||
return TimeSpan.FromSeconds(value); | ||
} | ||
|
||
return Option<TimeSpan>.None; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using CliWrap; | ||
using CliWrap.Buffered; | ||
|
||
namespace MkChap; | ||
|
||
public abstract class FFprobeBase | ||
{ | ||
protected static async Task<string> GetFFprobeOutput(IEnumerable<string> arguments) | ||
{ | ||
var result = await Cli.Wrap("ffprobe") | ||
.WithArguments(arguments) | ||
.WithValidation(CommandResultValidation.None) | ||
.WithStandardErrorPipe(PipeTarget.ToStream(Stream.Null)) | ||
.ExecuteBufferedAsync(); | ||
|
||
return result.StandardOutput; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace MkChap.Models; | ||
|
||
public record AnalysisResult(List<BlackSection> BlackSections, List<Chapter> Chapters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace MkChap.Models; | ||
|
||
public record BlackSection(TimeSpan Start, TimeSpan Finish, State State) | ||
{ | ||
public TimeSpan Duration => Finish - Start; | ||
public TimeSpan Midpoint() => Start + (Finish - Start) / 2.0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace MkChap.Models; | ||
|
||
public record Chapter(TimeSpan Start, TimeSpan Finish) | ||
{ | ||
public string GetMetadata(int num) | ||
{ | ||
var sb = new StringBuilder(); | ||
|
||
sb.Append("[CHAPTER]\n"); | ||
sb.Append("TIMEBASE=1/1000\n"); | ||
sb.Append($"START={Start.TotalMilliseconds.ToString(NumberFormatInfo.InvariantInfo)}\n"); | ||
sb.Append($"END={Finish.TotalMilliseconds.ToString(NumberFormatInfo.InvariantInfo)}\n"); | ||
sb.Append($"title=Chapter {num}\n"); | ||
sb.Append('\n'); | ||
|
||
return sb.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace MkChap.Models; | ||
|
||
public enum State | ||
{ | ||
TooShort, | ||
OutsideOfWindows, | ||
Ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MkChap.Models; | ||
|
||
public record Window(TimeSpan Start, TimeSpan Finish) | ||
{ | ||
public bool Contains(TimeSpan time) => time >= Start && time <= Finish; | ||
} |
Oops, something went wrong.