forked from SubtitleEdit/subtitleedit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pavel-belenko/feature/netflix-glyph-check
Feature / Netflix glyph check
- Loading branch information
Showing
21 changed files
with
720 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck | ||
{ | ||
public interface INetflixQualityChecker | ||
{ | ||
void Check(Subtitle subtitle, NetflixQualityReportBuilder report); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck | ||
{ | ||
public class NetflixGlyphChecker : INetflixQualityChecker | ||
{ | ||
private static int[] LoadNetflixGlyphs() | ||
{ | ||
int[] glyphs; | ||
|
||
using (MemoryStream ms = new MemoryStream(Properties.Resources.NetflixAllowedGlyphs)) | ||
{ | ||
using (BinaryReader br = new BinaryReader(ms)) | ||
{ | ||
const int codepointSize = 4; | ||
long n = ms.Length / codepointSize; | ||
glyphs = new int[n]; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
glyphs[i] = br.ReadInt32(); | ||
} | ||
} | ||
} | ||
|
||
return glyphs; | ||
} | ||
|
||
public void Check(Subtitle subtitle, NetflixQualityReportBuilder report) | ||
{ | ||
// Load allowed glyphs | ||
int[] allowedGlyphsArr = LoadNetflixGlyphs(); | ||
HashSet<int> allowedGlyphsSet = new HashSet<int>(allowedGlyphsArr); | ||
|
||
foreach (Paragraph paragraph in subtitle.Paragraphs) | ||
{ | ||
for (int pos = 0, actualPos = 0; pos < paragraph.Text.Length; | ||
pos += char.IsSurrogatePair(paragraph.Text, pos) ? 2 : 1, actualPos++) | ||
{ | ||
int curCodepoint = char.ConvertToUtf32(paragraph.Text, pos); | ||
|
||
if (!allowedGlyphsSet.Contains(curCodepoint)) | ||
{ | ||
string timecode = paragraph.StartTime.ToHHMMSSFF(); | ||
string context = NetflixQualityReportBuilder.StringContext(paragraph.Text, pos, 6); | ||
string comment = string.Format(Configuration.Settings.Language.NetflixQualityCheck.GlyphCheckReport, | ||
string.Format("U+{0:X}", curCodepoint), actualPos); | ||
|
||
report.AddRecord(timecode, context, comment); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck | ||
{ | ||
public class NetflixQualityReportBuilder | ||
{ | ||
public class Record | ||
{ | ||
public string Timecode { get; set; } | ||
public string Context { get; set; } | ||
public string Comment { get; set; } | ||
|
||
public Record(string timecode, string context, string comment) | ||
{ | ||
Timecode = timecode; | ||
Context = context; | ||
Comment = comment; | ||
} | ||
|
||
public string ToCSVRow() | ||
{ | ||
string safeContext = Context; | ||
safeContext = safeContext.Replace("\"", "\"\""); | ||
safeContext = safeContext.Replace("\r\n", "\n"); | ||
safeContext = safeContext.Replace("\n", "\\n"); | ||
safeContext = string.Format("\"{0}\"", safeContext); | ||
|
||
return string.Format("{0},{1},{2}", Timecode, safeContext, Comment); | ||
} | ||
} | ||
|
||
public List<Record> Records { get; private set; } | ||
|
||
public NetflixQualityReportBuilder() | ||
{ | ||
Records = new List<Record>(); | ||
} | ||
|
||
public void AddRecord(string timecod, string context, string comment) | ||
{ | ||
Records.Add(new Record(timecod, context, comment)); | ||
} | ||
|
||
public string ExportCSV() | ||
{ | ||
StringBuilder csvBuilder = new StringBuilder(); | ||
|
||
// Header | ||
csvBuilder.AppendLine("Timecode,Context,Comment"); | ||
|
||
// Rows | ||
Records.ForEach(r => csvBuilder.AppendLine(r.ToCSVRow())); | ||
|
||
return csvBuilder.ToString(); | ||
} | ||
|
||
public bool IsEmpty { | ||
get | ||
{ | ||
return Records.Count == 0; | ||
} | ||
} | ||
|
||
public static string StringContext(string str, int pos, int radius) | ||
{ | ||
int beginPos = Math.Max(0, pos - radius); | ||
int endPos = Math.Min(str.Length, pos + radius); | ||
int length = endPos - beginPos; | ||
return str.Substring(beginPos, length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Nikse.SubtitleEdit.Core.NetflixQualityCheck | ||
{ | ||
public class NetflixWhiteSpaceChecker : INetflixQualityChecker | ||
{ | ||
public void Check(Subtitle subtitle, NetflixQualityReportBuilder report) | ||
{ | ||
foreach (Paragraph p in subtitle.Paragraphs) | ||
{ | ||
foreach (Match m in Regex.Matches(p.Text, "([ ]{2,}|(\n|\r\n){2,})")) | ||
{ | ||
string timecode = p.StartTime.ToHHMMSSFF(); | ||
string context = NetflixQualityReportBuilder.StringContext(p.Text, m.Index, 6); | ||
string comment = string.Format(Configuration.Settings.Language.NetflixQualityCheck.WhiteSpaceCheckReport, m.Index); | ||
|
||
report.AddRecord(timecode, context, comment); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.