-1

enter image description hereCan someone please help me in Downloading a Playlist from YT and compressing them into Audio file please.. I tried with below shown code..

from pytube import YouTube
from pytube import Playlist
from pydub import AudioSegment
import os
import moviepy.editor as mp
import re

YOUTUBE_STREAM_AUDIO = '140' # modify the value to download a different stream
DOWNLOAD_DIR = 'C:\\MyData\\NewAudio'

playlist = Playlist("https://www.youtube.com/watch?v=z9bZufPHFLU&list=PLfqMhTWNBTe0b2nM6JHVCnAkhQRGiZMSJ")

# this fixes the empty playlist.videos list
#playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")

print(len(playlist.video_urls))

for url in playlist.video_urls:
    print(url)

# physically downloading the audio track
for video in playlist.videos:
    audioStream = video.streams.get_by_itag(YOUTUBE_STREAM_AUDIO)
    audioStream.download(output_path=DOWNLOAD_DIR)

    for file in os.listdir(DOWNLOAD_DIR):
        if re.search('mp4', file):
            mp4_path = os.path.join(DOWNLOAD_DIR, file)
            mp3_path = os.path.join(DOWNLOAD_DIR, os.path.splitext(file)[0] + '.mp3')
            new_file = mp.AudioFileClip(mp4_path)
            new_file.write_audiofile(mp3_path)

            os.remove(mp4_path)
Zynk
  • 33
  • 5

1 Answers1

0

If those warnings are preventing your code to function properly... You'll want to install FFMPEG by either:

  • Placing ffmpeg.exe in your project's directory
  • Putting ffmpeg.exe into PATH

You can find downloads for FFMPEG here, and you can find the ffmpeg.exe in the bin folder of the .zip/.7z file downloaded from there.

Pepe Salad
  • 238
  • 2
  • 6