Advertisement
AnCak

yt-dlp-section

Nov 15th, 2023
18
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. # 경로설정
  5. # 임시로 다운로드될 파일 경로
  6. $tempRoot = Join-Path -Path "${PSScriptRoot}" -ChildPath "temp"
  7. # yt-dlp 와 ffmpege가 위치한 경로
  8. $binRoot= Join-Path -Path "${PSScriptRoot}" -ChildPath "bin"
  9. # yt-dlp의 경로
  10. $ytdlp = Get-ChildItem -Path "${binRoot}" -Filter "yt-dlp.exe"
  11. # ffmpege 경로
  12. $ffmpeg = Get-ChildItem -Path "${binRoot}" -Filter "ffmpeg.exe"
  13.  
  14. # 고유아이디
  15. $guid = New-Guid
  16.  
  17. <#
  18. 다운로드 실행
  19.   - yt-dlp가 실행되는 부분 옵션을 추가하고 싶으면 여기에 추가하면됨
  20.   - 파일명을 수정하고 싶으면 영상정보 가져오는곳의 경로나 저장경로를 수정
  21. #>
  22. Start-Process "$($ytdlp.FullName)" -ArgumentList "$c",--live-from-start,"--download-sections ""#-${m}minutes - 0""","-P ""$tempRoot""","-o ""${guid}_%(section_start)s-%(section_end)s.%(ext)s""" -Wait
  23.  
  24. # 영상정보 가져오기 (파일경로 만들때 사용)
  25. $info = & $ytdlp $c --no-warnings --print filename,id --skip-download -P "${PSScriptRoot}" -o "download/%(uploader)s/[%(release_date>%Y-%m-%d)s] %(title)s"
  26. $filePath = $info[0] -replace " [\d-]{10} [\d_]+$", "" # yt-dlp 이 버전에서 파일명에 다운받은 날짜 시간을 포함하기에 삭제
  27. $dirPath = Split-Path "$filePath" # 파일이 저장될 폴더
  28.  
  29. # 파일명 패턴
  30. $partPattern = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(f\d+)\.(\w+)\.part$"
  31. $videoPatten = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(\w+)$"
  32.  
  33. # 디렉터리 내의 파일 목록 가져오기
  34. $files = Get-ChildItem "$tempRoot"
  35.  
  36. # 대상폴더가 없다면 폴더 생성
  37. if (-not (Test-Path "$dirPath" -PathType Container)) {
  38.     $null = New-Item -Path "$dirPath" -ItemType Directory
  39. }
  40.  
  41. # 정규식을 사용하여 파일 목록 필터링
  42. $partFiles = $files | Where-Object { $_.Name -match $partPattern }
  43. $videoFiles = $files | Where-Object { $_.Name -match $videoPatten }
  44.  
  45. # 다운로드한 영상 결과에 따른 처리
  46. if ($partFiles.Count -eq 2 -and $partFiles[0] -match $partPattern) { # part 파일이 존재하는 경우 (다운로드 완료전 종료)
  47.     # 병합할 파일경로
  48.     $file1 = $partFiles[0].FullName
  49.     $file2 = $partFiles[1].FullName
  50.  
  51.     # 섹션 시간 변환
  52.     $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1]).ToLocalTime().ToString("HHmmss")
  53.     $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2]).ToLocalTime().ToString("HHmmss")
  54.     # 확장자
  55.     $ext = $Matches[4]
  56.     # 저장경로
  57.     $savePath = "${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"
  58.  
  59.     # ffmpeg로 오디오 비디오 파일 병합
  60.     & ${ffmpeg} -i "${file1}" -i "${file2}" -c copy "${savePath}" -loglevel error
  61.     Write-Host "다운로드된 임시 파일을 병합했습니다"
  62. } elseif ($videoFiles.Count -eq 1 -and $videoFiles[0] -match $videoPatten) { # 비디오 파일이 존재하는 경우
  63.     #섹션 시간 변환
  64.     $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1]).ToLocalTime().ToString("HHmmss")
  65.     $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2]).ToLocalTime().ToString("HHmmss")
  66.     #확장자
  67.     $ext = $Matches[3]
  68.     # 저장경로
  69.     $savePath = "${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"
  70.  
  71.     # 파일 이동 (이름변경)
  72.     Move-Item -Path "$($videoFiles[0].FullName)" -Destination "${savePath}"
  73.     Write-Host "다운로드가 완료되었습니다."
  74. } else {
  75.     Write-Host "일치하는 파일이 존재하지 않습니다."
  76. }
  77.  
  78. # 임시파일 삭제
  79. Get-ChildItem -Path "$tempRoot" -Filter "${guid}_*" | ForEach-Object { Remove-Item $_.FullName }
  80.  
  81. # 다운로드된 경로열기
  82. explorer "$dirPath"
  83.  
  84. # 자동종료방지
  85. Read-Host "press enter key to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement