from __future__ import unicode_literals
import youtube_dl



def my_hook(d):
    if d['status'] == 'finished':
        print('Done downloading, now converting ...')


ydl_opts = {
    'format': 'bestaudio/best',
#     'download_archive': 'downloaded_songs.txt',
#     'outtmpl': '%(title)s.%(ext)s',
    'outtmpl': 'sample.mp3',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
    'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=2IkoKhr6Tss'])
    
#     music_file ="sample.mp3"   # mp3 or mid file
#     
#     import sounddevice as sd
#     from scipy.io.wavfile import write
#     import random
#     import pygame
#     import time
#     
#     pygame.init()
#     pygame.mixer.init(devicename='CABLE Input (VB-Audio Virtual Cable)')
#     
#     fs = 44100  # Sample rate
#     seconds = 00.1  # Duration of recording
# 
#     myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
#     sd.wait()  # Wait until recording is finished
#     
#     write(f'./sample3.mp3', fs, myrecording)
#     pygame.mixer.music.load(f'./sample.mp3') #Load the mp3  
#     pygame.mixer.music.play() #Play it
#     time.sleep(00.1)

