Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $c = Read-Host "input youtube url"
- $m = Read-Host "input minutes"
- # 경로설정
- # 임시로 다운로드될 파일 경로
- $tempRoot = Join-Path -Path "${PSScriptRoot}" -ChildPath "temp"
- # yt-dlp 와 ffmpege가 위치한 경로
- $binRoot= Join-Path -Path "${PSScriptRoot}" -ChildPath "bin"
- # yt-dlp의 경로
- $ytdlp = Get-ChildItem -Path "${binRoot}" -Filter "yt-dlp.exe"
- # ffmpege 경로
- $ffmpeg = Get-ChildItem -Path "${binRoot}" -Filter "ffmpeg.exe"
- # 고유아이디
- $guid = New-Guid
- <#
- 다운로드 실행
- - yt-dlp가 실행되는 부분 옵션을 추가하고 싶으면 여기에 추가하면됨
- - 파일명을 수정하고 싶으면 영상정보 가져오는곳의 경로나 저장경로를 수정
- #>
- 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
- # 영상정보 가져오기 (파일경로 만들때 사용)
- $info = & $ytdlp $c --no-warnings --print filename,id --skip-download -P "${PSScriptRoot}" -o "download/%(uploader)s/[%(release_date>%Y-%m-%d)s] %(title)s"
- $filePath = $info[0] -replace " [\d-]{10} [\d_]+$", "" # yt-dlp 이 버전에서 파일명에 다운받은 날짜 시간을 포함하기에 삭제
- $dirPath = Split-Path "$filePath" # 파일이 저장될 폴더
- # 파일명 패턴
- $partPattern = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(f\d+)\.(\w+)\.part$"
- $videoPatten = "${guid}_(\d+\.\d+)-(\d+\.\d+)\.(\w+)$"
- # 디렉터리 내의 파일 목록 가져오기
- $files = Get-ChildItem "$tempRoot"
- # 대상폴더가 없다면 폴더 생성
- if (-not (Test-Path "$dirPath" -PathType Container)) {
- $null = New-Item -Path "$dirPath" -ItemType Directory
- }
- # 정규식을 사용하여 파일 목록 필터링
- $partFiles = $files | Where-Object { $_.Name -match $partPattern }
- $videoFiles = $files | Where-Object { $_.Name -match $videoPatten }
- # 다운로드한 영상 결과에 따른 처리
- if ($partFiles.Count -eq 2 -and $partFiles[0] -match $partPattern) { # part 파일이 존재하는 경우 (다운로드 완료전 종료)
- # 병합할 파일경로
- $file1 = $partFiles[0].FullName
- $file2 = $partFiles[1].FullName
- # 섹션 시간 변환
- $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1]).ToLocalTime().ToString("HHmmss")
- $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2]).ToLocalTime().ToString("HHmmss")
- # 확장자
- $ext = $Matches[4]
- # 저장경로
- $savePath = "${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"
- # ffmpeg로 오디오 비디오 파일 병합
- & ${ffmpeg} -i "${file1}" -i "${file2}" -c copy "${savePath}" -loglevel error
- Write-Host "다운로드된 임시 파일을 병합했습니다"
- } elseif ($videoFiles.Count -eq 1 -and $videoFiles[0] -match $videoPatten) { # 비디오 파일이 존재하는 경우
- #섹션 시간 변환
- $sectionTimeStart = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[1]).ToLocalTime().ToString("HHmmss")
- $sectionTimeEnd = (Get-Date "1970-01-01 00:00:00").AddSeconds($Matches[2]).ToLocalTime().ToString("HHmmss")
- #확장자
- $ext = $Matches[3]
- # 저장경로
- $savePath = "${filePath} [${sectionTimeStart}-${sectionTimeEnd}].${ext}"
- # 파일 이동 (이름변경)
- Move-Item -Path "$($videoFiles[0].FullName)" -Destination "${savePath}"
- Write-Host "다운로드가 완료되었습니다."
- } else {
- Write-Host "일치하는 파일이 존재하지 않습니다."
- }
- # 임시파일 삭제
- Get-ChildItem -Path "$tempRoot" -Filter "${guid}_*" | ForEach-Object { Remove-Item $_.FullName }
- # 다운로드된 경로열기
- explorer "$dirPath"
- # 자동종료방지
- Read-Host "press enter key to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement