YouTube 동영상 Python의 제목 추출

Sep 16 2020

YouTube 비디오에서 오디오를 추출하기 위해 파이썬으로 작은 코드를 작성했습니다. 다음은 코드입니다.

from __future__ import unicode_literals
import youtube_dl

link = input("Enter the video link:")
name = input("Enter the title of the video:")

path = f'D:\\{name}.mp3'

ydl_opts = {
    'outtmpl':path,
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([link])

여기서 제가하고 싶은 것은 사용자에게 동영상 제목을 묻는 대신 링크를 사용하여 동영상 제목을 가져 오는 것입니다. 나는 몇 가지 방법을 시도했지만 그들은 나를 위해 작동하지 않았습니다. 이 작업을 수행하는 쉬운 방법이 있습니까? 감사.

답변

1 AlexandraDudkina Sep 16 2020 at 15:44

다음과 같은 방법으로 extract_info 메소드를 사용할 수 있습니다.

with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    info_dict = ydl.extract_info(video, download=False)
    video_title = info_dict.get('title', None)