问题描述
23
00:01:32,670 --> 00:01:40,110
It's basically double the amount of work that he used to have. But that's not all because he gets complaints
23
24
00:01:40,110 --> 00:01:46,250
from users saying that "hey in your app some of the layouts look really weird".
24
25
00:01:46,260 --> 00:01:47,670
"It doesn't look right."
25
所以我想删除它怎么办?
注意::我有一点编程经验
解决方法
将“RemoveDoubleNumbering.exe”放在字幕所在的文件夹中并打开它。将处理此文件夹及其所有子文件夹中的字幕。还将创建备份。
需要 .Net Framework 4.5+。
如果有人担心病毒,请自行编译:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace RemoveDoubleNumbering
{
class Program
{
static void Main(string[] args)
{
if (args.Length > 0 && args[0] == "restore")
{
Console.WriteLine("Starting to restore backups");
RestoreSubtitlesFromBackup(GetCurrentDirectory());
Console.WriteLine("Backups restored.");
return;
}
var subtitlesDirectory = GetSubtitlesDirectory(args);
var subtitleFiles = GetSubtitleFiles(subtitlesDirectory);
RemoveDoubleNumbering(subtitleFiles);
}
/// <summary>Gets subtitles directory.</summary>
/// <param name="args">Program arguments.</param>
/// <returns>Current directory or directory from arguments.</returns>
static string GetSubtitlesDirectory(string[] args)
{
if (args.Length == 0)
{
return GetCurrentDirectory();
}
return args[0];
}
/// <summary>Gets current directory of exe.</summary>
/// <returns>Current directory.</returns>
static string GetCurrentDirectory()
{
try
{
return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return null;
}
}
/// <summary>Removes double numbering from subtitle files. Overwrites existing files.</summary>
/// <param name="subtitleFiles">Subtitle files path.</param>
static void RemoveDoubleNumbering(IEnumerable<string> subtitleFiles)
{
foreach (var subtitleFile in subtitleFiles)
{
RemoveDoubleNumbering(subtitleFile);
}
}
/// <summary>Removes double numbering from subtitle file. Overwrites existing file.</summary>
/// <param name="subtitleFile">Subtitle file path.</param>
static void RemoveDoubleNumbering(string subtitleFile)
{
if (!CreateSubtitleBackup(subtitleFile))
{
Console.WriteLine($@"Failed to create backup for ""{subtitleFile}""");
Console.WriteLine($@"The ""{subtitleFile}"" will be skipped");
return;
}
try
{
Console.WriteLine($@"Removing double numbering in ""{subtitleFile}""");
var subtitleText = File.ReadAllText(subtitleFile);
var subtitleLines = subtitleText.Split(new string[] { Environment.NewLine },StringSplitOptions.None);
var startIndex = GetStartIndex(subtitleLines);
var newSubtitleLines = new List<string>();
for (int currentLineIndex = startIndex; currentLineIndex < subtitleLines.Length; currentLineIndex++)
{
if (!IsDoubleNumbering(subtitleLines,startIndex,currentLineIndex))
{
newSubtitleLines.Add(subtitleLines[currentLineIndex]);
}
}
var newSubtitleText = string.Join(Environment.NewLine,newSubtitleLines);
newSubtitleText = newSubtitleText.Trim() + Environment.NewLine + Environment.NewLine;
File.WriteAllText(subtitleFile,newSubtitleText);
Console.WriteLine($@"Deletion of double numbering in ""{subtitleFile}"" completed.");
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
/// <summary>Checks if the current line is the double numbering.</summary>
/// <param name="subtitleLines">Subtitle lines.</param>
/// <param name="startIndex">Start index.</param>
/// <param name="currentLineIndex">Current line index.</param>
/// <returns><see langword="true"/> if the current line is the double numbering; otherwise <see langword="false"/>.</returns>
static bool IsDoubleNumbering(IEnumerable<string> subtitleLines,int startIndex,int currentLineIndex)
{
var arrowLineIndex = currentLineIndex - 2;
try
{
var isDoubleNumbering = arrowLineIndex >= startIndex
&& subtitleLines.ElementAt(currentLineIndex).IsNumber()
&& subtitleLines.ElementAt(arrowLineIndex).Contains("-->");
return isDoubleNumbering;
}
catch (Exception exc)
{
Console.Write(exc.Message);
return false;
}
}
/// <summary>Gets start index.</summary>
/// <param name="subtitleLines">Subtitle lines.</param>
/// <returns>1 if garbage exists in the first line; otherwise 0.</returns>
static int GetStartIndex(IEnumerable<string> subtitleLines)
{
if (subtitleLines.Count() > 0 && subtitleLines.First() != "1")
{
return 1;
}
return 0;
}
/// <summary>Gets .srt subtitle files. Gets files from directory and all its subdirectories.</summary>
/// <param name="directory">Directory where subtitle files are located.</param>
/// <param name="mask">Files mask.</param>
/// <returns>Full paths to subtitle files.</returns>
static IEnumerable<string> GetSubtitleFiles(string directory,string mask = "*.srt")
{
try
{
return Directory.EnumerateFiles(directory,mask,SearchOption.AllDirectories);
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return new string[] { };
}
}
/// <summary>Creates subtitle file backup.</summary>
/// <param name="subtitleFile">subtitle file path.</param>
/// <returns><see langword="true"/> if the backup is successfully created; otherwise <see langword="false"/>.</returns>
static bool CreateSubtitleBackup(string subtitleFile)
{
try
{
var backupName = Path.GetFileName(subtitleFile) + ".backup";
var backupPath = Path.Combine(Path.GetDirectoryName(subtitleFile),backupName);
if (!File.Exists(backupPath))
{
File.Copy(subtitleFile,backupPath);
}
return true;
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return false;
}
}
/// <summary>Restore subtitles from backups.</summary>
/// <param name="directory">Directory with subtitles.</param>
/// <returns><see langword="true"/> if the subtitles is successfully resotred; otherwise <see langword="false"/>.</returns>
static bool RestoreSubtitlesFromBackup(string directory)
{
try
{
var backupFiles = GetSubtitleFiles(directory,"*.srt.backup");
foreach (var backupFile in backupFiles)
{
var originalFileName = Path.GetFileNameWithoutExtension(backupFile);
var originalFile = Path.Combine(Path.GetDirectoryName(backupFile),originalFileName);
if (File.Exists(originalFile))
{
File.Delete(originalFile);
}
File.Move(backupFile,originalFile);
}
return true;
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
return false;
}
}
}
/// <summary>Extends the string class.</summary>
static class StringExtension
{
/// <summary>Checks if a string is actually a number.</summary>
/// <param name="line">The string itself.</param>
/// <returns><see langword="true"/> if the string is a number; otherwise <see langword="false"/>.</returns>
public static bool IsNumber(this string line)
{
int number;
if (int.TryParse(line,out number))
{
return true;
}
return false;
}
}
}