Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Last active October 23, 2023 16:27
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 Hashbrown777/a9e3053f6f9f70fab4f656a3f79a1839 to your computer and use it in GitHub Desktop.
Save Hashbrown777/a9e3053f6f9f70fab4f656a3f79a1839 to your computer and use it in GitHub Desktop.
Param($file)
$file = Get-Item $file
mkvinfo $file.FullName `
| &{
Begin {
$current = $NULL
}
Process {
if (!$current) {
}
elseif ($_ -match '^\| \+ ([^:]+): (.*)$') {
switch -Exact ($Matches[1]) {
'Language (IETF BCP 47)' {
$current['language'] = $Matches[2]
}
'Name' {
$current['name'] = $Matches[2]
}
'Track type' {
$current['type'] = $Matches[2]
}
'Track number' {
$current['track'] = ($Matches[2] -replace '[^0-9].*','') - 1
}
'Codec ID' {
$current['extension'] = switch -Exact ($Matches[2] -replace '[^A-Z0-9_].*', '') {
'A_AAC' { '.aac' }
'V_MPEG4' { '.m4v' }
'S_TEXT' { '.srt' }
}
}
}
}
else {
$current['filename'] += '_' + ($current['track'] + 1)
if ($current['name']) {
$current['filename'] += '_' + $current['name']
}
if ($current['type'] -in 'audio','subtitles') {
$current['filename'] += (" ($($current['language']))","_$($current['language'])")[!$current['name']]
}
$current['filename'] += $current['extension']
$current
$current = $NULL
}
if ($_ -match '^\| \+ Track') {
$current = @{
filename = $file.BaseName
}
}
}
} `
| &{
Begin {
$commands = $file.FullName,'tracks'
}
Process {
$commands += $_['track'],$_['filename'] -join ':'
}
End {
mkvextract @commands
}
}
gi *.srt `
| %{
$item = $_
&{
'WEBVTT'
"Language: $($item.BaseName -replace '^.*_','' -replace '^.* *\((.*)\)$','$1')"
''
gc $item
} `
| %{
if ($_ -match '^((^|:)\d{2}){2,3},\d{3}(( --> |:)\d{2}){2,3},\d{3}(\s|$)') {
([Regex]',').Replace($_, '.', 2)
}
else {
$_
}
"`r`n"
} `
| Out-File -NoNewline -LiteralPath ($_.BaseName + '.vtt')
}
ffmpeg \
-i "$1.mp4" \
-c:v libx264 \
-crf 23 \
-c:a aac \
-movflags faststart \
-maxrate 2M \
-bufsize 2M \
"$dest/$1.mp4"
curl \
--form subrip_file=@"$1.srt" \
-o "$dest/$1.vtt" \
'https://atelier.u-sub.net/srt2vtt/'
@Hashbrown777
Copy link
Author

Hashbrown777 commented Oct 21, 2023

gi *.srt | %{ curl --form "subrip_file=@$($_.Name)" -o ($_.Name -replace 'srt$','vtt') 'https://atelier.u-sub.net/srt2vtt/' }
MP4Box -add x_1.m4v -new x.mp4; MP4Box -add x_2.acc x.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment