Created
February 14, 2017 16:06
-
-
Save itshaadi/8dc47acaf49fa847ffd861806e1c151c to your computer and use it in GitHub Desktop.
fixing weird subtitles with c sharp.
This file contains hidden or 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
using System; | |
using System.Text; | |
using System.Collections.Specialized; | |
using System.IO; | |
using Ude; | |
public static void ConvertFileEncoding(String sourcePath, String destPath, Encoding sourceEncoding, Encoding destEncoding) | |
{ | |
// If the destination’s parent doesn’t exist, create it. | |
String parent = Path.GetDirectoryName(Path.GetFullPath(destPath)); | |
if (!Directory.Exists(parent)) | |
{ | |
Directory.CreateDirectory(parent); | |
} | |
// If the source and destination encodings are the same, just copy the file. | |
if (sourceEncoding == destEncoding) | |
{ | |
File.Copy(sourcePath, destPath, true); | |
return; | |
} | |
// Convert the file. | |
String tempName = null; | |
try | |
{ | |
tempName = Path.GetTempFileName(); | |
using (StreamReader sr = new StreamReader(sourcePath, sourceEncoding, false)) | |
{ | |
using (StreamWriter sw = new StreamWriter(tempName, false, destEncoding)) | |
{ | |
int charsRead; | |
char[] buffer = new char[128 * 1024]; | |
while ((charsRead = sr.ReadBlock(buffer, 0, buffer.Length)) > 0) | |
{ | |
sw.Write(buffer, 0, charsRead); | |
} | |
} | |
} | |
File.Delete(destPath); | |
File.Move(tempName, destPath); | |
} | |
finally | |
{ | |
File.Delete(tempName); | |
} | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
this.AllowDrop = true; | |
this.DragEnter += new DragEventHandler(Form_DragEnter); | |
this.DragDrop += new DragEventHandler(Form_DragDrop); | |
} | |
// This event occurs when the user drags over the form with | |
// the mouse during a drag drop operation | |
void Form_DragEnter(object sender, DragEventArgs e) | |
{ | |
// Check if the Dataformat of the data can be accepted | |
// (we only accept file drops from Explorer, etc.) | |
if (e.Data.GetDataPresent(DataFormats.FileDrop)) | |
e.Effect = DragDropEffects.Copy; // Okay | |
else | |
e.Effect = DragDropEffects.None; // Unknown data, ignore it | |
} | |
void Form_DragDrop(object sender, DragEventArgs e){ | |
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); | |
string[] VideoFormats = { "AVI", "ASF", "WMV", "AVS", "FLV", "MKV", "MOV", "3GP", "MP4", "MPG", "MPEG", "DAT", "OGM", "VOB", "RM", "RMVB", "TS", "TP", "IFO", "NSV" }; | |
int c = FileList.Length; | |
string VideoName = null; | |
string VideoPath = null; | |
string SubtitlePath = null; | |
var charset = ""; | |
foreach (string ext in VideoFormats) | |
{ | |
if (Path.GetExtension(FileList[0]).ToUpper().Replace(".", "") == ext) | |
{ | |
VideoName = Path.GetFileNameWithoutExtension(FileList[0]); | |
VideoPath = FileList[0]; | |
SubtitlePath = FileList[1]; | |
break; | |
} | |
else | |
{ | |
VideoName = Path.GetFileNameWithoutExtension(FileList[1]); | |
VideoPath = FileList[1]; | |
SubtitlePath = FileList[0]; | |
} | |
} | |
FileStream fs = File.OpenRead(SubtitlePath) | |
Ude.CharsetDetector cdet = new Ude.CharsetDetector(); | |
cdet.Feed(fs); | |
cdet.DataEnd(); | |
charset = cdet.Charset; | |
fs.Close(); | |
if (charset != null) | |
{ | |
if (charset.Contains("cyrillic")) | |
{ | |
charset = "windows-1256"; | |
} | |
ConvertFileEncoding(SubtitlePath, SubtitlePath+".tmp", Encoding.GetEncoding(charset), Encoding.UTF8); | |
File.Delete(SubtitlePath); | |
File.Move(SubtitlePath+".tmp", SubtitlePath.Replace(Path.GetFileName(SubtitlePath), VideoName + ".srt")); | |
} | |
else | |
{ | |
MessageBox.Show("Status: Unable to detect subtitle encoding"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment