0

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:

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))
  • your options look good otherwise, but I guess you have a typo. for audio, mp4 should be m4a instead for audio format. try with `bestaudio[ext=m4a]`. – rv.kvetch Jan 17 '22 at 16:19
  • @rv.kvetch thank you. I tried, but, the result is still without audio. I get the url of the video as follows: `https://manifest.googlevideo.com/api/manifest/dash/expire/1642458889/ei/qZrlYYPdBJ-I2LYP28KQgAk/ip/[...]` instead of `https://rr5---sn-5ualdnlr.googlevideo.com/videoplayback?expire=1642458998&ei=FpvlYcmeJJT4hgbv2JeQCQ&ip[...]`. I changed a little the code and I could get the result. I'll answer my own question. Thank you for your help. – Marco Aurelio Fernandez Reyes Jan 17 '22 at 16:40

1 Answers1

1

After more tests, I leave the ydl options as follows:

ydl_opts = {'format': 'bestvideo[ext=mp4]+bestaudio[ext=mp4]/mp4+best[height<=480]'}

Then, I modified further the source code for set in a separate variable the video found.

# Variable that will store the video found. It will be a <dict> type value.
videoFound = None

Next, (once I detect the desired video), I set the value of videoFound created in the previous line:

# 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): 
      # Video found.
      videoFound = videoResult
      break # No need for keep looking further.

And finally, when I verify if any video was found, I read the videoFound variable = which is a dictionary.

# Verify if the video was found: 
if (len(urlFound.strip()) == 0): 
  print("No valid video found - use next page")
else: 
  try: 
    if (videoFound is not None): 
      print("Title: " + videoFound['title'])

      # This is the video - for some reason, 
      # while using (bestaudio[ext=m4a]) - as suggested in the comment - 
      # Link: https://stackoverflow.com/questions/70744328/how-to-specify-ydl-options-for-download-video-with-audio?noredirect=1#comment125064688_70744328
      # the returned URL is a "dash" video, but, with no sound, so, 
      # I rather get the values from the "videoFound" variable: 
      try: 
        print("URL: " + videoFound['url'])
      except Exception as ex: 
        print("This is the valid video: " + urlFound)

      print("Webpage URL: " + videoFound['webpage_url'])
      print("Video ID: " + videoFound['id'])
      print("Duration: " + videoFound['duration_string'])
      print("Published at: " + videoFound['upload_date'])
      print("View count: " + str(videoFound['view_count']))
      print("Uploader: " + videoFound['uploader'])
      print("Channel ID: " + videoFound['channel_id'])
      print("Channel URL: " + videoFound['channel_url'])
      print("Resolution: " + videoFound['resolution'])
    else: 
      # Optional - show the url get in the "urlFound" variable: 
      print("This is the valid video: " + urlFound)
  except Exception as ex: 
    print("Error at getting information from video found:")
    print(str(ex))