I'm using youtube-dl and Python for download a mp4 video (with sound/audio included); I get the mp4 video, but, I'm unable to set the correct ydl options to get the video with sound.
My goal is to download a video in mp4 format (with quality 240p or 360p) with sound.
The following are the different ydl options I tried - all of these combinations get the video, but, with no sound:
ydl_opts = {'format': 'bestvideo[height<=480]+bestaudio[ext=mp4]/best'}
ydl_opts = {'format': 'bestvideo[height<=480]/best'}
ydl_opts = {'format': 'bestvideo[ext=mp4]+bestaudio[ext=mp4]/mp4+best[height<=480]'}
I tried:
- Change the ydl options using
-f "best[height<=480]"
- code from README.md. - Modify the option as shwon in this question and this answer.
Is there any easier-to-follow documentation for set the ydl options for accomplish the goal described above?
This is my full code:
# Library import:
from yt_dlp import YoutubeDL
# Variables:
amount = 8 # Quantity of videos per page - next, research how to make search pagination.
urlFound = "" # Testing - this is the url of the video to download.
valid_formats = ["133", "134"] # Acceptable formats: 240p or 360p.
# ydl options.
# Sources:
# https://stackoverflow.com/q/60489888/12511801
# https://stackoverflow.com/a/49222737/12511801
#ydl_opts = {'format': 'bestvideo[height<=480]+bestaudio[ext=mp4]/best'}
#ydl_opts = {'format': 'bestvideo[height<=480]/best'}
ydl_opts = {'format': 'bestvideo[ext=mp4]+bestaudio[ext=mp4]/mp4+best[height<=480]'}
# Input the search term:
searchTerm = input("Please enter the search term: ")
# Validate input -> field cannot be empty:
while (len(searchTerm.strip()) == 0):
searchTerm = input("Field cannot be empty - please enter the search term: ")
# Use ydl for search videos:
with YoutubeDL(ydl_opts) as ydl:
try:
info = ydl.extract_info(f"ytsearch{amount}:{searchTerm}", download=False, ie_key="YoutubeSearch")
if (len(info["entries"]) == 0):
print("No results for (" + searchTerm + ")")
else:
print("Checking " + str(len(info["entries"])) + " results: ")
# Loop the results and verify if a valid video is found:
for indx, videoResult in enumerate(info["entries"]):
# Loop for valid format(s) available:
for frm_in_video in videoResult["formats"]:
if (str(frm_in_video['format_id']) in valid_formats):
urlFound = frm_in_video['url']
break # Video found.
# Verify if the video was found:
if (len(urlFound.strip()) == 0):
print("No valid video found - use next page and see more results")
else:
print("This is the valid video: " + urlFound)
except Exception as ex:
print("Error at searching videos. See details bellow")
print(str(ex))