forked from jarrettv/ChapterGrabber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapterInfo.cs
252 lines (225 loc) · 9.94 KB
/
ChapterInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Reflection;
using System.Xml;
namespace JarrettVance.ChapterTools
{
public class ChapterInfo
{
public string Title { get; set; }
public int? ChapterSetId { get; set; }
public String ImdbId { get; set; }
public int? MovieDbId { get; set; }
public string Extractor { get; set; }
public string LangCode { get; set; }
public string VolumeName { get; set; }
public string SourceName { get; set; }
public string SourceType { get; set; }
public string SourceHash { get; set; }
public double FramesPerSecond { get; set; }
public TimeSpan Duration { get; set; }
public int Confirmations { get; set; }
public List<ChapterEntry> Chapters { get; set; }
public ChapterInfo() { }
public bool HasNames()
{
if (this.Chapters.Count == 0) return false;
string name = this.Chapters[0].Name ?? string.Empty;
// assume either blank or "Chapter 1"
var charCount = this.Chapters
.Select(x => x.Name ?? string.Empty)
.Select(x => RemoveStandardChapterNameText(x))
.Sum(x => x.Length);
// on average each chapter has 2 characters
return charCount > this.Chapters.Count * 2;
}
public override string ToString()
{
return string.Format("{0}, {1}, {2} chapter(s)", SourceName, Duration.ToShortString(), Chapters.Count);
}
public void ChangeFps(double fps)
{
for (int i = 0; i < Chapters.Count; i++)
{
ChapterEntry c = Chapters[i];
double frames = c.Time.TotalSeconds * FramesPerSecond;
Chapters[i] = new ChapterEntry() { Name = c.Name, Time = new TimeSpan((long)Math.Round(frames / fps * TimeSpan.TicksPerSecond)) };
}
double totalFrames = Duration.TotalSeconds * FramesPerSecond;
Duration = new TimeSpan((long)Math.Round((totalFrames / fps) * TimeSpan.TicksPerSecond));
FramesPerSecond = fps;
}
public void SaveText(string filename)
{
List<string> lines = new List<string>();
int i = 0;
foreach (ChapterEntry c in Chapters)
{
i++;
lines.Add("CHAPTER" + i.ToString("00") + "=" + c.Time.ToShortString());
lines.Add("CHAPTER" + i.ToString("00") + "NAME=" + c.Name);
}
File.WriteAllLines(filename, lines.ToArray());
}
public void SaveQpfile(string filename)
{
List<string> lines = new List<string>();
foreach (ChapterEntry c in Chapters)
{
lines.Add(string.Format("{0} I -1", (long)Math.Round(c.Time.TotalSeconds * FramesPerSecond)));
}
File.WriteAllLines(filename, lines.ToArray());
}
public void SaveCelltimes(string filename)
{
List<string> lines = new List<string>();
foreach (ChapterEntry c in Chapters)
{
lines.Add(((long)Math.Round(c.Time.TotalSeconds * FramesPerSecond)).ToString());
}
File.WriteAllLines(filename, lines.ToArray());
}
public void SaveTsmuxerMeta(string filename)
{
string text = "--custom-" + Environment.NewLine + "chapters=";
foreach (ChapterEntry c in Chapters)
{
text += c.Time.ToShortString() + ";";
}
text = text.Substring(0, text.Length - 1);
File.WriteAllText(filename, text);
}
public void SaveTimecodes(string filename)
{
List<string> lines = new List<string>();
foreach (ChapterEntry c in Chapters)
{
lines.Add(c.Time.ToShortString());
}
File.WriteAllLines(filename, lines.ToArray());
}
public static readonly XNamespace CgNs = "http://jvance.com/2008/ChapterGrabber";
public static ChapterInfo Load(string filename)
{
XDocument doc = XDocument.Load(filename);
return ChapterInfo.Load(doc.Root);
}
public static ChapterInfo Load(XElement root)
{
ChapterInfo ci = new ChapterInfo();
ci.LangCode = (string)root.Attribute(XNamespace.Xml + "lang");
ci.Extractor = (string)root.Attribute("extractor");
if (root.Element(CgNs + "title") != null)
ci.Title = (string)root.Element(CgNs + "title");
XElement @ref = root.Element(CgNs + "ref");
if (@ref != null)
{
ci.ChapterSetId = (int?)@ref.Element(CgNs + "chapterSetId");
ci.ImdbId = (string)@ref.Element(CgNs + "imdbId");
ci.MovieDbId = (int?)@ref.Element(CgNs + "movieDbId");
}
if (root.Attribute("confirmations") != null)
ci.Confirmations = (int)root.Attribute("confirmations");
XElement src = root.Element(CgNs + "source");
if (src != null)
{
ci.SourceName = (string)src.Element(CgNs + "name");
if (src.Element(CgNs + "type") != null)
ci.SourceType = (string)src.Element(CgNs + "type");
ci.SourceHash = (string)src.Element(CgNs + "hash");
ci.FramesPerSecond = Convert.ToDouble(src.Element(CgNs + "fps").Value, new System.Globalization.NumberFormatInfo());
ci.Duration = TimeSpan.Parse(src.Element(CgNs + "duration").Value);
if (src.Element(CgNs + "volume") != null)
ci.VolumeName = (string)src.Element(CgNs + "volume");
}
ci.Chapters = root.Element(CgNs + "chapters").Elements(CgNs + "chapter")
.Select(e => new ChapterEntry() { Name = (string)e.Attribute("name"), Time = TimeSpan.Parse((string)e.Attribute("time")) }).ToList();
return ci;
}
public void Save(string filename)
{
ToXElement().Save(filename);
}
public XElement ToXElement()
{
var reference = new XElement(CgNs + "ref");
if (ChapterSetId.HasValue) reference.Add(new XElement(CgNs + "chapterSetId", ChapterSetId));
if (MovieDbId.HasValue) reference.Add(new XElement(CgNs + "movieDbId", MovieDbId));
if (ImdbId != null) reference.Add(new XElement(CgNs + "imdbId", ImdbId));
return new XElement(new XElement(CgNs + "chapterInfo",
new XAttribute(XNamespace.Xml + "lang", LangCode),
new XAttribute("version", "3"),
Extractor != null ? new XAttribute("extractor", Extractor) : null,
Title != null ? new XElement(CgNs + "title", Title) : null,
reference.Elements().Count() > 0 ? reference : null,
new XElement(CgNs + "source",
new XElement(CgNs + "name", SourceName),
SourceType != null ? new XElement(CgNs + "type", SourceType) : null,
VolumeName != null ? new XElement(CgNs + "volume", VolumeName) : null,
new XElement(CgNs + "hash", SourceHash),
new XElement(CgNs + "fps", FramesPerSecond),
new XElement(CgNs + "duration", Duration.ToString())),
new XElement(CgNs + "chapters",
Chapters.Select(c =>
new XElement(CgNs + "chapter",
new XAttribute("time", c.Time.ToString()),
new XAttribute("name", c.Name))))));
}
public void SaveXml(string filename)
{
new XDocument(new XElement("Chapters",
new XElement("EditionEntry",
new XElement("EditionFlagHidden", "0"),
new XElement("EditionFlagDefault", "0"),
//new XElement("EditionUID", "1"),
Chapters.Select(c =>
new XElement("ChapterAtom",
new XElement("ChapterDisplay",
new XElement("ChapterString", c.Name),
new XElement("ChapterLanguage", LangCode == null ? "und" : LangCode)),
new XElement("ChapterTimeStart", c.Time.ToString()),
new XElement("ChapterFlagHidden", "0"),
new XElement("ChapterFlagEnabled", "1")))
))).Save(filename);
// <Chapters>
//<EditionEntry>
// <EditionFlagHidden>0</EditionFlagHidden>
// <EditionFlagDefault>0</EditionFlagDefault>
// <EditionUID>62811788</EditionUID>
// <ChapterAtom>
// <ChapterDisplay>
// <ChapterString>Test1</ChapterString>
// <ChapterLanguage>und</ChapterLanguage>
// </ChapterDisplay>
// <ChapterUID>2401693056</ChapterUID>
// <ChapterTimeStart>00:01:40.000000000</ChapterTimeStart>
// <ChapterFlagHidden>0</ChapterFlagHidden>
// <ChapterFlagEnabled>1</ChapterFlagEnabled>
// </ChapterAtom>
}
private static string RemoveStandardChapterNameText(string str)
{
return str
.ToLowerInvariant()
.Replace("chapter", "")
.Replace("scene", "")
.Replace("kapitel", "")
.Replace("capítulo", "")
.Replace("capitulo", "")
.Replace("chapitre", "")
.Replace("глава", "")
.Replace("章", "")
.Replace("kapitola", "")
.Replace("hoofdstuk", "")
.Replace("array", "")
.Replace("{empty chapter}", "")
.Trim()
.RemoveSpecialCharacters()
.RemoveNumbers();
}
}
}