Skip to content
Snippets Groups Projects
Commit 71ec7b7b authored by Anduin Xue's avatar Anduin Xue
Browse files

Release 0.0.3

parent 63e2e68c
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ namespace Aiursoft.Parser.FFmpeg ...@@ -9,6 +9,7 @@ namespace Aiursoft.Parser.FFmpeg
private readonly ILogger<FFmpegEntry> _logger; private readonly ILogger<FFmpegEntry> _logger;
private readonly FFmpegOptions _options; private readonly FFmpegOptions _options;
private readonly CommandService _commandService; private readonly CommandService _commandService;
private const long MbToBytes = 1024 * 1024;
public FFmpegEntry( public FFmpegEntry(
ILogger<FFmpegEntry> logger, ILogger<FFmpegEntry> logger,
...@@ -34,45 +35,58 @@ namespace Aiursoft.Parser.FFmpeg ...@@ -34,45 +35,58 @@ namespace Aiursoft.Parser.FFmpeg
foreach (var file in videos) foreach (var file in videos)
{ {
_logger.LogTrace("Parsing video file: " + file); _logger.LogTrace("Parsing video file: " + file);
await Parse(file, coder: _options.UseGpu ? "hevc_nvenc" : "libx265"); await this.ProcessVideoAsync(file);
} }
} }
private async Task Parse(string filePath, string coder) private async Task ProcessVideoAsync(string filePath)
{ {
var folder = Path.GetDirectoryName(filePath) ?? throw new Exception($"{filePath} is invalid!"); var folder = Path.GetDirectoryName(filePath) ?? throw new Exception($"{filePath} is invalid!");
var baseFileInfo = await _commandService.RunCommand("ffmpeg", $@"-i ""{filePath}""", folder); var baseFileInfo = await _commandService.RunCommandAsync("ffmpeg", $@"-i ""{filePath}""", folder);
var fileInfo = new FileInfo(filePath); var fileInfo = new FileInfo(filePath);
var shouldParse =
fileInfo.Length > 20 * 1024 * 1024 && // 20MB
( if (ShouldParseVideo(baseFileInfo, fileInfo))
!baseFileInfo.Contains("Video: hevc") || // Not HEVC
!filePath.EndsWith(".mp4") || // Or not MP4
baseFileInfo.Contains("creation_time") // Or contains privacy info (Never parsed before)
);
var bareName = Path.GetFileNameWithoutExtension(filePath);
var newFileName = $"{fileInfo.Directory}{Path.DirectorySeparatorChar}{bareName}_265.mp4";
if (shouldParse)
{ {
_logger.LogWarning($"{filePath} WILL be parsed, with codec: {coder}, crf is {_options.Crf}"); var newFileName = GetNewFileName(fileInfo);
await ParseVideoAsync(filePath, newFileName, folder, coder: _options.UseGpu ? "hevc_nvenc" : "libx265", crf: _options.Crf);
}
else
{
_logger.LogInformation($"{filePath} don't have to be parsed...");
}
}
File.Delete(newFileName); private bool ShouldParseVideo(string baseFileInfo, FileInfo fileInfo)
await _commandService.RunCommand("ffmpeg", $@"-i ""{filePath}"" -codec:a copy -codec:v {coder} -crf {_options.Crf} ""{newFileName}""", folder, getOutput: false); {
return fileInfo.Length > 20 * MbToBytes &&
(!baseFileInfo.Contains("Video: hevc") || !fileInfo.Name.EndsWith(".mp4") || baseFileInfo.Contains("creation_time"));
}
if (File.Exists(newFileName) && new FileInfo(newFileName).Length > 8 * 1024 * 1024) // Larger than 8MB. private string GetNewFileName(FileInfo fileInfo)
{ {
// Delete old file. var bareName = Path.GetFileNameWithoutExtension(fileInfo.FullName);
File.Delete(filePath); return $"{fileInfo.Directory}{Path.DirectorySeparatorChar}{bareName}_265.mp4";
} }
else
{ private async Task ParseVideoAsync(string sourceFilePath, string targetFilePath, string folder, string coder, int crf)
throw new Exception("After parsing, still couldn't locate the converted file: " + newFileName); {
} _logger.LogWarning($"{sourceFilePath} WILL be parsed, with codec: {coder}, crf is {crf}");
if (File.Exists(targetFilePath))
{
File.Delete(targetFilePath);
}
await _commandService.RunCommandAsync("ffmpeg", $@"-i ""{sourceFilePath}"" -preset slower -codec:a copy -codec:v {coder} -crf {crf} ""{targetFilePath}""", folder, getOutput: false);
var targetFileInfo = new FileInfo(targetFilePath);
if (targetFileInfo.Exists && targetFileInfo.Length > 8 * MbToBytes)
{
File.Delete(sourceFilePath);
} }
else else
{ {
_logger.LogInformation($"{filePath} don't have to be parsed..."); throw new Exception("After parsing, still couldn't locate the converted file: " + targetFilePath);
} }
} }
} }
......
...@@ -4,7 +4,7 @@ namespace Aiursoft.Parser.FFmpeg.Services ...@@ -4,7 +4,7 @@ namespace Aiursoft.Parser.FFmpeg.Services
{ {
public class CommandService public class CommandService
{ {
public async Task<string> RunCommand(string bin, string arg, string path, bool getOutput = true) public async Task<string> RunCommandAsync(string bin, string arg, string path, bool getOutput = true)
{ {
var p = new Process var p = new Process
{ {
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<PropertyGroup> <PropertyGroup>
<!--Build and code--> <!--Build and code-->
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<Version>0.0.2</Version> <Version>0.0.3</Version>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<AssemblyName>parser</AssemblyName> <AssemblyName>parser</AssemblyName>
<RootNamespace>Aiursoft.Parser</RootNamespace> <RootNamespace>Aiursoft.Parser</RootNamespace>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment