공부

파이썬 pytubefix를 이용한 유튜브 영상, 음원 다운로드 (pytube 오류)

푸쥬 ! 2025. 4. 26. 10:23
반응형

 

오랜만에 pytube로 유튜브 영상을 받아보려했는데

나를 반기는 에러코드가 있었으니..

 

"HTTP Error 400: Bad Request"

 

 

[BUG] I keep getting the issur "An error occurred: HTTP Error 400: Bad Request" and cant downlpoad things · Issue #2044 · pytu

Before creating an issue Please confirm that you are on the latest version of pytube by installing from the source. You can do this by running python -m pip install git+https://github.com/pytube/py...

github.com

 

결론은 pytube -> pytubefix 쓰라는 이야기였다.

패키지 모듈 이름은 다르지만, 문법은 동일하다.

기존 코드에서 pytube를 pytubefix로 교체만 하면 된다.

 

 

 

파이썬 pytube를 이용한 유튜브 영상, 음원 다운로드

pytube를 이용해 유튜브 영상을 다운로드 할 수 있다. 온라인으로 변환하기 귀찮아서 코드를 작성해봤다. 실행환경 Python 3.9 Windows 10 Pytube 설치 pip install pytube ffmpeg 설치 (선택) ffmpeg는 mp4 to mp3 변환

rurup.tistory.com

 

실행환경

 

Python 3.10

Windows 10

 

Pytube 설치

 

pip install pytubefix

 

ffmpeg 설치 (선택)

 

mp3 변환을 위한 모듈이다.

 

아래 링크에서 Windows 버전으로 사용한다.

환경변수를 설정해주면 편하게 사용할 수 있다.

 

 

 

Download FFmpeg

If you find FFmpeg useful, you are welcome to contribute by donating. More downloading options Git Repositories Since FFmpeg is developed with Git, multiple repositories from developers and groups of developers are available. Release Verification All FFmpe

ffmpeg.org

 

영상만 필요하면 설치하지 않아도 된다.

 

영상 다운로드 및 MP3 변환 코드

 

from pytubefix import YouTube
from pytubefix.cli import on_progress
import os
import subprocess

url = "https://www.youtube.com/watch?v=TnK5aAXuJU4"

yt = YouTube(url, on_progress_callback=on_progress)

print(f'==================================================================')
print(f'* 제작자\t: {yt.author}')
print(f'* 채널 ID\t: {yt.channel_id}')
print(f'* 채널 URL\t: {yt.channel_url}')
print(f'==================================================================')
print(f'* 영상 ID\t: {yt.video_id}')
print(f'* 영상 URL\t: {yt.watch_url}')
print(f'* 썸네일 URL\t: {yt.thumbnail_url}')
print(f'* 나이 제한\t: {yt.age_restricted}')
print(f'* 영상 제목\t: {yt.title}')
print(f'* 영상 길이\t: {str(yt.length // 60).zfill(2)}:{str(yt.length % 60).zfill(2)} / {yt.length} 초.')
print(f'* 영상 조회수\t: {yt.views}')
print(f'==================================================================')
print(f'* 영상 키워드\t: {yt.keywords}\n')
print(f'* 영상 설명\n\n{yt.description}')
print(f'==================================================================')


ytvid = yt.streams.all()

for i in range(len(ytvid)):
    print(f'{ytvid[i]}')

itagnum = int(input("다운로드 itag 번호 입력 : "))
dpath = "D:\TEST"  # 자유롭게 변경

#오디오 파일만 다운로드

yt.streams.filter(only_audio=True)
yt.streams.get_by_itag(itagnum).download(dpath)

#비디오 다운로드 아래 코드 이용
'''
yt.streams.filter(file_extension='mp4')
yt.streams.get_by_itag(itagnum).download(dpath)
'''

# MP4 -> MP3 변환 (선택)
'''
dfile = yt.streams.get_by_itag(itagnum).default_filename
subprocess.call(["ffmpeg", '-i', os.path.join(dpath, dfile), os.path.join(dpath, dfile[:-3]+'mp3')])
'''

 

상황에 맞게 필요한 코드를 사용하면 된다.

pytube 때와 코드가 같아서 사용하기 편하다.

기타 가이드가 필요하면 아래 문서 참고.

 

https://pytubefix.readthedocs.io/

 

 

반응형