Advertisement
AnCak

Untitled

Oct 30th, 2023 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $c = Read-Host "input youtube url"
  2. $m = Read-Host "input minutes"
  3.  
  4. # Root 경로 이 경로 밑의 download 에 저장됨 수정해서 사용
  5. $root = "E:\yt-dlp-live-sections"
  6. # 임시로 다운로드될 파일 경로
  7. $troot = "E:\yt-dlp-live-sections\temp"
  8. # yt-dlp의 경로
  9. $ytdlp = "E:\yt-dlp-live-sections\bin\yt-dlp.exe"
  10. # ffmpege 경로
  11. $ffmpeg = "E:\yt-dlp-live-sections\bin\ffmpeg.exe"
  12.  
  13. # 파일저장 정보 가져오기 (파일경로 파일명에서 사용불가능한 특수문자 제거 귀찮아서 사용)
  14. $info = Invoke-Expression "${ytdlp} $c --no-warnings --print filename,id --skip-download -P ${root} -o ""download/%(uploader)s/[%(release_date>%Y-%m-%d)s] %(title)s"""
  15. $filePath = $info[0] -replace " [\d-]{10} [\d_]+$", "" # yt-dlp 이 버전에서 파일명에 다운받은 날짜 시간을 포함하기에 삭제
  16. $guid = New-Guid # 고유아이디
  17. $dirPath = Split-Path $filePath # 파일이 저장될 폴더
  18.  
  19. # 다운로드 실행
  20. Start-Process $ytdlp -ArgumentList $c,--live-from-start,"--download-sections ""#-${m}minutes - 0""",-P,$troot,-o,"${guid}_%(section_start)s-%(section_end)s.%(ext)s" -Wait
  21.  
  22. # 파일명 패턴
  23. $partPattern = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(f\d+)\.(\w+)\.part$"
  24. $videoPatten = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(\w+)$"
  25.  
  26. # 디렉터리 내의 파일 목록 가져오기
  27. $files = Get-ChildItem $troot
  28.  
  29. # 대상폴더가 없다면 폴더 생성
  30. if (-not (Test-Path $dirPath -PathType Container)) {
  31.     $null = New-Item -Path $dirPath -ItemType Directory
  32. }
  33.  
  34. # 정규식을 사용하여 파일 목록 필터링
  35. $partFiles = $files | Where-Object { $_.Name -match $partPattern }
  36. $videoFiles = $files | Where-Object { $_.Name -match $videoPatten }
  37.  
  38. if ($partFiles.Count -eq 2) { # 파트파일이 존재하는 경우 (다운로드 완료전 종료)
  39.     # 병합할 파일경로
  40.     $file1 = $partFiles[0].FullName
  41.     $file2 = $partFiles[1].FullName
  42.  
  43.     $partFiles[0] -match $partPattern
  44.     #섹션 시간 변환
  45.     $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1] ).ToLocalTime().ToString("HHmmss")
  46.     $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2] ).ToLocalTime().ToString("HHmmss")
  47.  
  48.     #확장자
  49.     $ext = $Matches[4]
  50.     # ffmpeg로 오디오 비디오 파일 병합
  51.     Invoke-Expression "${ffmpeg} -i ${file1} -i ${file2} -c copy ""${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"""
  52.     Write-Host "강제종료되었습니다. 다운로드된 임시 파일을 병합했습니다"
  53. } elseif ($videoFiles.Count -eq 1) { # 비디오 파일이 존재하는 경우
  54.     $videoFiles[0] -match $videoPatten
  55.  
  56.     #섹션 시간 변환
  57.     $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1] ).ToLocalTime().ToString("HHmmss")
  58.     $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2] ).ToLocalTime().ToString("HHmmss")
  59.  
  60.     #확장자
  61.     $ext = $Matches[3]
  62.  
  63.     # 파일 이동 (이름변경)
  64.     Move-Item -Path $videoFiles[0].FullName -Destination "${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"
  65. } else {
  66.     Write-Host "일치하는 파일이 존재하지 않습니다."
  67. }
  68.  
  69. # 임시파일 삭제
  70. Get-ChildItem -Path $troot -Filter "${guid}_*" | ForEach-Object { Remove-Item $_.FullName }
  71.  
  72. # 다운로드된 경로열기
  73. explorer $dirPath
  74.  
  75. # 자동종료방지
  76. Read-Host "press enter key to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement