import os

from youtube_dl import YoutubeDL
from youtube_dl import MaxDownloadsReached

def download(url, audio_only):
    """Downloads the youtube video from the url

    Args:
        url: The youtube URL pointing to the video to download.
        audio_only: True if we only want to download the best audio.

    Returns:
        A (file name, video title) tuple.

    The file name is ONLY the file name, and does not include the file path.
    """
    downloader = YoutubeDL()
    downloader.add_default_info_extractors()

    downloader.params['outtmpl'] = os.path.join(os.getcwd(),
                                                'temp/%(id)s.%(ext)s')
    downloader.params['verbose'] = True
    downloader.params['cachedir'] = None
    downloader.params['noplaylist'] = True
    downloader.params['max_downloads'] = 1
    if audio_only:
        downloader.params['format'] = 'bestaudio'

    try:
        info = downloader.extract_info(url)
    except MaxDownloadsReached:
        info = downloader.extract_info(url, download=False)

    file_name = downloader.prepare_filename(info)
    file_name = file_name.encode('ascii', 'ignore')

    title = info.get('title', os.path.basename(file_name))
    return file_name, title

import os

from youtube_dl import YoutubeDL
from youtube_dl import MaxDownloadsReached

def download(url, audio_only):
    """Downloads the youtube video from the url

    Args:
        url: The youtube URL pointing to the video to download.
        audio_only: True if we only want to download the best audio.

    Returns:
        A (file name, video title) tuple.

    The file name is ONLY the file name, and does not include the file path.
    """
    downloader = YoutubeDL()
    downloader.add_default_info_extractors()

    downloader.params['outtmpl'] = os.path.join(os.getcwd(),
                                                'temp/%(id)s.%(ext)s')
    downloader.params['verbose'] = True
    downloader.params['cachedir'] = None
    downloader.params['noplaylist'] = True
    downloader.params['max_downloads'] = 1
    # We don't really have to do this, but YoutubeDL sometimes has a problem
    # combining the video and audio portions of webm files, so this is a good
    # workaround since we really only care about the audio part.
    if audio_only:
        downloader.params['format'] = 'bestaudio'

    try:
        info = downloader.extract_info(url)
    except MaxDownloadsReached:
        info = downloader.extract_info(url, download=False)

    file_name = downloader.prepare_filename(info)
    file_name = file_name.encode('ascii', 'ignore')

    title = info.get('title', os.path.basename(file_name))
    return file_name, title

Add comment explaining why use choose bestaudio for audio downloads.
Add comment explaining why use choose bestaudio for audio downloads.

  import os
  
  from youtube_dl import YoutubeDL
  from youtube_dl import MaxDownloadsReached
  
  def download(url, audio_only):
      """Downloads the youtube video from the url
  
      Args:
          url: The youtube URL pointing to the video to download.
          audio_only: True if we only want to download the best audio.
  
      Returns:
          A (file name, video title) tuple.
  
      The file name is ONLY the file name, and does not include the file path.
      """
      downloader = YoutubeDL()
      downloader.add_default_info_extractors()
  
      downloader.params['outtmpl'] = os.path.join(os.getcwd(),
                                                  'temp/%(id)s.%(ext)s')
      downloader.params['verbose'] = True
      downloader.params['cachedir'] = None
      downloader.params['noplaylist'] = True
      downloader.params['max_downloads'] = 1
+     # We don't really have to do this, but YoutubeDL sometimes has a problem
+     # combining the video and audio portions of webm files, so this is a good
+     # workaround since we really only care about the audio part.
      if audio_only:
          downloader.params['format'] = 'bestaudio'
  
      try:
          info = downloader.extract_info(url)
      except MaxDownloadsReached:
          info = downloader.extract_info(url, download=False)
  
      file_name = downloader.prepare_filename(info)
      file_name = file_name.encode('ascii', 'ignore')
  
      title = info.get('title', os.path.basename(file_name))
      return file_name, title
  
Add comment explaining why use choose bestaudio for audio downloads.
## Code Before:
import os

from youtube_dl import YoutubeDL
from youtube_dl import MaxDownloadsReached

def download(url, audio_only):
    """Downloads the youtube video from the url

    Args:
        url: The youtube URL pointing to the video to download.
        audio_only: True if we only want to download the best audio.

    Returns:
        A (file name, video title) tuple.

    The file name is ONLY the file name, and does not include the file path.
    """
    downloader = YoutubeDL()
    downloader.add_default_info_extractors()

    downloader.params['outtmpl'] = os.path.join(os.getcwd(),
                                                'temp/%(id)s.%(ext)s')
    downloader.params['verbose'] = True
    downloader.params['cachedir'] = None
    downloader.params['noplaylist'] = True
    downloader.params['max_downloads'] = 1
    if audio_only:
        downloader.params['format'] = 'bestaudio'

    try:
        info = downloader.extract_info(url)
    except MaxDownloadsReached:
        info = downloader.extract_info(url, download=False)

    file_name = downloader.prepare_filename(info)
    file_name = file_name.encode('ascii', 'ignore')

    title = info.get('title', os.path.basename(file_name))
    return file_name, title

## Instruction:
Add comment explaining why use choose bestaudio for audio downloads.
## Code After:
import os

from youtube_dl import YoutubeDL
from youtube_dl import MaxDownloadsReached

def download(url, audio_only):
    """Downloads the youtube video from the url

    Args:
        url: The youtube URL pointing to the video to download.
        audio_only: True if we only want to download the best audio.

    Returns:
        A (file name, video title) tuple.

    The file name is ONLY the file name, and does not include the file path.
    """
    downloader = YoutubeDL()
    downloader.add_default_info_extractors()

    downloader.params['outtmpl'] = os.path.join(os.getcwd(),
                                                'temp/%(id)s.%(ext)s')
    downloader.params['verbose'] = True
    downloader.params['cachedir'] = None
    downloader.params['noplaylist'] = True
    downloader.params['max_downloads'] = 1
    # We don't really have to do this, but YoutubeDL sometimes has a problem
    # combining the video and audio portions of webm files, so this is a good
    # workaround since we really only care about the audio part.
    if audio_only:
        downloader.params['format'] = 'bestaudio'

    try:
        info = downloader.extract_info(url)
    except MaxDownloadsReached:
        info = downloader.extract_info(url, download=False)

    file_name = downloader.prepare_filename(info)
    file_name = file_name.encode('ascii', 'ignore')

    title = info.get('title', os.path.basename(file_name))
    return file_name, title
