Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flet import *
- from ytmusicapi import YTMusic
- from pytube import YouTube
- import os
- import json
- def main(page:Page):
- page.theme_mode = "light"
- resultmusic = Column(scroll="auto")
- page.scroll="always"
- audio1 = Audio(
- # THIS SRC CANOT BE BLANK MUST HAVE URL
- src="https://luan.xyz/files/audio/ambient_c_motion.mp3",
- autoplay=True
- )
- page.overlay.append(audio1 )
- # FIRST IF YOU SEARCH MUSIC THEN OLD MUSIC CAN BE DELETE
- # AND DOWNLOAD NEW MUSIC IN FORLDER MYMUSIC
- def removesound():
- # THIS FOR YOU FOLDER MUSIC AFTER DOWNLOAD FROM YOUTUBE
- path = os.path.join(os.getcwd(),"mymusic")
- for file in os.listdir(path):
- # THEN REMOVE OLD MUSIC
- os.remove(os.path.join(path,file))
- print("YOU SUCCES DELETE OLD MUSIC !!!!")
- def playFUN(e):
- audio1.resume()
- page.update()
- def pauseFUN(e):
- audio1.pause()
- page.update()
- def playmusic(e):
- # IF CHANGE YOU MUSIC TO OTHER MUSIC
- # THE REMOVE FILE IN FOLDER MYMUSIC AND DOWNLOAD AGAIN
- removesound()
- # ADD EFFECT PROGRESS BAR
- page.splash = ProgressBar(
- width=400,color="pink",
- bar_height=5
- )
- page.update()
- # THEN GET VIDEO ID YOUTUBE FROM SUBTITLE OF LISTTILE
- videoId = e.control.subtitle.value
- videoUrl = "https://youtube.com/watch?v=" + str(videoId)
- yt = YouTube(videoUrl)
- # AND DOWNLOAD THE VIDEO
- stream = yt.streams.filter(only_audio=True,file_extension="mp4").last()
- result = stream.download(output_path="mymusic/")
- # AND IF FINISH DOWNLOAD THEN COPY FILE TO FOLDER MYMUSIC
- if not result == "":
- print("YOU DOWNLOAD IS FINISHED !!!!!")
- # DISABLE PROGRESSBAR
- page.splash = None
- path = os.getcwd()
- audio1.src = result
- print("YOu audio",audio1.src)
- # AND PLAY MUSIC
- audio1.play()
- audio1.autoplay=True
- playpausemenu.visible = True
- page.update()
- page.update()
- def searchsong(e):
- removesound()
- result = YTMusic().search(query=e.control.value,filter="songs")
- try:
- # THIS SCRIPT IS AUTO CREATE FOLDER MYMUSIC IF NOT CREATED
- # MANUAL
- os.makedirs("mymusic")
- print("MY MUSIC FOLDER CREATED")
- except FileExistsError:
- print("FOLDER MY MUSIC ALREADY CREATED")
- # IF FOUND THEN PUSH RESULT TO WIDGET resultmusic
- if not result == "":
- for x in result:
- resultmusic.controls.append(
- ListTile(
- leading=Image(
- src=x['thumbnails'][1]['url'],
- fit="cover",
- width=120,
- height=120
- ),
- title=Column([
- Text(x['title'],size=20,weight="bold"),
- Text(x['duration'],size=15),
- ]),
- subtitle=Text(x['videoId'],size=15),
- # AND IF CLICK THIS LISTTILE THEN RUN FUNCTION
- on_click=lambda e:playmusic(e)
- )
- )
- page.update()
- txtsearch = TextField(
- label="search music here",
- on_submit=lambda e:searchsong(e)
- )
- appBar = AppBar(
- bgcolor="yellow",
- toolbar_height=150,
- title=Column([
- Text("THE MUSIC",size=30),
- txtsearch
- ])
- )
- playpausemenu = Container(
- bgcolor="yellow",
- border_radius=30,
- top=50,
- right=30,
- content=Row([
- IconButton(icon="play_circle_rounded",
- icon_size=90,
- on_click=playFUN
- ),
- IconButton(icon="pause_circle_rounded",
- icon_size=90,
- on_click=pauseFUN
- ),
- ],alignment="center")
- )
- # HIDE THE PLAY PAUSE BEACAUSE NO MUSIC IS PLAY
- playpausemenu.visible = False
- page.update()
- page.add(
- appBar,
- Stack([
- resultmusic,
- playpausemenu
- ])
- )
- flet.app(target=main,assets_dir="mymusic",view=WEB_BROWSER)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement