Advertisement
Guest User

Untitled

a guest
Jan 20th, 2023
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. from flet import *
  2. from ytmusicapi import YTMusic
  3. from pytube import YouTube
  4. import os
  5. import json
  6.  
  7.  
  8. def main(page:Page):
  9. page.theme_mode = "light"
  10. resultmusic = Column(scroll="auto")
  11. page.scroll="always"
  12. audio1 = Audio(
  13. # THIS SRC CANOT BE BLANK MUST HAVE URL
  14. src="https://luan.xyz/files/audio/ambient_c_motion.mp3",
  15. autoplay=True
  16.  
  17. )
  18. page.overlay.append(audio1 )
  19.  
  20. # FIRST IF YOU SEARCH MUSIC THEN OLD MUSIC CAN BE DELETE
  21. # AND DOWNLOAD NEW MUSIC IN FORLDER MYMUSIC
  22.  
  23. def removesound():
  24. # THIS FOR YOU FOLDER MUSIC AFTER DOWNLOAD FROM YOUTUBE
  25. path = os.path.join(os.getcwd(),"mymusic")
  26. for file in os.listdir(path):
  27. # THEN REMOVE OLD MUSIC
  28. os.remove(os.path.join(path,file))
  29. print("YOU SUCCES DELETE OLD MUSIC !!!!")
  30.  
  31. def playFUN(e):
  32. audio1.resume()
  33. page.update()
  34.  
  35. def pauseFUN(e):
  36. audio1.pause()
  37. page.update()
  38.  
  39. def playmusic(e):
  40. # IF CHANGE YOU MUSIC TO OTHER MUSIC
  41. # THE REMOVE FILE IN FOLDER MYMUSIC AND DOWNLOAD AGAIN
  42. removesound()
  43.  
  44. # ADD EFFECT PROGRESS BAR
  45. page.splash = ProgressBar(
  46. width=400,color="pink",
  47. bar_height=5
  48. )
  49. page.update()
  50.  
  51. # THEN GET VIDEO ID YOUTUBE FROM SUBTITLE OF LISTTILE
  52. videoId = e.control.subtitle.value
  53. videoUrl = "https://youtube.com/watch?v=" + str(videoId)
  54. yt = YouTube(videoUrl)
  55. # AND DOWNLOAD THE VIDEO
  56. stream = yt.streams.filter(only_audio=True,file_extension="mp4").last()
  57. result = stream.download(output_path="mymusic/")
  58.  
  59. # AND IF FINISH DOWNLOAD THEN COPY FILE TO FOLDER MYMUSIC
  60. if not result == "":
  61. print("YOU DOWNLOAD IS FINISHED !!!!!")
  62. # DISABLE PROGRESSBAR
  63. page.splash = None
  64. path = os.getcwd()
  65.  
  66. audio1.src = result
  67. print("YOu audio",audio1.src)
  68.  
  69. # AND PLAY MUSIC
  70. audio1.play()
  71. audio1.autoplay=True
  72. playpausemenu.visible = True
  73. page.update()
  74. page.update()
  75.  
  76.  
  77.  
  78.  
  79.  
  80. def searchsong(e):
  81. removesound()
  82. result = YTMusic().search(query=e.control.value,filter="songs")
  83. try:
  84. # THIS SCRIPT IS AUTO CREATE FOLDER MYMUSIC IF NOT CREATED
  85. # MANUAL
  86. os.makedirs("mymusic")
  87. print("MY MUSIC FOLDER CREATED")
  88. except FileExistsError:
  89. print("FOLDER MY MUSIC ALREADY CREATED")
  90.  
  91. # IF FOUND THEN PUSH RESULT TO WIDGET resultmusic
  92. if not result == "":
  93. for x in result:
  94. resultmusic.controls.append(
  95. ListTile(
  96. leading=Image(
  97. src=x['thumbnails'][1]['url'],
  98. fit="cover",
  99. width=120,
  100. height=120
  101. ),
  102. title=Column([
  103. Text(x['title'],size=20,weight="bold"),
  104. Text(x['duration'],size=15),
  105. ]),
  106. subtitle=Text(x['videoId'],size=15),
  107.  
  108. # AND IF CLICK THIS LISTTILE THEN RUN FUNCTION
  109. on_click=lambda e:playmusic(e)
  110. )
  111. )
  112. page.update()
  113.  
  114.  
  115.  
  116.  
  117. txtsearch = TextField(
  118. label="search music here",
  119. on_submit=lambda e:searchsong(e)
  120. )
  121.  
  122.  
  123. appBar = AppBar(
  124. bgcolor="yellow",
  125. toolbar_height=150,
  126. title=Column([
  127. Text("THE MUSIC",size=30),
  128. txtsearch
  129. ])
  130.  
  131. )
  132. playpausemenu = Container(
  133. bgcolor="yellow",
  134. border_radius=30,
  135. top=50,
  136. right=30,
  137. content=Row([
  138. IconButton(icon="play_circle_rounded",
  139. icon_size=90,
  140. on_click=playFUN
  141. ),
  142. IconButton(icon="pause_circle_rounded",
  143. icon_size=90,
  144. on_click=pauseFUN
  145. ),
  146.  
  147. ],alignment="center")
  148.  
  149. )
  150.  
  151. # HIDE THE PLAY PAUSE BEACAUSE NO MUSIC IS PLAY
  152. playpausemenu.visible = False
  153. page.update()
  154. page.add(
  155. appBar,
  156. Stack([
  157. resultmusic,
  158. playpausemenu
  159.  
  160. ])
  161.  
  162. )
  163. flet.app(target=main,assets_dir="mymusic",view=WEB_BROWSER)
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement