I am trying to write a code to download YouTube videos using Pytube on Python 3.6. But for most videos progressive download(Audio and Video in same file) format is available only upto 360p. So I want to download audio and video files separately and combine it. I am able to to download the audio and video files. How can I combine the two file together?

- 14,289
- 18
- 86
- 145

- 145
- 1
- 2
- 6
-
Can you provide some code examples you already tried? Where specifically is your problem? Your question seem s to be too broad to be answered concisely. (https://stackoverflow.com/help/how-to-ask) – Robin B Oct 01 '19 at 13:14
5 Answers
Basically I don't find any method to marge Audio and Video in Pytube
but you can use ffmpeg
for muxing.
First of all you have to install ffmpeg
ffmpeg
installation guide for Windowsfor
Ubuntu
justsudo apt install ffmpeg
Add a dependency ffmpeg-python
a python wrapper of ffmpeg
pip install ffmpeg-python
Now we are ready to go with this code snippet
import ffmpeg
video_stream = ffmpeg.input('Of Monsters and Men - Wild Roses.mp4')
audio_stream = ffmpeg.input('Of Monsters and Men - Wild Roses_audio.mp4')
ffmpeg.output(audio_stream, video_stream, 'out.mp4').run()
for more, ffmpeg-python
API References

- 136
- 3
If you keep getting a video without audio, that's because of the adaptive streaming from pytube. A work-around is to download both video and audio... then merge them with ffpmeg.
For instance, something like this to get both audio and video (audio part adapted from here)
from pytube import YouTube
import os
youtube = YouTube('https://youtu.be/ksu-zTG9HHg')
video = youtube.streams.filter(res="1080p").first().download()
os.rename(video,"video_1080.mp4")
audio = youtube.streams.filter(only_audio=True)
audio[0].download()
and then the ffmpeg part (adapted from both here and here) you can set it up on Windows following this procedure and then run something like
ffmpeg -i video.mp4 -i audio.mp4 -c:v copy -c:a aac output.mp4

- 14,289
- 18
- 86
- 145
Merging audio and video using ffmpeg
Once you have downloaded both video and audio files (‘videoplayback.mp4’ and ‘videoplayback.m4a’ respectively), here’s how you can merge them into a single file:
In case of MP4 format (all, except 1440p 60fps & 2160p 60fps):
ffmpeg -i videoplayback.mp4 -i videoplayback.m4a -c:v copy -c:a copy output.mp4
In case of WebM format (1440p 60fps and 2160p 60fps):
ffmpeg -i videoplayback.webm -i videoplayback.m4a -c:v copy -c:a copy output.mkv
Wait until ffmpeg finishes merging audio and video into a single file named "output.mp4".

- 198
- 1
- 8
How do I convert the downloaded audio file to mp3?
you need to execute the following command in the Command Prompt window:
ffmpeg -i INPUT_FILE -ab BITRATE -vn OUTPUT_FILE
Example:
ffmpeg -i videoplayback.m4a -ab 128000 -vn music.mp3
Example:2 (without bit rate)
ffmpeg -i videoplayback.m4a -vn music.mp3

- 198
- 1
- 8
-
1Please avoid asking queastions in answers, even if the quote the question post or are rhetorical. You risk being misread as asking a question instead of answering, i.e. of havin your question flagged as not an answer. – Yunnosch Apr 12 '21 at 21:35
you can use moviepy
to merge
from pytube import YouTube
from moviepy.editor import VideoFileClip, AudioFileClip, concatenate_videoclips
# download video and audio
yt = YouTube('https://www.youtube.com/watch?v=xxxx')
yt.streams.get_by_itag(135).download(filename="video.mp4")
yt.streams.get_by_itag(140).download(filename="audio.mp4")
# combine the video clip with the audio clip
video_clip = VideoFileClip("video.mp4")
audio_clip = AudioFileClip("audio.mp4")
final_clip = video_clip.set_audio(audio_clip)
final_clip.write_videofile("filename" + ".mp4")

- 308
- 1
- 5