Reputation: 21
I am working on a kivy based application Which can download youtube videos using the pytube Api I am able to download the videos in all resolutions using pytube only But when I Put this logic into the Kivy application I am able to download only 360p with both video and audio. If I change to any other resolution it gets downloaded but there is no audio. Please Help me How to solve the problem
My code only with Pytube
from pytube import YouTube
url = input('Enter the link to Link download')
type1 = int(input('1.Video\n2.Audio'))
mime_type = ''
if type1 == 1:
mime_type = 'video'
elif type1 == 2:
mime_type = 'audio'
else:
print('Please select appropriatly')
exit()
type2 = int(input('1.mp4\n2.webm'))
if type2 == 1:
mime_type += '/mp4'
elif type2 == 2:
mime_type += '/webm'
else:
print('Please select appropriatly')
exit()
name = input('Name Of the file? : ')
try:
yt = YouTube(url)
except:
print('Couldnt connect')
exit()
if type1 == 2:
stream = yt.streams.filter(mime_type=mime_type).first()
stream.download(filename=name)
print("Download successfull")
exit()
def resolution():
res_query = int(input('Select Resolution\n1.144p\n2.240p\n3.360p\n4.480p\n5.720p\n6.1080p'))
res = ''
if res_query == 1:
res = '144p'
elif res_query == 2:
res = '240p'
elif res_query == 3:
res = '360p'
elif res_query == 4:
res = '480p'
elif res_query == 5:
res = '720p'
elif res_query == 6:
res = '1080p'
else:
print('Enter appropriate resolution')
exit()
res = resolution()
stream = yt.streams.filter(mime_type=mime_type, res=res)
if stream == []:
print('Resolution of {} is not availible for download\nPlease try with different resolution')
res = resolution()
else:
stream.first().download(filename=name)
print("Download successfull")
My Youtube downloader using kivy Gui
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty
from pytube import YouTube
class Option(Screen):
def on_pre_enter(self, *args):
Window.clearcolor = (0.15, 0.15, 1, 1)
class Video(Screen):
url = ObjectProperty(None)
res=ObjectProperty(None)
filename = ObjectProperty(None)
def on_pre_enter(self, *args):
Window.clearcolor = (1, 1, 1, 1)
def Download(self):
print(self.url.text)
print(self.res.text)
mime_type = 'video/mp4'
yt=YouTube(self.url.text)
stream = yt.streams.filter(mime_type=mime_type, res=self.res.text).first()
stream.download(filename=self.filename.text)
print('Download Done')
class Audio(Screen):
url = ObjectProperty(None)
filename=ObjectProperty(None)
def on_pre_enter(self, *args):
Window.clearcolor = (1, 1, 1, 1)
def Download(self):
print(self.url.text)
mime_type = 'audio/mp4'
yt = YouTube(self.url.text)
stream = yt.streams.filter(mime_type=mime_type).first()
stream.download(filename=self.filename.text)
class Manager(ScreenManager):
pass
sm = ScreenManager()
kv = Builder.load_file('You.kv')
class YouApp(App):
def build(self):
Window.size = (360, 600)
self.icon = r'icon.png'
sm.add_widget(Option())
sm.add_widget(Video())
sm.add_widget(Audio())
return sm
YouApp().run()
My kv file
<Manager>:
Option:
id:option
name:'option'
Audio:
id:audio
name:'audio'
Video:
id:video
name:'video'
<Option>
name:'option'
Widget:
size_hint:[0.3,0.2]
pos_hint:{'center_x':0.5,'top':0.92}
canvas:
Ellipse:
size:self.size
pos:self.pos
source:'icon.png'
GridLayout:
rows:2
size_hint:[0.8,0.27]
pos_hint:{'center_x':0.5,'center_y':0.5}
Button:
text:" Download Video"
background_color:204/255,229/255,1,1
on_press:
app.root.transition.direction='left'
app.root.current='video'
Button:
text:"Download Audio"
background_color:204/255,229/255,1,1
on_press:
app.root.transition.direction='right'
app.root.current='audio'
<Video>
name:'video'
res:res
url:url
filename:filename
Widget:
size_hint:[0.3,0.2]
pos_hint:{'center_x':0.5,'top':0.92}
canvas:
Ellipse:
size:self.size
pos:self.pos
source:'icon.png'
GridLayout:
cols:2
spacing:15
size_hint:[0.8,0.25]
pos_hint:{'center_x':0.5,'top':0.6}
Label:
text:'Url : '
color:0,0,0,1
TextInput:
id :url
multiline :False
font_size:20
Label:
text:'Resolution : '
color:0,0,0,1
TextInput:
id :res
text:'360p'
multiline :False
font_size:20
Label:
text:'Video Name '
color:0,0,0,1
TextInput:
id :filename
multiline :False
font_size:20
Button:
text:'Download'
size_hint:[0.8,0.1]
pos_hint:{'center_x':0.5,'top':0.3}
on_press:root.Download()
Button:
text:'Back'
size_hint:[0.8,0.1]
pos_hint:{'center_x':0.5,'top':0.18}
on_press:app.root.current='option'
<Audio>
name:'audio'
url:url
filename:filename
Widget:
size_hint:[0.3,0.2]
pos_hint:{'center_x':0.5,'top':0.92}
canvas:
Ellipse:
size:self.size
pos:self.pos
source:'icon.png'
GridLayout:
cols:2
spacing:15
size_hint:[0.8,0.2]
pos_hint:{'center_x':0.5,'top':0.6}
Label:
text:'Url : '
color:0,0,0,1
TextInput:
id :url
multiline :False
font_size:20
Label:
text:'Audio Name '
color:0,0,0,1
TextInput:
id :filename
multiline :False
font_size:20
Button:
text:'Download'
size_hint:[0.8,0.1]
pos_hint:{'center_x':0.5,'top':0.35}
on_press:root.Download()
Button:
text:'Back'
size_hint:[0.8,0.1]
pos_hint:{'center_x':0.5,'top':0.18}
on_press:app.root.current='option'
I am not Understanding where i am going wrong Please help. ThankYou
Upvotes: 2
Views: 621
Reputation: 461
Since the video file is downloaded successfully, you can download the audio file associated with the video then combine both of them using ffmpeg
Step 1
Install ffmpeg
through this command:
pip install ffmpeg-python
Step 2
Download the audio file using:
yt.streams.filter(only_audio=True).first().download(save_path, f"{title}.mp3")
where yt
is a Youtube
object.
Step 3
Combine video and audio files.
video_file = ffmpeg.input(join(save_path, f"{title}.mp4"))
audio_file = ffmpeg.input(join(save_path, f"{title}.mp3"))
ffmpeg.concat(video_file, audio_file, v=1, a=1).output(
os.path.join(save_path, f"{title} .mp4") #notice the space at the title here because we will delete the original file later
).run()
The original audio and video files are no longer needed. So, you can just delete them.
os.remove(os.path.join(save_path, f"{title}.mp4"))
os.remove(os.path.join(save_path, f"{title}.mp3"))
This will help you download even higher resolutions like 1080 and even 4k.
Upvotes: 0