Advertisement
Combreal

createSRTfromYTTranscr.ps1

Oct 28th, 2023
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.52 KB | Source Code | 0 0
  1. <#
  2.     Turn Youtube transcription into srt file. Specify the transcription file name, the offset in minute(s), the offset in second(s) and the end time of the video bellow.
  3.     To get a youtube video transcription :
  4.     Click the 3 dots next to the Share button and click Display the transcription. Then in the comment section click Display the transcription. Copy and paste the transcription on the right of the video to a file.
  5. #>
  6.  
  7. $transcriptionFile = "subtitle-issou-short-retimed.txt"
  8. $minutesOffset = 3
  9. $secondsOffset = 21
  10. $endTime = "00:03:03,000" # example : 3 minutes and 3 seconds
  11.  
  12. $PSDefaultParameterValues = @{'*:Encoding' = 'utf8'}
  13. $counter = 0
  14. $CustomObjs=@()
  15. $previousTime = "00:00:00,000"
  16. $srtName = $transcriptionFile.Replace(".txt", "")
  17. $srtName = "$srtName.srt"
  18. if (Test-Path $srtName) {
  19.   Remove-Item $srtName
  20. }
  21.  
  22. Get-Content $transcriptionFile | foreach {
  23.     If ($_ -match '\d{1}\:') {
  24.         $counter = $counter + 1
  25.         $oldTime = $_.Split(":")
  26.         $oldTimeMinutes = $oldTime[0]
  27.         $oldTimeSecondes = $oldTime[1]
  28.         $newTimeNMinutes = $oldTimeMinutes - $minutesOffset
  29.         [int]$newTimeSecondes = $oldTimeSecondes - $secondsOffset
  30.         If ($newTimeSecondes -lt 0) {
  31.             $newTimeNMinutes = $newTimeNMinutes - 1
  32.             $newTimeSecondes = 60 + $newTimeSecondes
  33.         }
  34.         If ($newTimeSecondes -lt 10) {
  35.             $strNewTimeNMinutes = [string]$newTimeSecondes
  36.             $strNewTimeNMinutes = '0' + $strNewTimeNMinutes
  37.             [string]$newTimeSecondes = $strNewTimeNMinutes
  38.         }
  39.         $newTime = "00:0$($newTimeNMinutes):$newTimeSecondes"
  40.         If ($counter - 2 -ge 0) {
  41.             [string]$CustomObjs[$counter-2].Time = "$($CustomObjs[$counter-2].Time + $newTime),000"
  42.             $dateArray =$([string]$CustomObjs[$counter-2].Time).Split(" --> ")
  43.             $fullTime = "$($dateArray[5]) --> "
  44.         }
  45.         Else {
  46.             $fullTime = "00:00:00,000 --> "
  47.         }
  48.         $CustomObj = New-Object -TypeName PSObject -Property @{ 'ID' = $counter ; 'Time' = $fullTime ; 'Text' = "" }
  49.     }
  50.     Else {
  51.         $CustomObj.Text = $_
  52.         $CustomObjs += $CustomObj
  53.     }
  54. }
  55. [string]$CustomObjs[$counter-1].Time = "$($CustomObjs[$counter-1].Time)$endTime"
  56.  
  57. $endCounter = 0
  58. $CustomObjs | foreach {
  59.     If ($endCounter -eq 33) {
  60.         Add-Content $srtName "$($_.ID)`n$($_.Time)`n$($_.Text)" -NoNewline
  61.     }
  62.     Else {
  63.         Add-Content $srtName "$($_.ID)`n$($_.Time)`n$($_.Text)`n"
  64.     }
  65.     $endCounter = $endCounter + 1
  66. }
Tags: srt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement