Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download audio directly via SDK #25057

Closed
rajatjain opened this issue Apr 29, 2020 · 2 comments
Closed

Download audio directly via SDK #25057

rajatjain opened this issue Apr 29, 2020 · 2 comments
Labels

Comments

@rajatjain
Copy link

@rajatjain rajatjain commented Apr 29, 2020

Checklist

  • I'm asking a question
  • I've looked through the README and FAQ for similar questions
  • I've searched the bugtracker for similar questions including closed ones

Question

WRITE QUESTION HERE
Is there a way to download only the audio corresponding to a youtube link via the SDK? I currently use the following code to download the video file and then use other converters to convert them to audio. I couldn't find a way in the source code to directly download in audio form.

def download_video(video_id, target_f):
    params = {
        'nocheckcertificate': True,
        'format': 'mp4',
        'ignoreerrors': True,
        'outtmpl': target_f,
    }

    yt = YoutubeDL(params=params)
    rc = 0
    try:
        rc = yt.download([video_id])
    except Exception as e:
        print("Couldn't download file with id %s. Exception: %s" % (video_id, str(e)))

    if rc != 0:
        return False
    return True

I tried putting in 'format': 'mp3' but it doesn't work.

@rajatjain rajatjain added the question label Apr 29, 2020
@dstftw
Copy link
Collaborator

@dstftw dstftw commented Apr 29, 2020

Read FAQ on format selection.

@dstftw dstftw closed this Apr 29, 2020
@rajatjain
Copy link
Author

@rajatjain rajatjain commented Apr 29, 2020

I tried the following code, but I'm unable to play it on iTunes, nor uses utilities like mutagen or eyed3. Both are unable to load the file.

def download_video(video_id, target_f):
    params = {
        'nocheckcertificate': True,
        'format': 'bestaudio/mpg3',
        'ignoreerrors': True,
        'outtmpl': target_f,
    }

    yt = YoutubeDL(params=params)
    rc = 0
    try:
        rc = yt.download([video_id])
    except Exception as e:
        print("Couldn't download file with id %s. Exception: %s" % (video_id, str(e)))

    if rc != 0:
        return False
    return True

I tried using 'format': 'bestaudio', bestaudio/mp3 but none of them work. I get the following error on the terminal.

$ afinfo ~/tmp/test.mp3
File:           /Users/rjain/tmp/test.mp3
Fail: AudioFileOpenURL failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.