Skip to content

Commit 3eeeb0a

Browse files
author
Jin Jae-yeon
committed
Add SRT Detector
1 parent f93549b commit 3eeeb0a

File tree

6 files changed

+2100
-7
lines changed

6 files changed

+2100
-7
lines changed

Daramee.FileTypeDetector/AbstractRegexSignatureDetector.cs

+20-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@ public abstract class AbstractRegexSignatureDetector : IDetector
1717

1818
public virtual bool Detect ( Stream stream )
1919
{
20-
int readBufferSize = Signature.ToString ().Length * 4;
20+
int readBufferSize = Signature.ToString ().Length * 64;
21+
char [] buffer = new char [ readBufferSize ];
2122

22-
var encodings = new [] { Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode };
23+
var encodings = new []
24+
{
25+
Encoding.GetEncoding ( "ascii" ),
26+
Encoding.GetEncoding ( "ks_c_5601-1987" ),
27+
Encoding.GetEncoding ( "iso-2022-kr" ),
28+
Encoding.GetEncoding ( "shift_jis" ),
29+
Encoding.GetEncoding ( "csISO2022JP" ),
30+
Encoding.GetEncoding ( "windows-1250" ),
31+
Encoding.GetEncoding ( "windows-1251" ),
32+
Encoding.GetEncoding ( "windows-1252" ),
33+
Encoding.GetEncoding ( "windows-1253" ),
34+
Encoding.GetEncoding ( "windows-1254" ),
35+
Encoding.UTF8,
36+
Encoding.GetEncoding ( "utf-7" ),
37+
Encoding.GetEncoding ( "utf-32" ),
38+
Encoding.Unicode,
39+
Encoding.BigEndianUnicode
40+
};
2341
foreach ( var encoding in encodings )
2442
{
2543
stream.Position = 0;
2644
using ( StreamReader reader = new StreamReader ( stream, encoding, true, readBufferSize, true ) )
2745
{
28-
char [] buffer = new char [ readBufferSize ];
2946
reader.ReadBlock ( buffer, 0, readBufferSize );
3047
if ( Signature.IsMatch ( new string ( buffer ) ) )
3148
return true;

Daramee.FileTypeDetector/Daramee.FileTypeDetector.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="Detectors\RichTextFormatDetector.cs" />
100100
<Compile Include="Detectors\ShockwaveFlashDetector.cs" />
101101
<Compile Include="Detectors\SQLiteDetector.cs" />
102+
<Compile Include="Detectors\SRTDetector.cs" />
102103
<Compile Include="Detectors\UStarFormatTarDetector.cs" />
103104
<Compile Include="Detectors\TextDetector.cs" />
104105
<Compile Include="Detectors\ThumbsDBDetector.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Text.RegularExpressions;
6+
using System.Threading.Tasks;
7+
8+
namespace Daramee.FileTypeDetector.Detectors
9+
{
10+
class SRTDetector : AbstractRegexSignatureDetector
11+
{
12+
public override string Precondition => "txt";
13+
public override string Extension => "srt";
14+
15+
protected override Regex Signature => new Regex ( "\\d+\r?\n(\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d) --> (\\d\\d:\\d\\d:\\d\\d,\\d\\d\\d)\r?\n(.*)\r?\n\r?\n" );
16+
17+
public override string ToString () => "SubRip Subtitle Detector";
18+
}
19+
}

Daramee.FileTypeDetector/Detectors/TextDetector.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public bool Detect ( Stream stream )
8282

8383
foreach ( var encoding in encodings )
8484
{
85-
for ( int count = readed; count >= ( readed - 4 ); --count )
85+
for ( int count = readed; count >= ( readed - 16 ); --count )
8686
{
8787
bool succeed = true;
8888

@@ -100,9 +100,9 @@ public bool Detect ( Stream stream )
100100

101101
int byted = encoding.GetBytes ( TextBuffer, 0, texted, EncodingBuffer, 0 );
102102

103-
if ( succeed && readed == byted )
103+
if ( succeed/* && readed == byted*/ )
104104
{
105-
for ( int i = 0; i < readed; ++i )
105+
for ( int i = 0; i < count; ++i )
106106
{
107107
if ( ReadBuffer [ i ] != EncodingBuffer [ i ] )
108108
{

0 commit comments

Comments
 (0)