Skip to content

Commit

Permalink
1. Remove Unnecessary chapter node before adding chapter file
Browse files Browse the repository at this point in the history
2. Allow restart a task.
  • Loading branch information
LittlePox committed Sep 29, 2019
1 parent f31cef6 commit 076d330
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 15 deletions.
2 changes: 1 addition & 1 deletion OKEGui/OKEGui/Gui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<GridViewColumn Header="" Width="30">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsEnabled}"></CheckBox>
<CheckBox IsChecked="{Binding Path=IsEnabled}" Checked="Checkbox_Changed" Unchecked="Checkbox_Changed"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Expand Down
9 changes: 8 additions & 1 deletion OKEGui/OKEGui/Gui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ public MainWindow()
WorkerNumber.Text = "工作单元:" + WorkerCount.ToString();
}

private void Checkbox_Changed(object sender, RoutedEventArgs e)
{
BtnRun.IsEnabled = tm.HasNextTask();
}

private void BtnNew_Click(object sender, RoutedEventArgs e)
{
// 新建任务。具体实现请见Gui/wizardWindow
try
{
var wizard = new WizardWindow(wm);
wizard.ShowDialog();
BtnRun.IsEnabled = true;
BtnRun.IsEnabled = tm.HasNextTask();
tm.IsCanStart = true;
}
catch (Exception ex)
Expand Down Expand Up @@ -133,6 +138,7 @@ private void BtnDelete_Click(object sender, RoutedEventArgs e)
MessageBox.Show("任务删除失败!", "OKEGui", MessageBoxButton.OK, MessageBoxImage.Error);
}

BtnRun.IsEnabled = tm.HasNextTask();
return;
}

Expand All @@ -141,6 +147,7 @@ private void BtnEmpty_Click(object sender, RoutedEventArgs e)
if (!tm.IsCanStart)
{
tm.taskStatus.Clear();
BtnRun.IsEnabled = false;
}
}

Expand Down
102 changes: 102 additions & 0 deletions OKEGui/OKEGui/JobProcessor/Chapter/ChapterChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using OKEGui.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace OKEGui.JobProcessor
{
public class ChapterChecker
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

private readonly OKEFile ChapterFile;
private readonly long LengthInMiliSec;
private readonly SortedDictionary<string, string> Chapters;

public ChapterChecker(OKEFile chapterFile, long lengthInMiliSec)
{
ChapterFile = chapterFile;
LengthInMiliSec = lengthInMiliSec;
Chapters = ReadChapters(chapterFile);
}

private static SortedDictionary<string, string> ReadChapters(OKEFile file)
{
SortedDictionary<string, string> chapters = new SortedDictionary<string, string>();
string fileContent = File.ReadAllText(file.GetFullPath());
string[] chapterLines = fileContent.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (int i = 0; i < chapterLines.Length / 2; i++)
{
string strTime = chapterLines[i + i].Split(new char[] { '=' })[1];
string name = chapterLines[i + i + 1].Split(new char[] { '=' })[1];
chapters.Add(strTime, name);
}
return chapters;
}

private static long StrToMilisec(string str)
{
string[] hms = str.Split(new char[] { ':' });
if (hms.Length != 3)
{
throw new ArgumentException(str + "无法识别为时间!");
}
long h = long.Parse(hms[0]);
long m = long.Parse(hms[1]);
double s = double.Parse(hms[2]);
return h * 3600000 + m * 60000 + (long)(s * 1000 + 0.5);
}

private void WriteChapter()
{
int idx = 0;
string allText = "";
foreach (KeyValuePair<string, string> chapter in Chapters)
{
idx++;
string strIdx = idx.ToString("D2");
allText += "CHAPTER" + strIdx + "=" + chapter.Key + Environment.NewLine;
allText += "CHAPTER" + strIdx + "NAME=" + chapter.Value + Environment.NewLine;
}
string filePath = ChapterFile.GetFullPath();
File.Move(filePath, Path.ChangeExtension(filePath, ".bak") + ChapterFile.GetExtension());
File.WriteAllText(filePath, allText);
}

public void RemoveUnnecessaryEnd()
{
List<string> toRemove = new List<string>();
foreach (KeyValuePair<string, string> chapter in Chapters)
{
long timeInMiliSec = StrToMilisec(chapter.Key);
if (timeInMiliSec > LengthInMiliSec - 1000)
{
Logger.Info(chapter.Value + ":" + chapter.Key + "的时间在文件结尾1秒内,删除。");
toRemove.Add(chapter.Key);
}
}

if (toRemove.Count > 0)
{
foreach (string key in toRemove)
{
Chapters.Remove(key);
}
WriteChapter();
}
}

public bool IsEmpty()
{
if (Chapters.ContainsKey("00:00:00.000"))
{
return Chapters.Count == 1;
}
else
{
return Chapters.Count == 0;
}
}
}
}
3 changes: 2 additions & 1 deletion OKEGui/OKEGui/JobProcessor/Demuxer/EACDemuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ public MediaFile Extract(Action<double, EACProgressType> progressCallback)
mf.AddTrack(new SubtitleTrack(file, subInfo));
break;
case TrackType.Chapter:
mf.AddTrack(new ChapterTrack(file));
//TODO: 暂时不加入原盘自带的章节,否则无法检测末端多余章节点。
//mf.AddTrack(new ChapterTrack(file));
break;
case TrackType.Video:
mf.AddTrack(new VideoTrack(file, new VideoInfo()));
Expand Down
10 changes: 9 additions & 1 deletion OKEGui/OKEGui/JobProcessor/Demuxer/TrackInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ public bool IsDuplicate(in TrackInfo other)

public void MarkSkipping()
{
File.Move(OutFileName, Path.ChangeExtension(OutFileName, ".bak") + FileExtension);
try
{
File.Move(OutFileName, Path.ChangeExtension(OutFileName, ".bak") + FileExtension);
}
catch (Exception)
{
Logger.Warn("无法备份文件,直接删除。如果是重启的任务,这很正常。");
File.Delete(OutFileName);
}
DupOrEmpty = true;
}
}
Expand Down
10 changes: 5 additions & 5 deletions OKEGui/OKEGui/JobProcessor/Video/CommandlineVideoEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class CommandlineVideoEncoder : CommandlineJobProcessor
{
#region variables

private ulong numberOfFrames;
public ulong NumberOfFrames { get; protected set; }
private ulong currentFrameNumber;
protected long fps_n = 0, fps_d = 0;

Expand Down Expand Up @@ -41,7 +41,7 @@ protected void getInputProperties(VideoJob job)
VSPipeInfo vsHelper = new VSPipeInfo(job.Input, job.VspipeArgs);
fps_n = vsHelper.FpsNum;
fps_d = vsHelper.FpsDen;
numberOfFrames = (ulong)vsHelper.TotalFreams;
NumberOfFrames = (ulong)vsHelper.TotalFreams;
if (fps_n != job.FpsNum || fps_d != job.FpsDen)
{
OKETaskException ex = new OKETaskException(Constants.fpsMismatchSmr);
Expand Down Expand Up @@ -128,11 +128,11 @@ protected void Update()
}
else
{
job.TimeRemain = TimeSpan.FromSeconds((double)(numberOfFrames - currentFrameNumber) / speed);
job.TimeRemain = TimeSpan.FromSeconds((double)(NumberOfFrames - currentFrameNumber) / speed);
}

job.Speed = speed.ToString("0.00") + " fps";
job.Progress = (double)currentFrameNumber / (double)numberOfFrames * 100;
job.Progress = (double)currentFrameNumber / (double)NumberOfFrames * 100;

if (bitrate == 0)
{
Expand Down Expand Up @@ -162,7 +162,7 @@ public static String HumanReadableFilesize(double size, int digit)

protected void encodeFinish(ulong reportedFrames)
{
if (reportedFrames < numberOfFrames)
if (reportedFrames < NumberOfFrames)
{
OKETaskException ex = new OKETaskException(Constants.vsCrashSmr);
throw ex;
Expand Down
1 change: 1 addition & 0 deletions OKEGui/OKEGui/OKEGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="JobProcessor\Audio\FFmpegVolumeChecker.cs" />
<Compile Include="JobProcessor\Chapter\ChapterChecker.cs" />
<Compile Include="JobProcessor\Demuxer\TrackInfo.cs" />
<Compile Include="JobProcessor\ExceptionParser.cs" />
<Compile Include="JobProcessor\Video\X264Encoder.cs" />
Expand Down
2 changes: 1 addition & 1 deletion OKEGui/OKEGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.9.*")]
[assembly: AssemblyVersion("4.10.*")]
4 changes: 2 additions & 2 deletions OKEGui/OKEGui/Task/TaskDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class TaskDetail : TaskStatus
public Queue<Job> JobQueue = new Queue<Job>();

// 输出文件轨道。MediaOutFile是主文件(mp4/mkv), MkaOutFile是外挂mka
public MediaFile MediaOutFile = new MediaFile();
public MediaFile MkaOutFile = new MediaFile();
public MediaFile MediaOutFile;
public MediaFile MkaOutFile;

public bool IsRunning;
public string Tid;
Expand Down
17 changes: 17 additions & 0 deletions OKEGui/OKEGui/Task/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,22 @@ public TaskDetail GetNextTask()

return null;
}

public bool HasNextTask()
{
lock (o)
{
// 找出下一个可用任务
foreach (var task in taskStatus)
{
if (task.IsEnabled)
{
return true;
}
}
}

return false;
}
}
}
21 changes: 19 additions & 2 deletions OKEGui/OKEGui/Worker/ExecuteTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
task.WorkerName = args.Name;
task.IsEnabled = false;
task.IsRunning = true;
task.MediaOutFile = new MediaFile();
task.MkaOutFile = new MediaFile();

// 抽取音轨
FileInfo eacInfo = new FileInfo(".\\tools\\eac3to\\eac3to.exe");
Expand Down Expand Up @@ -155,6 +157,8 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
}
}

long lengthInMiliSec = 0;

while (task.JobQueue.Count != 0)
{
Job job = task.JobQueue.Dequeue();
Expand Down Expand Up @@ -212,6 +216,9 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
{
processor = new X264Encoder(videoJob);
}

lengthInMiliSec = (long)(processor.NumberOfFrames / videoJob.Fps * 1000 + 0.5);

task.CurrentStatus = "压制中";
task.ProgressValue = 0.0;
processor.start();
Expand All @@ -231,9 +238,19 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
FileInfo txtChapter = new FileInfo(Path.ChangeExtension(task.InputFile, ".txt"));
if (txtChapter.Exists)
{
task.MediaOutFile.AddTrack(new ChapterTrack(new OKEFile(txtChapter)));
}
OKEFile chapterFile = new OKEFile(txtChapter);
ChapterChecker checker = new ChapterChecker(chapterFile, lengthInMiliSec);
checker.RemoveUnnecessaryEnd();

if (checker.IsEmpty())
{
Logger.Info(txtChapter.Name + "为空,跳过封装。");
}
else
{
task.MediaOutFile.AddTrack(new ChapterTrack(chapterFile));
}
}

// 封装
if (profile.ContainerFormat != "")
Expand Down
4 changes: 3 additions & 1 deletion OKEGui/OKEGui/demo.vpy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ a="00000.m2ts"
src8 = core.lsmas.LWLibavSource(a)
src16 = core.fmtc.bitdepth(src8,bits=16)

res = src16
op = core.rgvs.RemoveGrain(src16, 20)

res = core.std.Trim(src16, 0, int(op_start) - 1) + core.std.Trim(op, int(op_start), int(op_end)) + core.std.Trim(src16, int(op_end) + 1, src16.num_frames - 1)

#OKE:DEBUG
Debug = 1
Expand Down

0 comments on commit 076d330

Please sign in to comment.