Skip to content

Instantly share code, notes, and snippets.

@musinsky
Last active February 6, 2024 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musinsky/cef5dad7d153994b4e10c40bb1b78333 to your computer and use it in GitHub Desktop.
Save musinsky/cef5dad7d153994b4e10c40bb1b78333 to your computer and use it in GitHub Desktop.
PowerShell bin cue rename
# 2022-05-31
<#
PS> Get-ExecutionPolicy # PowerShell 5.1 (default in Win10)
Restricted
PS> Set-ExecutionPolicy Unrestricted -Force # as admin
PS> Get-ExecutionPolicy # PowerShell 7.2.3 (installed in Win10)
RemoteSigned
#>
<#
https://docs.microsoft.com/en-us/powershell/scripting/dev-cross-plat/vscode/understanding-file-encoding
https://docs.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell
In PowerShell 6+, the default encoding 'utf8NoBOM' is UTF-8 without BOM on all platforms.
But option 'utf8NoBOM' is not available in previous PowerShell 5.1 (in Win10 default).
In Windows PowerShell 5, the default encoding 'Default' (usually ANSI) is usually Windows-1252,
an extension of latin-1, also known as ISO 8859-1. Options 'UTF8' in PowerShell 5.1 is with BOM.
!!! Change $OutputEncoding to use UTF-8 NoBOM encoding rather than ASCII !!!
The previous encoding, ASCII (7-bit), would result in incorrect alteration of the output in some cases.
Making UTF-8 NoBOM the default preserves Unicode output with an encoding supported by most tools
and operating systems.
'utf8NoBOM' UTF-8 without BOM is best choice and is default in PowerShell 6+.
#>
<#
https://renenyffenegger.ch/notes/Windows/PowerShell/command-inventory/noun/childItem/get/index
'-Filter' is faster, and the results are more predictable.
'-Filter' accepts only a single string. '-Include' accepts multiple values.
Get-ChildItem -File -Recurse -Filter "*.ogg"
#>
# https://gist.github.com/Pxtl/daed801def23a08cb81c6b8b771d69d6
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.ogg", "*.mp3" | ForEach-Object {
$BinName = $_.Name
$BinFullName = $_.FullName
Write-Host("bin file :", $BinFullName)
$CueFullName = [System.IO.Path]::ChangeExtension($BinFullName, 'cue')
$CueTest = [System.IO.Path]::ChangeExtension($BinFullName, 'test.cue') # test
Write-Host("cue file :", $CueFullName)
$content = Get-Content -Path $CueFullName
$lineOld = $content | Select-String 'FILE "' | Select-Object -ExpandProperty Line
Write-Host("line old :", $lineOld)
$lineNew = 'FILE "' + $BinName + '" WAVE'
Write-Host("line new :", $lineNew)
# $FinalCueName = $CueFullName # rewrite original CUE file
$FinalCueName = $CueTest # create new test CUE file
# $content -replace $lineOld, $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # problem with parentheses in PowerShell
# https://stackoverflow.com/questions/39101291/replace-does-not-replace-string-with
# $content -replace [Regex]::Escape($lineOld), $lineNew | Set-Content -Encoding utf8NoBOM -Path $FinalCueName -Force # OK
# https://stackoverflow.com/questions/17144355/how-can-i-replace-every-occurrence-of-a-string-in-a-file-with-powershell
# don't require PowerShell 6+, this method take care of the encoding of the file (UTF-8 without BOM)
$content2 = [System.IO.File]::ReadAllText($CueFullName).Replace($lineOld, $lineNew)
[System.IO.File]::WriteAllText($FinalCueName, $content2)
Write-Host("")
}
# 2022-05-12
#Requires -Version 6.0
Get-ChildItem -File -Recurse -Include "*.srt" | ForEach-Object {
$FileFullName = $_.FullName
Write-Host("file :", $FileFullName)
$FileTest = [io.path]::ChangeExtension($FileFullName, 'test.srt') # test
$content = Get-Content -Path $FileFullName
# $content | Set-Content -Encoding utf8NoBOM -Path $FileFullName -Force # rewrite original SRT file
$content | Set-Content -Encoding utf8NoBOM -Path $FileTest -Force # create new test SRT file
Write-Host("")
}
# 2024-02-07
# PS script in UTF8 (no ANSI)
$in_dir = "e:\2024_01_CT\Navstevnici.1983.WEB-DL.1080p.Cze\"
$in_ext = ".ts"
$out_dir = "z:\TV_SeriesCS\Navstevnici.1983.WEB-DL.1080p.Cze\"
$out_ext = ".mkv"
$mkvmerge_exe = "D:\PROGRAMS\MKVToolNix\mkvmerge.exe"
$mkv_a_lang = "cs"
foreach ($in_file in Get-ChildItem -Path $in_dir *$in_ext)
{
Write-Host "in :" $in_file
Write-Host "in.dir :" $in_file.Directory
Write-Host "in.name :" $in_file.Name
Write-Host "in.base :" $in_file.BaseName
Write-Host "in.ext :" $in_file.Extension
Write-Host ""
# https://copyprogramming.com/howto/powershell-change-extension
# System.IO.Path class from .NET
#$out_file = [System.IO.Path]::ChangeExtension($in_file.Name, $out_ext)
#
# Get-ChildItem *.txt | Rename-Item -NewName { $_.name -replace '\.txt$','.log' }
# The $ represents the syntax of a regular expression, which signifies the end of a line.
# This rule ensures that the -replace does not match the occurrence of "extension"
# in any other part of the filename.
#
#$out_file = $in_file.Name -replace $in_ext, $out_ext # WRONG
$out_file = $in_file.Name -replace "$in_ext$", $out_ext
$out_file = $out_dir + $out_file
Write-Host "out: " $out_file
Write-Host "=========="
# Call operator &
& $mkvmerge_exe --output $out_file `
--no-subtitles --no-chapters `
--no-buttons --no-attachments --no-track-tags --no-global-tags `
--language 0:und `
--language 1:$mkv_a_lang `
'(' $in_file ')' `
--track-order 0:0,0:1 `
--verbose
Start-Sleep -Seconds 1.0
Write-Host "=================================================="
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment