yt.ps1

7r·2023년 5월 12일

PowerShell Script

목록 보기
2/2

yt-dlp 유튜브 실시간 추적 다운로더

업데이트

22.12.18 - ffmpeg merge 옵션 삭제
22.11.21 - --no-keep-fragments 옵션 추가, 3개의 쓰레드 사용 옵션 제거
22.10.08 - yt-dlp 설치링크 추가( 박스 용량문제로 yt-dlp 삭제)
22.10.01 - 출력파일명 수정 (채널명-연월일-방송시작시간-방제.확장자)

실행법

yt [유튜브링크][저장할_최상위_폴더]

사용준비

박스

arst

설치법

  1. yt-dlp 설치 (환경변수 설정된 폴더에 저장해야합니다.)

  2. 압축 해제한 폴더를 본인지 절대 지우지 않을 폴더에 저장한다.

  3. 윈도우 탐색기에서 yt-dlp.exe가 보이는 데까지 이동한 후 아래 링크를 따라서 한다

  • 환경 변수 폴더 만들기 - 7r 채널 (arca.live)
  1. 등록한 후, 파워쉘을 끄고 켠다.

  2. 파워쉘에서 아래 명령어를 입력하면, 2022.11.11"와 같이 업데이트 된 날짜가 나와야 한다.

    yt-dlp.exe --version

  3. 파일을 열어 수정할 부분을 수정한다.

  4. 주석을 보고 설명에 따라서 아래 변수를 설정한다

    • "과 "사이에 원하는 폴더 경로를 입력하세요. 그러면 파워쉘스크립트 실행시에 따로 입력하지 않는 한 기본값으로 적용이됩니다.
[string]$root_path="$HOME\Videos\youtube\streaming"
  1. 유튜브 성인인증 방법 : 성인인증 된 계정 로그인 하고 쿠키를 추출한다.
    • 쿠키를 추춮한 파일의 절대경로\파일명을 C:\youtube.com_cookies.txt 대신에 입력해 주세요.
    • ffmpeg 설치된 절대경로를 C:\ffmpeg\bin 대신에 입력해주세요.
    • 2개로 나눠지는 경우를 최대한 막기위해 필요합니다.
[string]$configs = "--cookies `"C:\youtube.com_cookies.txt`" --ffmpeg-location `"C:\ffmpeg\bin`""
  1. 참고로 실시간 추적 기능은 yt-dlp에서도 실험적인 기능으로 안정성을 보장하지 않습니다.
  2. .ytdl 파일(주로 남는 찌꺼기 파일입니다.)을 지웁니다.
  3. 파일이 두개로 분리될 수 있기 때문에 아래 명령어로 합치면 됩니다.
  4. ffmpeg -i '비디오파일' -i '오디오파일' -c copy '저장할파일명.확장자'

주의사항

  • 꼭 yt-dlp.exe 파일을 환경변수 설정된 폴더에 저장해야 합니다.
  • [yt-dlp] 유튜브 실시간 추적 다운로더 - 7r 채널 (arca.live) 에서 질문을 받습니다.

yt-dlp 업데이트 방법

  1. yt-dlp 업데이트: 아래 명령어를 파워쉘에서 입력합니다. 환경변수가 설정되어 있다면 정상 작동합니다.
  • 혹시라도, 문제가 생기면 yt-dlp를지우고 링크 에서 다시 다운로드 하세요.

    yt-dlp -U

아래는 소스코드

param( [string]$Uri, [string]$root_path = "${HOME}\Videos\youtube\live", [int]$time=60 )
[string]$externals = "--cookies `"C:\youtube.com_cookies.txt`" --ffmpeg-location `"C:\ffmpeg\bin`""

[string]$generalOpt = '-f bestvideo+bestaudio/best --merge-output-format mkv --no-part --ignore-errors --skip-unavailable-fragments --hls-use-mpegts --buffer-size 1M --force-overwrites'
[string]$postprocessor = '--postprocessor-args "Merger+ffmpeg_o:-sn -dn -map_metadata -1 -map_chapters -1 -metadata creation_time=now -movflags faststart -vcodec copy -acodec copy"'
[string]$outputOpt = "%(channel)s\%(channel)s-%(release_date>%y%m%d,upload_date>%y%m%d|Unknown)s-%(epoch-3600>%Hh%Mm%Ss)s-%(title)s.%(ext)s"
[string]$property = "${externals} ${generalOpt} --live-from-start ${postprocessor} -o `"${outputOpt}`""

function Invoke-Loop {
  try {
    Clear-Host;
    while (($null -eq $Uri) -or ("" -eq $Uri)) { $Uri = Read-Host "링크 입력 " };
    if ( -not (Test-Path $root_path) ) {New-Item $root_path -Type Directory | Out-Null};  Set-Location $root_path; 
    $i=0; $loop = $true;
    [string]$Live_Uri = $Uri | Get-Info | New-LiveUri
    if ($Live_Uri) {
      Write-Host "유튜브 라이브 전역 링크: ${Live_Uri}"
      Write-Host "`r`nyt-dlp ${property} ${Live_Uri}";
      while($loop) {
        $i++; Write-Host "`r`n`r`n${i}회차 루프"; 
        Start-Process -NoNewWindow -Wait yt-dlp "${property} ${Live_Uri}"
        timeout.exe /NoBreak $time
      }
    }
  } catch { 
    Write-Host "`r`nAn error occurred:`r`n    $($_.Exception.Message)";
    $loop = $false
  } finally { Write-Host "Exit!" }
}

function Get-Info {
  param (
    [CmdletBinding()][Parameter(ValueFromPipeline=$true)]
    [string] $sourceUri = $null
  )
  if ( -not ($sourceUri -match 'youtu') ) {
    $sourceUri = "https://www.youtube.com/@${sourceUri}"
  }
  try {
    $response = Invoke-WebRequest $sourceUri
    if ($response.StatusCode -ge 400) {
      throw "HTTP error occurred. Status code:`r`n    $($response.StatusCode)"
    }
    return $response.Content
  } catch [System.Net.WebException] {
    Write-Host "An error occurred. Error:`r`n    $($_.Exception.Message)"
    return $null
  }
}

function New-LiveUri {
  param (
    [CmdletBinding()][Parameter(ValueFromPipeline=$true)]
    [string]$text
  )
  if ($text -match "/@(\w+)") {
    return "https://www.youtube.com/@$($Matches[1])/live"
  } else { return $null }
}

Invoke-Loop
profile
7r 입니다.

0개의 댓글