Skip to content

Commit 076d330

Browse files
committed
1. Remove Unnecessary chapter node before adding chapter file
2. Allow restart a task.
1 parent f31cef6 commit 076d330

File tree

12 files changed

+170
-15
lines changed

12 files changed

+170
-15
lines changed

OKEGui/OKEGui/Gui/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<GridViewColumn Header="" Width="30">
1919
<GridViewColumn.CellTemplate>
2020
<DataTemplate>
21-
<CheckBox IsChecked="{Binding Path=IsEnabled}"></CheckBox>
21+
<CheckBox IsChecked="{Binding Path=IsEnabled}" Checked="Checkbox_Changed" Unchecked="Checkbox_Changed"></CheckBox>
2222
</DataTemplate>
2323
</GridViewColumn.CellTemplate>
2424
</GridViewColumn>

OKEGui/OKEGui/Gui/MainWindow.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ public MainWindow()
5050
WorkerNumber.Text = "工作单元:" + WorkerCount.ToString();
5151
}
5252

53+
private void Checkbox_Changed(object sender, RoutedEventArgs e)
54+
{
55+
BtnRun.IsEnabled = tm.HasNextTask();
56+
}
57+
5358
private void BtnNew_Click(object sender, RoutedEventArgs e)
5459
{
5560
// 新建任务。具体实现请见Gui/wizardWindow
5661
try
5762
{
5863
var wizard = new WizardWindow(wm);
5964
wizard.ShowDialog();
60-
BtnRun.IsEnabled = true;
65+
BtnRun.IsEnabled = tm.HasNextTask();
6166
tm.IsCanStart = true;
6267
}
6368
catch (Exception ex)
@@ -133,6 +138,7 @@ private void BtnDelete_Click(object sender, RoutedEventArgs e)
133138
MessageBox.Show("任务删除失败!", "OKEGui", MessageBoxButton.OK, MessageBoxImage.Error);
134139
}
135140

141+
BtnRun.IsEnabled = tm.HasNextTask();
136142
return;
137143
}
138144

@@ -141,6 +147,7 @@ private void BtnEmpty_Click(object sender, RoutedEventArgs e)
141147
if (!tm.IsCanStart)
142148
{
143149
tm.taskStatus.Clear();
150+
BtnRun.IsEnabled = false;
144151
}
145152
}
146153

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using OKEGui.Model;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text;
6+
7+
namespace OKEGui.JobProcessor
8+
{
9+
public class ChapterChecker
10+
{
11+
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
12+
13+
private readonly OKEFile ChapterFile;
14+
private readonly long LengthInMiliSec;
15+
private readonly SortedDictionary<string, string> Chapters;
16+
17+
public ChapterChecker(OKEFile chapterFile, long lengthInMiliSec)
18+
{
19+
ChapterFile = chapterFile;
20+
LengthInMiliSec = lengthInMiliSec;
21+
Chapters = ReadChapters(chapterFile);
22+
}
23+
24+
private static SortedDictionary<string, string> ReadChapters(OKEFile file)
25+
{
26+
SortedDictionary<string, string> chapters = new SortedDictionary<string, string>();
27+
string fileContent = File.ReadAllText(file.GetFullPath());
28+
string[] chapterLines = fileContent.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
29+
for (int i = 0; i < chapterLines.Length / 2; i++)
30+
{
31+
string strTime = chapterLines[i + i].Split(new char[] { '=' })[1];
32+
string name = chapterLines[i + i + 1].Split(new char[] { '=' })[1];
33+
chapters.Add(strTime, name);
34+
}
35+
return chapters;
36+
}
37+
38+
private static long StrToMilisec(string str)
39+
{
40+
string[] hms = str.Split(new char[] { ':' });
41+
if (hms.Length != 3)
42+
{
43+
throw new ArgumentException(str + "无法识别为时间!");
44+
}
45+
long h = long.Parse(hms[0]);
46+
long m = long.Parse(hms[1]);
47+
double s = double.Parse(hms[2]);
48+
return h * 3600000 + m * 60000 + (long)(s * 1000 + 0.5);
49+
}
50+
51+
private void WriteChapter()
52+
{
53+
int idx = 0;
54+
string allText = "";
55+
foreach (KeyValuePair<string, string> chapter in Chapters)
56+
{
57+
idx++;
58+
string strIdx = idx.ToString("D2");
59+
allText += "CHAPTER" + strIdx + "=" + chapter.Key + Environment.NewLine;
60+
allText += "CHAPTER" + strIdx + "NAME=" + chapter.Value + Environment.NewLine;
61+
}
62+
string filePath = ChapterFile.GetFullPath();
63+
File.Move(filePath, Path.ChangeExtension(filePath, ".bak") + ChapterFile.GetExtension());
64+
File.WriteAllText(filePath, allText);
65+
}
66+
67+
public void RemoveUnnecessaryEnd()
68+
{
69+
List<string> toRemove = new List<string>();
70+
foreach (KeyValuePair<string, string> chapter in Chapters)
71+
{
72+
long timeInMiliSec = StrToMilisec(chapter.Key);
73+
if (timeInMiliSec > LengthInMiliSec - 1000)
74+
{
75+
Logger.Info(chapter.Value + ":" + chapter.Key + "的时间在文件结尾1秒内,删除。");
76+
toRemove.Add(chapter.Key);
77+
}
78+
}
79+
80+
if (toRemove.Count > 0)
81+
{
82+
foreach (string key in toRemove)
83+
{
84+
Chapters.Remove(key);
85+
}
86+
WriteChapter();
87+
}
88+
}
89+
90+
public bool IsEmpty()
91+
{
92+
if (Chapters.ContainsKey("00:00:00.000"))
93+
{
94+
return Chapters.Count == 1;
95+
}
96+
else
97+
{
98+
return Chapters.Count == 0;
99+
}
100+
}
101+
}
102+
}

OKEGui/OKEGui/JobProcessor/Demuxer/EACDemuxer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ public MediaFile Extract(Action<double, EACProgressType> progressCallback)
415415
mf.AddTrack(new SubtitleTrack(file, subInfo));
416416
break;
417417
case TrackType.Chapter:
418-
mf.AddTrack(new ChapterTrack(file));
418+
//TODO: 暂时不加入原盘自带的章节,否则无法检测末端多余章节点。
419+
//mf.AddTrack(new ChapterTrack(file));
419420
break;
420421
case TrackType.Video:
421422
mf.AddTrack(new VideoTrack(file, new VideoInfo()));

OKEGui/OKEGui/JobProcessor/Demuxer/TrackInfo.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,15 @@ public bool IsDuplicate(in TrackInfo other)
8484

8585
public void MarkSkipping()
8686
{
87-
File.Move(OutFileName, Path.ChangeExtension(OutFileName, ".bak") + FileExtension);
87+
try
88+
{
89+
File.Move(OutFileName, Path.ChangeExtension(OutFileName, ".bak") + FileExtension);
90+
}
91+
catch (Exception)
92+
{
93+
Logger.Warn("无法备份文件,直接删除。如果是重启的任务,这很正常。");
94+
File.Delete(OutFileName);
95+
}
8896
DupOrEmpty = true;
8997
}
9098
}

OKEGui/OKEGui/JobProcessor/Video/CommandlineVideoEncoder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class CommandlineVideoEncoder : CommandlineJobProcessor
1212
{
1313
#region variables
1414

15-
private ulong numberOfFrames;
15+
public ulong NumberOfFrames { get; protected set; }
1616
private ulong currentFrameNumber;
1717
protected long fps_n = 0, fps_d = 0;
1818

@@ -41,7 +41,7 @@ protected void getInputProperties(VideoJob job)
4141
VSPipeInfo vsHelper = new VSPipeInfo(job.Input, job.VspipeArgs);
4242
fps_n = vsHelper.FpsNum;
4343
fps_d = vsHelper.FpsDen;
44-
numberOfFrames = (ulong)vsHelper.TotalFreams;
44+
NumberOfFrames = (ulong)vsHelper.TotalFreams;
4545
if (fps_n != job.FpsNum || fps_d != job.FpsDen)
4646
{
4747
OKETaskException ex = new OKETaskException(Constants.fpsMismatchSmr);
@@ -128,11 +128,11 @@ protected void Update()
128128
}
129129
else
130130
{
131-
job.TimeRemain = TimeSpan.FromSeconds((double)(numberOfFrames - currentFrameNumber) / speed);
131+
job.TimeRemain = TimeSpan.FromSeconds((double)(NumberOfFrames - currentFrameNumber) / speed);
132132
}
133133

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

137137
if (bitrate == 0)
138138
{
@@ -162,7 +162,7 @@ public static String HumanReadableFilesize(double size, int digit)
162162

163163
protected void encodeFinish(ulong reportedFrames)
164164
{
165-
if (reportedFrames < numberOfFrames)
165+
if (reportedFrames < NumberOfFrames)
166166
{
167167
OKETaskException ex = new OKETaskException(Constants.vsCrashSmr);
168168
throw ex;

OKEGui/OKEGui/OKEGui.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<SubType>Designer</SubType>
9797
</ApplicationDefinition>
9898
<Compile Include="JobProcessor\Audio\FFmpegVolumeChecker.cs" />
99+
<Compile Include="JobProcessor\Chapter\ChapterChecker.cs" />
99100
<Compile Include="JobProcessor\Demuxer\TrackInfo.cs" />
100101
<Compile Include="JobProcessor\ExceptionParser.cs" />
101102
<Compile Include="JobProcessor\Video\X264Encoder.cs" />

OKEGui/OKEGui/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
4848
// 方法是按如下所示使用“*”: :
4949
// [assembly: AssemblyVersion("1.0.*")]
50-
[assembly: AssemblyVersion("4.9.*")]
50+
[assembly: AssemblyVersion("4.10.*")]

OKEGui/OKEGui/Task/TaskDetail.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class TaskDetail : TaskStatus
1616
public Queue<Job> JobQueue = new Queue<Job>();
1717

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

2222
public bool IsRunning;
2323
public string Tid;

OKEGui/OKEGui/Task/TaskManager.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,22 @@ public TaskDetail GetNextTask()
111111

112112
return null;
113113
}
114+
115+
public bool HasNextTask()
116+
{
117+
lock (o)
118+
{
119+
// 找出下一个可用任务
120+
foreach (var task in taskStatus)
121+
{
122+
if (task.IsEnabled)
123+
{
124+
return true;
125+
}
126+
}
127+
}
128+
129+
return false;
130+
}
114131
}
115132
}

0 commit comments

Comments
 (0)