Search code examples
pythonmoviepypytube

Code works on Pycharm but gets blocked in the console


I've coded a python Youtube to MP3 tool to download youtube mp3s using a keystroke, here's my code:

from pytube import YouTube
from moviepy.editor import *
import os, shutil
import keyboard as kb

def keyboard():
    if kb.is_pressed("ctrl+i"):
        try:
            dl()
        except:
            pass


def dl():
    print("Downloading...")
    win32clipboard.OpenClipboard()
    url = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()

    mp4 = YouTube(url).streams.get_highest_resolution().download()
    mp3 = mp4.split(".mp4", 1)[0] + ".mp3"

    video_clip = VideoFileClip(mp4)
    audio_clip = video_clip.audio
    audio_clip.write_audiofile(mp3)

    audio_clip.close()
    video_clip.close()

    os.remove(mp4)
    shutil.move(mp3, r"C:\Users\eghos\Documents\YouTubeDownloads0")


while True:
    keyboard()

When I run it on Pycharm, it works well but when I run it in the terminal, it gets blocked after closing the clipboard. Thank you for your help


Solution

  • Have you tried running cmd as administrator?