Skip to content

Instantly share code, notes, and snippets.

@dipanjal
Created November 7, 2019 02:01
Show Gist options
  • Save dipanjal/1a53fd41fea4a66d1063d74be794b4e5 to your computer and use it in GitHub Desktop.
Save dipanjal/1a53fd41fea4a66d1063d74be794b4e5 to your computer and use it in GitHub Desktop.
Downloads 1080 video and 128Kbps audio form youtube separately and combine them afterwords.
from pytube import YouTube
import sys
import time
# from ffmpeg import _ffmpeg as ffmpeg
import ffmpeg
class YoutubeDL:
_video = None
_audio = None
def progress_func(self, stream, chunk, file_handle, bytes_remaining):
bar_length = 20
percent = (1 - bytes_remaining / self._video.filesize)
hashes = '=' * int(round(percent * bar_length))
spaces = ' ' * (bar_length - len(hashes))
progress = round(percent * 100, 3)
sys.stdout.write('\rProgress: [{0}] {1}%'.format(hashes + spaces, progress))
sys.stdout.flush()
def cli_progress_test(self, end_val, bar_length=20):
for i in range(0, end_val):
percent = float(i) / end_val
hashes = '#' * int(round(percent * bar_length))
spaces = ' ' * (bar_length - len(hashes))
sys.stdout.write("\rPercent: [{0}] {1}%".format(hashes + spaces, int(round(percent * 100))))
sys.stdout.flush()
time.sleep(1)
def __init__(self):
try:
yt = YouTube('https://www.youtube.com/watch?v=C9LPQmkao8c', on_progress_callback=self.progress_func)
streams = yt.streams
self._video = streams.filter(adaptive=True, file_extension='mp4', type='video').first()
self._audio = streams.filter(adaptive=True, file_extension='mp4', type='audio').first()
# print('downloading video:', yt.title+"_video")
# self._video.download()
# print('\nTask Completed!')
# print('downloading audio:', yt.title)
# self._audio.download(filename=yt.title + "_audio")
# print('\nTask Completed!')
# mixing audio and video file
video_input = ffmpeg.input('Of Monsters and Men - Wild Roses.mp4')
audio_input = ffmpeg.input('Of Monsters and Men - Wild Roses_audio.mp4')
ffmpeg.output(audio_input, video_input, 'out.mp4').run()
# ffmpeg.output(self._audio, self._video, 'out.mp4').run()
except Exception as ex:
print(ex) # to handle exception
YoutubeDL()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment