Skip to content

Instantly share code, notes, and snippets.

@xaguzman
Last active February 22, 2020 03:12
Show Gist options
  • Save xaguzman/fcf99fa2c130d603f00222bca66744ea to your computer and use it in GitHub Desktop.
Save xaguzman/fcf99fa2c130d603f00222bca66744ea to your computer and use it in GitHub Desktop.
Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-9bda399\src\posh-git.psd1'
Function SrtToUtf8(){
Param(
[Parameter(Mandatory=$true)] [string] $Path
)
if (Test-Path $Path -PathType Leaf){
Write-Host "Attemping to convert $Path to UTF8"
$tmp = "$Path.tmp";
$lines = Get-Content $Path
$keep = $true
$newContent = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $lines.Length - 1; $i++){
$line = $lines[$i]
if ([string]::Compare($line, "www.tusubtitulo.com", $true) -eq 0){
$keep = $false
}elseif ([string]::IsNullOrEmpty($line)){
$keep = $true
}
if ($keep){
$c = $newContent.Add($line)
}
}
Set-Content $tmp $newContent -Encoding UTF8
Remove-Item $Path
Move-Item $tmp $Path
}else{
Throw "$Path not found";
}
}
Function Mp4Subtitle(){
Param(
[Parameter(Mandatory=$false)] [string] $Path = ".",
[Switch] $SrtToUTF8
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "mp4box" -ErrorAction SilentlyContinue))
{
Throw 'mp4box not found in Path'
}
#$mkvFilePath = resolve-path $path
$mp4FilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mp4FilePath -PathType Container){
Get-ChildItem $mp4FilePath -Filter *.mp4 | ForEach-Object {
Mp4Subtitle -Path $_.FullName -SrtToUTF8:$SrtToUTF8
}
}else{
#Write-Host "Param Path: $Path, mkv: $mp4FilePath";
$mp4 = Get-Item $mp4FilePath;
$srt = "$($mp4.Directory.FullName)\$($mp4.BaseName).srt"
if (Test-Path $srt -PathType Leaf){
if ($SrtToUTF8){
SrtToUtf8 -Path $srt
}
Write-Host "Adding subtitle $srt to $($mp4.FullName)";
mp4box -lang 2=eng -add "$($srt):lang=spa" "$($mp4.FullName)"
Rename-Item "$srt" "$($srt.Replace('.srt', '.spa.srt'))"
}else{
Write-Host "Skipping subtitling of $($mp4.Name), no srt found with name $($mp4.BaseName).srt"
}
}
}
Function Mp4FastStart(){
Param(
[Parameter(Mandatory=$false)] [string] $Path = "."
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue))
{
Throw 'ffmpeg not found in Path'
}
$mp4FilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mp4FilePath -PathType Container){
Get-ChildItem $mp4FilePath -Filter *.mp4 | ForEach-Object {
Mp4FastStart -Path $_.FullName
}
}else{
#Write-Host "Param Path: $Path, mkv: $mp4FilePath";
$mp4 = Get-Item $mp4FilePath;
$outPath = "$($mp4.Directory.FullName)\$($mp4.BaseName)_fast-start.$($mp4.Extension)"
Write-Host "Adding movflag +faststart to $($mp4.FullName). Saving ass $outPath";
ffmpeg -i "$($mp4.FullName)" -c copy -map 0 -movflags +faststart "$outPath"
}
}
Function MkvToMp4()
{
Param(
[Parameter(Mandatory=$false)] [string] $Path = ".",
[Switch] $Subtitle,
[Switch] $SrtToUTF8
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue))
{
Throw 'ffmpeg not found in Path'
}
#$mkvFilePath = resolve-path $path
$mkvFilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mkvFilePath -PathType Container){
Get-ChildItem $mkvFilePath -Filter *.mkv | ForEach-Object {
MkvToMp4 -Path $_.FullName -Subtitle:$Subtitle -SrtToUTF8:$SrtToUTF8
}
}else{
#Write-Host "Param Path: $Path, mkv: $mkvFilePath";
$mkv = Get-Item $mkvFilePath;
$destFile = "$($mkv.Directory.FullName)\$($mkv.BaseName).mp4"
Write-Host "Changing container $mkvFilePath to $destFile";
ffmpeg -i "$($mkv.FullName)" -acodec copy -vcodec copy -scodec mov_text -map 0 -movflags faststart "$destFile"
if ($Subtitle){
Mp4Subtitle -Path "$destFile" -SrtToUTF8:$SrtToUTF8
}
}
}
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
Function srtToUtf8(){
Param(
[Parameter(Mandatory=$true)] [string] $Path
)
if (Test-Path $Path -PathType Leaf){
Write-Host "Attemping to convert $Path to UTF8"
$tmp = "$Path.tmp";
#$lines = Get-Content $Path # for windows
$lines = iconv -f ISO-8859-1 -t UTF-8//TRANSLIT $Path #for linux
$keep = $true
$newContent = New-Object System.Collections.ArrayList
for ($i = 0; $i -le $lines.Length - 1; $i++){
$line = $lines[$i]
if ([string]::Compare($line, "www.tusubtitulo.com", $true) -eq 0){
$keep = $false
}elseif ([string]::IsNullOrEmpty($line)){
$keep = $true
}
if ($keep){
$c = $newContent.Add($line)
}
}
Set-Content $tmp $newContent -Encoding UTF8
Remove-Item $Path
Move-Item $tmp $Path
}else{
Throw "$Path not found";
}
}
Function mp4Subtitle(){
Param(
[Parameter(Mandatory=$false)] [string] $Path = ".",
[Switch] $SrtToUTF8
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "MP4Box" -ErrorAction SilentlyContinue))
{
Throw 'MP4Box not found in Path'
}
#$mkvFilePath = resolve-path $path
$mp4FilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mp4FilePath -PathType Container){
Get-ChildItem $mp4FilePath -Filter *.mp4 | ForEach-Object {
Mp4Subtitle -Path $_.FullName -SrtToUTF8:$SrtToUTF8
}
}else{
#Write-Host "Param Path: $Path, mkv: $mp4FilePath";
$mp4 = Get-Item $mp4FilePath;
$srt = "$($mp4.Directory.FullName)/$($mp4.BaseName).srt"
if (Test-Path $srt -PathType Leaf){
if ($SrtToUTF8){
SrtToUtf8 -Path $srt
}
Write-Host "Adding subtitle $srt to $($mp4.FullName)";
MP4Box -lang 2=eng -add "$($srt):lang=spa" "$($mp4.FullName)"
Rename-Item "$srt" "$($srt.Replace('.srt', '.spa.srt'))"
}else{
Write-Host "Skipping subtitling of $($mp4.Name), no srt found with name $($mp4.BaseName).srt"
}
}
}
Function mp4FastStart(){
Param(
[Parameter(Mandatory=$false)] [string] $Path = "."
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue))
{
Throw 'ffmpeg not found in Path'
}
$mp4FilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mp4FilePath -PathType Container){
Get-ChildItem $mp4FilePath -Filter *.mp4 | ForEach-Object {
Mp4FastStart -Path $_.FullName
}
}else{
#Write-Host "Param Path: $Path, mkv: $mp4FilePath";
$mp4 = Get-Item $mp4FilePath;
$outPath = "$($mp4.Directory.FullName)/$($mp4.BaseName)_fast-start.$($mp4.Extension)"
Write-Host "Adding movflag +faststart to $($mp4.FullName). Saving ass $outPath";
ffmpeg -i "$($mp4.FullName)" -c copy -map 0 -movflags +faststart "$outPath"
}
}
Function mkvToMp4()
{
Param(
[Parameter(Mandatory=$false)] [string] $Path = ".",
[Switch] $Subtitle,
[Switch] $SrtToUTF8
)
#make sure ffmpeg is in the Path
if (-not (Get-Command "ffmpeg" -ErrorAction SilentlyContinue))
{
Throw 'ffmpeg not found in Path'
}
#$mkvFilePath = resolve-path $path
$mkvFilePath = Resolve-Path $Path;
#if the path is a directory, look for all the files
if (Test-Path $mkvFilePath -PathType Container){
Get-ChildItem $mkvFilePath -Filter *.mkv | ForEach-Object {
MkvToMp4 -Path $_.FullName -Subtitle:$Subtitle -SrtToUTF8:$SrtToUTF8
}
}else{
#Write-Host "Param Path: $Path, mkv: $mkvFilePath";
$mkv = Get-Item $mkvFilePath;
$destFile = "$($mkv.Directory.FullName)/$($mkv.BaseName).mp4"
Write-Host "Changing container $mkvFilePath to $destFile";
ffmpeg -i "$($mkv.FullName)" -acodec copy -vcodec copy -scodec mov_text -map 0 -movflags faststart "$destFile"
if ($Subtitle){
Mp4Subtitle -Path "$destFile" -SrtToUTF8:$SrtToUTF8
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment