Skip to content

Commit

Permalink
新增自动重命名图像序列;新增Host连续输出FFmpeg进度信息时,将覆盖上一条进度信息(仅当输出内容为一行时)
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed Mar 3, 2023
1 parent f6f4dc9 commit bdd1df2
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 75 deletions.
88 changes: 75 additions & 13 deletions SimpleFFmpegGUI.Core/FileSystemUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SimpleFFmpegGUI.Model;
using FzLib.IO;
using SimpleFFmpegGUI.FFmpegLib;
using SimpleFFmpegGUI.Model;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -11,18 +13,10 @@ public static class FileSystemUtility
{
private const string TempDirKey = "TempDir";

public static string GetTempDir()
{
using var db = FFmpegDbContext.GetNew();
var value = db.Configs.FirstOrDefault(p => p.Key == TempDirKey)?.Value;
if (value == null)
{
string str = Guid.NewGuid().ToString();
value = Path.Combine(Path.GetTempPath(), str);
Directory.CreateDirectory(value);
}
return value;
}
/// <summary>
/// 不合法的文件名字符集合
/// </summary>
private static HashSet<char> invalidFileNameChars = Path.GetInvalidFileNameChars().ToHashSet();

public static string GetSequence(string sampleFilePath)
{
Expand Down Expand Up @@ -61,5 +55,73 @@ public static string GetSequence(string sampleFilePath)
}
return null;
}

public static string GetTempDir()
{
using var db = FFmpegDbContext.GetNew();
var value = db.Configs.FirstOrDefault(p => p.Key == TempDirKey)?.Value;
if (value == null)
{
string str = Guid.NewGuid().ToString();
value = Path.Combine(Path.GetTempPath(), str);
Directory.CreateDirectory(value);
}
return value;
}

/// <summary>
/// 生成输出路径
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static string GenerateOutputPath(TaskInfo task)
{
string output = task.Output.Trim();
var a = task.Arguments;
if (string.IsNullOrEmpty(output))
{
if (task.Inputs.Count == 0)
{
throw new Exception("没有指定输出路径,且输入文件为空");
}
output = task.Inputs[0].FilePath;
}

//删除非法字符
string dir = Path.GetDirectoryName(output);
string filename = Path.GetFileName(output);
if (filename.Any(p => invalidFileNameChars.Contains(p)))
{
foreach (var c in invalidFileNameChars)
{
filename = filename.Replace(c.ToString(), "");
}
output = Path.Combine(dir, filename);
}


//修改扩展名
if (!string.IsNullOrEmpty(a?.Format))
{
VideoFormat format = VideoFormat.Formats.Where(p => p.Name == a.Format || p.Extension == a.Format).FirstOrDefault();
if (format != null)
{
string name = Path.GetFileNameWithoutExtension(output);
string extension = format.Extension;
output = Path.Combine(dir, name + "." + extension);
}
}

//获取非重复文件名
task.RealOutput = FileSystem.GetNoDuplicateFile(output);

//创建目录
if (!new FileInfo(task.RealOutput).Directory.Exists)
{
new FileInfo(task.RealOutput).Directory.Create();
}
return task.RealOutput;
}
}
}
73 changes: 14 additions & 59 deletions SimpleFFmpegGUI.Core/Manager/FFmpegManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Text.RegularExpressions;
using System.Threading;
using Task = System.Threading.Tasks.Task;
using static SimpleFFmpegGUI.FileSystemUtility;

namespace SimpleFFmpegGUI.Manager
{
Expand Down Expand Up @@ -50,10 +51,6 @@ public class FFmpegManager : INotifyPropertyChanged
/// </summary>
private static readonly Regex rVMAF = new Regex(@"VMAF score: [0-9\.]+", RegexOptions.Compiled);

/// <summary>
/// 不合法的文件名字符集合
/// </summary>
private static HashSet<char> invalidFileNameChars = Path.GetInvalidFileNameChars().ToHashSet();

/// <summary>
/// 当前任务
Expand Down Expand Up @@ -135,6 +132,7 @@ public bool Paused
/// FFmpeg进程
/// </summary>
private FFmpegProcess Process { get; set; }

/// <summary>
/// 测试输出参数是否合法
/// </summary>
Expand Down Expand Up @@ -223,7 +221,7 @@ public async Task RunAsync()
string tempDir = FileSystemUtility.GetTempDir();
await (task.Type switch
{
TaskType.Code => RunCodeProcessAsync(tempDir, cancel.Token),
TaskType.Code => RunCodeProcessAsync(cancel.Token),
TaskType.Combine => RunCombineProcessAsync(cancel.Token),
TaskType.Compare => RunCompareProcessAsync(cancel.Token),
TaskType.Custom => RunCustomProcessAsync(cancel.Token),
Expand Down Expand Up @@ -271,58 +269,6 @@ public void Suspend()
ProcessExtension.SuspendProcess(Process.Id);
StatusChanged?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// 生成输出路径
/// </summary>
/// <param name="task"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static string GenerateOutputPath(TaskInfo task)
{
string output = task.Output.Trim();
var a = task.Arguments;
if (string.IsNullOrEmpty(output))
{
if (task.Inputs.Count == 0)
{
throw new Exception("没有指定输出路径,且输入文件为空");
}
output = task.Inputs[0].FilePath;
}

//删除非法字符
string dir = Path.GetDirectoryName(output);
string filename = Path.GetFileName(output);
if (filename.Any(p => invalidFileNameChars.Contains(p)))
{
foreach (var c in invalidFileNameChars)
{
filename = filename.Replace(c.ToString(), "");
}
output = Path.Combine(dir, filename);
}


//修改扩展名
if (!string.IsNullOrEmpty(a?.Format))
{
VideoFormat format = VideoFormat.Formats.Where(p => p.Name == a.Format || p.Extension == a.Format).FirstOrDefault();
if (format != null)
{
string name = Path.GetFileNameWithoutExtension(output);
string extension = format.Extension;
output = Path.Combine(dir, name + "." + extension);
}
}

//获取非重复文件名
task.RealOutput = FileSystem.GetNoDuplicateFile(output);
if (!new FileInfo(task.RealOutput).Directory.Exists)
{
new FileInfo(task.RealOutput).Directory.Create();
}
return task.RealOutput;
}

/// <summary>
/// 获取视频的长度
Expand Down Expand Up @@ -456,14 +402,23 @@ private async Task RunAsync(string arguments, string desc, CancellationToken can
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
private async Task RunCodeProcessAsync(string tempDir, CancellationToken cancellationToken)
private async Task RunCodeProcessAsync(CancellationToken cancellationToken)
{
string message = null;
if (task.Inputs.Count != 1)
{
throw new ArgumentException("普通编码,输入文件必须为1个");
}
//处理图像序列名
if (task.Inputs.Count == 1 && task.Inputs[0].Image2)
{
string seq = GetSequence(task.Inputs[0].FilePath);
if (seq != null)
{
task.Inputs[0].FilePath = seq;
}
}
GenerateOutputPath(task);
string message;
if (task.Arguments.Video == null || !task.Arguments.Video.TwoPass)
{
Progress = GetProgress();
Expand Down
19 changes: 18 additions & 1 deletion SimpleFFmpegGUI.Host/ConsoleLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public static void StartListen()
{
if (instance == null)
{
new ConsoleLogger();
instance = new ConsoleLogger();
}
}

private string lastLogContent = null;
private void Logger_Log(object sender, LogEventArgs e)
{
ConsoleColor defaultColor = Console.ForegroundColor;
Expand All @@ -40,6 +42,12 @@ private void Logger_Log(object sender, LogEventArgs e)
_ => e.Log.Type.ToString().PadLeft(2)
};
string time = e.Log.Time.ToString("yyyy-MM-dd HH:mm:ss");

if (lastLogContent != null && lastLogContent.StartsWith("frame=") && e.Log.Message.StartsWith("frame="))
{
ClearLine();
}

Console.Write(time);
Console.Write(" \t");
Console.ForegroundColor = color;
Expand All @@ -48,6 +56,15 @@ private void Logger_Log(object sender, LogEventArgs e)
Console.Write(" \t");
Console.Write(e.Log.Message);
Console.WriteLine();


lastLogContent = e.Log.Message;
}
static void ClearLine()
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
}
}
56 changes: 55 additions & 1 deletion SimpleFFmpegGUI.Web/src/components/StatusBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@
<el-col :span="16">
<el-progress
:text-inside="true"
class="unknown-progress"
:stroke-width="20"
color="transparent"
:percentage="100"
v-show="status.progress.isIndeterminate"
style="margin-right: 24px; margin-top: 4px"
:show-text="true"
text-color="black"
:format="(p) => '进度未知'"
define-back-color="#CCCA"
></el-progress>
<el-progress
:text-inside="true"
v-show="status.progress.isIndeterminate == false"
:stroke-width="20"
:color="progressColor"
style="margin-right: 24px; margin-top: 4px"
Expand Down Expand Up @@ -96,11 +110,25 @@
</el-row>
<el-row>
<el-col :span="20">
<el-progress
:text-inside="true"
class="unknown-progress"
:stroke-width="20"
color="transparent"
:percentage="100"
style="margin-right: 24px; margin-top: 4px"
:show-text="true"
v-show="status.progress.isIndeterminate"
text-color="black"
:format="(p) => '进度未知'"
define-back-color="#CCCA"
></el-progress>
<el-progress
:text-inside="true"
:stroke-width="20"
:color="progressColor"
style="margin-top: 10px"
v-show="status.progress.isIndeterminate == false"
:percentage="status.progress.percent * 100"
:format="(p) => p.toFixed(2) + '%'"
text-color="white"
Expand Down Expand Up @@ -163,7 +191,9 @@ import {
} from "../common";
export default Vue.component("status-bar", {
data() {
return {};
return {
walkingProgress: 0,
};
},
props: ["status", "windowWidth", "isPaused"],
computed: {
Expand Down Expand Up @@ -193,6 +223,26 @@ export default Vue.component("status-bar", {
components: {},
mounted: function () {
this.$nextTick(function () {
let delay = 0;
// setInterval(() => {
// if (this.status.isPaused) {
// //this.walkingProgress=0;
// //return;
// }
// console.log(this.walkingProgress);
// if (delay > 0) {
// delay--;
// if (delay == 0) {
// this.walkingProgress = 0;
// }
// return;
// }
// if (this.walkingProgress >= 100) {
// delay = 10;
// } else {
// this.walkingProgress++;
// }
// }, 50);
return;
});
},
Expand All @@ -211,6 +261,10 @@ export default Vue.component("status-bar", {
</style>

<style>
.unknown-progress > div > div > div {
text-align: center;
}
/* .el-progress__text {
font-size: 14px !important;
}
Expand Down
8 changes: 7 additions & 1 deletion 日志.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,10 @@

## 20230302

【Core】【WPF】新增根据输入文件猜测是否存在图像序列,并自动重命名
【Core】【WPF】新增根据输入文件猜测是否存在图像序列,并自动重命名

## 20230303

【Core】【Web】新增自动重命名图像序列

【Host】新增连续输出FFmpeg进度信息时,将覆盖上一条进度信息(仅当输出内容为一行时)

0 comments on commit bdd1df2

Please sign in to comment.