Forum Archive

Youtube_dl extract mp3 file

Drizzel

Good day,
I just copied a simple code from a YouTube tutorial that is supposed to download the mp3 file of any YouTube video. Here’s my code:

from __future__ import unicode_literals
import youtube_dl
import os
from sys import argv

# Download data and config

download_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(title)s.%(ext)s',
    'nocheckcertificate': True,
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

# Song Directory
if not os.path.exists('Songs'):
    os.mkdir('Songs')
else:
    os.chdir('Songs')

# Download Songs
with youtube_dl.YoutubeDL(download_options) as dl:
    with open('../' + 'songs.txt', 'r') as f:
        for song_url in f:
            dl.download([song_url])

The issue is the line key': 'FFmpegExtractAudio',
Kassenerfolg it is because I do not have Libav installed. Stash doesn’t find the package release urls. Does anyone know how to fix this?
Thanks a lot

JonB

Cant use ffmpeg on ios.
But I think you ought to be able to download the m4a without converting. Try getting rid of those post processing steps.

I think iOS supports m4a audio directly.

Drizzel

Great thanks a lot,
I made it work. The only issue is that the downloaded files are 7 min long, whereas the Original Video is about 3-4 min long. Therefore he file has about 3 min without any sound. Any idea why?

from __future__ import unicode_literals
import youtube_dl
import os
from sys import argv



# Download data and config

download_options = {
    'format': 'm4a',
    'outtmpl': '%(title)s.%(ext)s',
    'nocheckcertificate': True,
    'preferredcodec': 'best',
}

# Song Directory
if not os.path.exists('Songs'):
    os.mkdir('Songs')
else:
    os.chdir('Songs')

# Download Songs
with youtube_dl.YoutubeDL(download_options) as dl:
    with open('../' + 'songs.txt', 'r') as f:
        for song_url in f:
            dl.download([song_url])

print('done')
donaldcassey

The same code work for converting soundcloud to mp3 file?

Drizzel

@donaldcassey yes it should work with soundcloud. I didn’t test it, but the module contains a file for extracting data from soundcloud. If you want to use this, just copy the code into a new directory and create a file called ‘songs.txt’ . Then paste every link into a new line in the text file. The code does not work if the file contains empty lines, but you can fix that with by replacing

dl.download([song_url]) 

with

if not “\n” in song_url: dl.download([song_url])
happywheelsgame

You try to make me cry but I cried to much. U want me to suffer & breaking heart. happy wheels

JerrySmith1021

If you are a common user want to download YouTube music to MP3 but not familiar with these codes, don't worry. This tutorial will provide another way to extract audio from YouTube videos. In addition, it's very easy to operate for new users.

Blackshady

Save the hassle, and use Joyoshare Audio Recorder to extract the audio tracks from the video.

Bella Gorden

To convert YouTube video to MP3, you could take TunesKit Audio Capture into consideration. It should be a top-notch audio recorder that enables you to download and convert all types of videos or audios from streaming music services like YouTube, radios, game music, movies, and more to a series of plain audio formats like MP3 with lossless quality and complete ID3 tags.

Miyatehe

The command-line based FFmpeg is complicated for me. I have tried yet failed. As a result, I change to use an easy alternative - Joyoshare Media Cutter to extract MP3 from any YouTube video. This tool is easy to use and allows me to extract audio from YouTube video to MP3, OGG, MKA, AC3, AAC, FLAC, etc. with customizable quality.

Jacquelinana

Recently I find 2Conv more easy-to-use than YouTube_dl. It is able to download YouTube videos and save as MP3, MP4 and AVI easily. Sometimes I would also like to use Joyoshare VidiKit as a screen recorder to record whatever I like in the Internet. I am glad to share them here as alternatives and wish you a happy weekend ahead.