Skip to content

Instantly share code, notes, and snippets.

@virgium03
Last active November 18, 2015 21:24
Show Gist options
  • Save virgium03/3854013 to your computer and use it in GitHub Desktop.
Save virgium03/3854013 to your computer and use it in GitHub Desktop.
Replace non ASCII characters from Romanian subtitle files with their counterparts
# To allow the execution of unsigned local scripts on the local machine
# Set-ExecutionPolicy -scope LocalMachine -ExecutionPolicy RemoteSigned
# The actual script
$PSVersionTable.PSVersion
$currDir = Split-Path $args[0] -Parent
Write-Host "--- Current folder is: " $currDir
$files = Get-ChildItem -LiteralPath $currDir
$subtitleFiles = $files | where {$_.Extension -match "srt|sub"}
foreach ($subtitleFile in $subtitleFiles) {
$fullSubtitlePath = $currDir + '\' + $subtitleFile
Write-Host "--- Treating subtitle file: " $fullSubtitlePath
((Get-Content -LiteralPath $fullSubtitlePath) -creplace "º", "s" -creplace "þ", "t" -creplace "ª", "S" -creplace "Þ", "T") | set-content -LiteralPath $fullSubtitlePath
}
##############################################################################################################
## Add the possibility to run the script using Windows Explorer contextual menu when right clicking a file
# 1. Head to HKEY_CLASSES_ROOT\*\shell in the registry
# 2. Create a new Key named what you want (like say "FixSubtitles")
# 3. Set the "Default" REG_SZ value of that new key to the text you want to appear on the context menu
# (like say "Fix subtitles in this folder")
# 4. Create another new Key inside the key you just made, and name it "command"
# 5. Set the value of the "Default" REG_SZ in that new 'command' key to run the command you want
# 6. cmd /C powershell.exe -executionpolicy unrestricted -File "C:\Users\vigi\Documents\fix_subtitles_in_folder.ps1" "%1"
# 7. -File Runs the specified script in the local scope ("dot-sourced"), so that the functions and
# variables that the script creates are available in the current session. File must be the last
# parameter in the command, because all characters typed after the File parameter name are
# interpreted as the script file path followed by the script parameters.
package org.vigi;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Created by vigi on 11/18/2015.
*/
public class FixSubtitles {
public static void main(String[] args) throws IOException {
// cmd /K java -jar "D:\My Documents\subtitles.jar" in the registry key HKEY_CLASSES_ROOT\*\shell\Fix subtitles\command
String path = args.length > 0 ? args[0] : ".";
Path folder = Paths.get(path);
System.out.println("--- Folder: " + folder.getFileName());
Charset defaultCharset = Charset.defaultCharset();
System.out.println("--- Default charset is: " + defaultCharset);
try (DirectoryStream<Path> ds = Files.newDirectoryStream(folder, "*.{srt,sub}")) {
for (Path file : ds) {
replaceInFile(file);
}
}
}
private static void replaceInFile(Path file) throws IOException {
System.out.println("--- Replacing subtitle file: " + file.getFileName());
Charset charset = StandardCharsets.ISO_8859_1;
String content = new String(Files.readAllBytes(file))
.replaceAll("º", "s")
.replaceAll("þ", "t")
.replaceAll("ª", "S")
.replaceAll("Þ", "T");
Files.write(file, content.getBytes());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment