Page 1 of 5 123 ... LastLast
Results 1 to 10 of 46
  1. #1
    Join Date
    Jan 2012
    Posts
    124
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Rep Power
    2

    Converting YouTube to MP3

    Everyone's been in this situation at least one time. You find a song on YouTube love, want to download it to keep it for future uses only to find there's no real way. There's those websites that convert videos for you, but out of the few that actually work they place restrictions on you too. Plus, what's the fun in letting someone else do the work for you? I mean, we are using Linux after all.

    Here in this article I'll show you how to do this yourself using only two programs: youtube-dl and ffmpeg. While I know youtube-dl can convert to MP3, I prefer to use ffmpeg...plus, I don't think youtube-dl can do it on its own anyways, so we'll do it the long and painful way, with a script to automate the process in the end.

    Software Required
    As said, we only need two pieces of software to do this: youtube-dl and ffmpeg. Youtube-dl can be found at http://rg3.github.com/youtube-dl/ while ffmpeg can be found at http://ffmpeg.org/. You can also find these in your repo most likely (ffmpeg is a pretty popular multimedia library).

    Note that if you receive this message when trying to download YouTube videos: "ERROR: no fmt_url_map or conn information found in video info", Kevin108 resolved this issue by installing youtube-dl via GitHub: http://rg3.github.com/youtube-dl/download.html (Linux Mint 11 has an old version in their repos).

    Getting the YouTube Video

    Since we're going for audio-only, and I want to make this article kid-friendly, we'll use this dubstep song: http://www.youtube.com/watch?v=30amRuYU7dw

    The first thing we're going to discuss about this program here is the format of the video we are going to download (see "-f" switch in the man page). The available options for YouTube videos are:
    WebM video at 480p: 43
    WebM video at 720p: 45
    H264 video in MP4 container at 480p: 18
    H264 video in MP4 container at 720p: 22
    H264 video in MP4 container at 1080p: 37
    H264 video in FLV container at 360p: 34
    H264 video in FLV container at 480p: 35
    H263 video at 240p: 5
    3GP video: 17
    Personally I'm a fan of 720p, and while ultimately it doesn't matter for audio (the quality is going to be the same regardless), if you want to keep the video downloaded for future use may as well make it a format you enjoy.

    Next, we'll want to keep it fancy-ish looking so why not use the title of the video as the filename? For that we'll be using the "-t" switch. The only caveat to this is that it includes the video ID in the name as well...and since I'm not a fan of hacking of sed lines all that well, I'll leave that up to you for homework.

    Time To Download
    Running the youtube-dl command is pretty straight forward, with the output looking like this:
    Code:
    $ youtube-dl -f 22 -t http://www.youtube.com/watch?v=30amRuYU7dw
    [youtube] Setting language
    [youtube] 30amRuYU7dw: Downloading video webpage
    [youtube] 30amRuYU7dw: Downloading video info webpage
    [youtube] 30amRuYU7dw: Extracting video information
    [download] Destination: X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4
    [download] 100.0% of 29.51M at  853.20k/s ETA 00:00 
    $
    The "Destination" line will tell you the name of your video file.

    Time To Convert
    For ffmpeg we are going to get a little more advanced. We'll be using the "-i" switch for the input file (the above .mp4 destination file). Then we tell ffmpeg we ant an MP3 file with "-f mp3". Since 192 bitrate is about good for most people (which you can change if you want), we'll use an adjustable bitrate of 192k (-ab 192000). We don't want to record any video so we use "-vn" (ffmpeg's switch to disable video recording), and then give the output filename.

    Side note If you're using Ubuntu (or a flavor of it), you'll need to install libavcodec-extra-53 to get MP3 support. You can use ogg if you want (should be built into ffmpeg), but I'm not a fan of the format personally.

    The output of ffmpeg is big, so I'm going to truncate it down to the most important parts:
    Code:
    ffmpeg -i X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4 -f mp3 -ab 192000 -vn X_Sentinel_The_Unmaker_Dance_Electro.mp3
    ...
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'X_Sentinel_The_Unmaker_Dance_Electro-30amRuYU7dw.mp4':
      Metadata:
        major_brand     : mp42
        minor_version   : 0
        compatible_brands: isommp42
        creation_time   : 2011-10-19 18:20:28
      Duration: 00:03:30.00, start: 0.000000, bitrate: 1178 kb/s
        Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720, 1020 kb/s, 30 fps, 30 tbr, 1k tbn, 60 tbc
        Metadata:
          creation_time   : 1970-01-01 00:00:00
        Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 151 kb/s
        Metadata:
          creation_time   : 2011-10-19 18:20:28
    Output #0, mp3, to 'X_Sentinel_The_Unmaker_Dance_Electro.mp3':
      Metadata:
        major_brand     : mp42
        minor_version   : 0
        compatible_brands: isommp42
        TDEN            : 2011-10-19 18:20:28
        TSSE            : Lavf53.3.0
        Stream #0.0(und): Audio: libmp3lame, 44100 Hz, stereo, s16, 192 kb/s
        Metadata:
          creation_time   : 2011-10-19 18:20:28
    Stream mapping:
      Stream #0.1 -> #0.0
    Press ctrl-c to stop encoding
    size=    4922kB time=210.00 bitrate= 192.0kbits/s    
    video:0kB audio:4922kB global headers:0kB muxing overhead 0.003095%
    $
    Script
    I started you off here on how to strip out the video ID and such.

    PHP Code:
    #!/bin/sh

    ID="$1"

    if [ -"$ID]; then
            
    echo "$0 <YouTube video ID>"
            
    echo "\tExample: $0 5V5V5V-4V"

            
    exit 1
    fi

    YT
    ="http://www.youtube.com/watch?v=$ID"

    MP4=`youtube-dl -f 22 -t $YT | grep "Destination:" | awk '{print $3}'`
    #MP3=`echo "$MP4" | sed 's/.{$ID}//g'`

    ffmpeg -"$MP4-f mp3 -ab 192000 -vn "$MP4.mp3"

    echo "YouTube Link: $YT"
    echo "Downloaded video to file: $MP4"
    #echo "MP3 file: $MP3" 
    Outro
    I'll be writing up some more security articles soon. I wanted to pass this out there though for a breath of fresh air, and because I got annoyed with all of those converter websites that didn't do anything.
    Last edited by ehansen; 02-10-2012 at 09:24 AM. Reason: Added Kevin108's resolution to a download error
    Information Server Management
    Linux server management, PCI consultation and affordable web hosting.

    Security For Us - Where security works for you

    Providing server security and PCI compliance for individuals and businesses.




  2. #2
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I'm running Mint 11 and installing youtube-dl from the repo gives you an older version. For me, every video generated the error "ERROR: no fmt_url_map or conn information found in video info" I grabbed the latest version from the youtube-dl github and everything's good to go. (Can't post a link in my first post. DOH!)

  3. #3
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

  4. #4
    Join Date
    Oct 2011
    Location
    Dublin, PA
    Posts
    381
    Thanks
    33
    Thanked 18 Times in 16 Posts
    Rep Power
    10
    kevin,

    Sorry about that - spam control

    Welcome to the forum!

  5. #5
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    I've run a couple forums over the years. It made sense to me! I really enjoyed the puzzle piece captcha. Thanks for the welcome!

  6. #6
    Join Date
    Jan 2012
    Posts
    124
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Rep Power
    2
    Quote Originally Posted by Kevin108 View Post
    I'm running Mint 11 and installing youtube-dl from the repo gives you an older version. For me, every video generated the error "ERROR: no fmt_url_map or conn information found in video info" I grabbed the latest version from the youtube-dl github and everything's good to go. (Can't post a link in my first post. DOH!)
    Thanks Kevin for pointing this out! Its always difficult when advising to use repo's for installs because you never know what can happen.

    I'll update the post with this information.
    Information Server Management
    Linux server management, PCI consultation and affordable web hosting.

    Security For Us - Where security works for you

    Providing server security and PCI compliance for individuals and businesses.

  7. #7
    Join Date
    Feb 2012
    Posts
    90
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    2
    Jdownloader can do this properly, the only thing you need to to is add the url to the program. It will offer you to download the movie in different formats (including mp3, which is audio-only).

  8. #8
    Join Date
    Jan 2012
    Posts
    124
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Rep Power
    2
    Quote Originally Posted by carbon333 View Post
    Jdownloader can do this properly, the only thing you need to to is add the url to the program. It will offer you to download the movie in different formats (including mp3, which is audio-only).
    You are right, but the problem with this is that it requires Java, which can burden your system. If you want more of an automatic downloader (or have a system that isn't bogged down by Java) then JDownloader can definitely be a choice. However, most netbooks and such take a performance hit so this is a less resource-intensive approach.
    Information Server Management
    Linux server management, PCI consultation and affordable web hosting.

    Security For Us - Where security works for you

    Providing server security and PCI compliance for individuals and businesses.

  9. #9
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    What does this error indicate?

    Code:
    youtube-dl -f 22 -t <url_to_video>
    [youtube] Setting language
    [youtube] S4pWjpaSgpQ: Downloading video webpage
    [youtube] S4pWjpaSgpQ: Downloading video info webpage
    [youtube] S4pWjpaSgpQ: Extracting video information
    ERROR: requested format not available
    Glenn
    The Bassinator © ®
    Powered by Fedora 16 x86_64 or Arch Linux x86_64

    Laptop: Toshiba Satellite / Intel Core 2 Duo 1.73 GHz / 2GB / 160GB / Intel Mobile 945GM/GMS/GME/943/940GML Integrated Graphics
    Desktop: BioStar MCP6PB M2+ / AMD Phenom 9750 Quad Core / 2GB / 1TB SATA / 500GB SATA / NVidia GeForce 6150SE nForce

  10. #10
    Join Date
    Feb 2012
    Posts
    90
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Rep Power
    2
    Try changing -f parameter according to the list from the first post.

 

 

Similar Threads

  1. Replies: 0
    Last Post: 01-27-2012, 01:00 PM
  2. Replies: 0
    Last Post: 01-18-2012, 11:07 PM
  3. Replies: 0
    Last Post: 12-30-2011, 10:06 PM
  4. Replies: 1
    Last Post: 12-08-2011, 03:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
           








Check out Linux Central for Linux software and other goodies!





» Stats

Members: 3,530
Threads: 3,911
Posts: 9,418
Top Poster: Fred (1,486)
Welcome to our newest member, Chrissy Howard

» Links



Powered by vBadvanced CMPS