Showing posts with label mp3. Show all posts
Showing posts with label mp3. Show all posts

Saturday, 9 December 2017

Speeding up / down audio files

My children want to have some of the songs they listen on the radio in their MP3s.

This is a no-brainer. After they find the videos they want in YouTube, I put all the URLs in a file ('bajar.txt') and can get all the MP3 files with the following command.

youtube-dl -a bajar.txt -x --audio-format mp3 -o '%(title)s.%(ext)s'

But the second step is a bit more fun. One of the MP3s is wrong and it plays the songs a bit slower than it should. Instead of buying a new one, I just speed up the MP3 files in the computer before uploading them to the player. This script will just get all the *mp3 files in a directory, speed them up according to the SP factor, and copy them to the directory 'emma_mp3s', ready to upload to my daughter's player (modifying the IFS variable allows having white spaces in the file names)

#!/bin/bash
# mp3speed.sh                                                                                                                                                          # Adapted from https://ubuntuforums.org/showthread.php?t=1259851                    
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")

FILES="*.mp3"
mkdir -p emma_mp3s
for f in $FILES

do
 AR=96k
 SP=1.08897   # Speed up:  ini_length * (1/speedup) = final_length

 echo Encoding $f

 mplayer -af scaletempo -speed $SP $f -vc null -vo null -ao pcm:fast:waveheader:file=$f.wav

 ffmpeg -i $f.wav -acodec libmp3lame -ab $AR emma_mp3s/$f
 rm $f.wav

done

IFS=$SAVEIFS


Friday, 29 October 2010

Youtube videos to MP3 (or mpeg)

Today I wanted to download the music of a YouTube video, in order to show it to some friends off-line. It turns out that this is quite easy (for reference, this is using Ubuntu 10.04 LTS). (Information consulted: http://hubpages.com/hub/Youtube-to-MP3-on-Ubuntu-Linux and http://lookherefirst.wordpress.com/2007/12/15/how-to-convert-flv-files-to-mpg-or-mp3/).

The first step is to get the aptly named script youtube-dl. The version available in Ubuntu 10.04 is 2010.04.04, but this does not work properly anymore, and it gives the following error when trying to download a video:

angelv~$ youtube-dl http://www.youtube.com/watch?v=BTwhEPiv0U8
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
ERROR: format not available for video
angelv~$


If we download the latest version (2010.10.24) from http://bitbucket.org/rg3/youtube-dl/wiki/Home, and just copy the file to /usr/bin/youtube-dl, then we have no trouble and the video (in .flv format) is saved:

angelv~$ youtube-dl http://www.youtube.com/watch?v=BTwhEPiv0U8
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video webpage
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
[download] Destination: BTwhEPiv0U8.flv
[download] 100.0% of 7.73M at   59.74k/s ETA 00:00

angelv~$ ls -lt *flv
-rw-r--r-- 1 angelv dialout 8100631 2010-10-29 10:01 BTwhEPiv0U8.flv
angelv~$


Then, in order to convert it to an MP3 you will need ffmpeg and the libmp3lame0 library (and in Ubuntu 10.04, perhaps also in others, the package libavcodec-unstripped-52, so that ffmpeg cand find libmp3lame). With this in place, a small script will take care of everything:

#!/bin/bash
x=$RANDOM.flv
youtube-dl --output=$x "$1"
ffmpeg -i $x -ar 44100 -ab 160k -ac 2 "$2"
rm $x


As an example:

angelv~/temp$ yt2mp3 http://www.youtube.com/watch?v=BTwhEPiv0U8 chiclana.mp3
[youtube] Setting language
[youtube] BTwhEPiv0U8: Downloading video webpage
[youtube] BTwhEPiv0U8: Downloading video info webpage
[youtube] BTwhEPiv0U8: Extracting video information
[download] Destination: 17660.flv
[download] 100.0% of 7.73M at   59.97k/s ETA 00:00
FFmpeg version SVN-r0.5.1-4:0.5.1-1ubuntu1, Copyright (c) 2000-2009 Fabrice Bellard, et al.
  configuration: --extra-version=4:0.5.1-1ubuntu1 --prefix=/usr --enable-avfilter --enable-avfilter-lavf --enable-vdpau --enable-bzlib --enable-libgsm --enable-libschroedinger --e\
nable-libspeex --enable-libtheora --enable-libvorbis --enable-pthreads --enable-zlib --disable-stripping --disable-vhook --enable-runtime-cpudetect --enable-gpl --enable-postproc \
--enable-swscale --enable-x11grab --enable-libdc1394 --enable-shared --disable-static
  libavutil     49.15. 0 / 49.15. 0
  libavcodec    52.20. 1 / 52.20. 1
  libavformat   52.31. 0 / 52.31. 0
  libavdevice   52. 1. 0 / 52. 1. 0
  libavfilter    0. 4. 0 /  0. 4. 0
  libswscale     0. 7. 1 /  0. 7. 1
  libpostproc   51. 2. 0 / 51. 2. 0
  built on Mar  4 2010 12:41:55, gcc: 4.4.3

Seems stream 0 codec frame rate differs from container frame rate: 49.99 (4999/100) -> 25.00 (25/1)
Input #0, flv, from '17660.flv':
  Duration: 00:03:24.44, start: 0.000000, bitrate: 253 kb/s
    Stream #0.0: Video: h264, yuv420p, 320x240 [PAR 1:1 DAR 4:3], 253 kb/s, 25 tbr, 1k tbn, 49.99 tbc
    Stream #0.1: Audio: aac, 22050 Hz, stereo, s16
Output #0, mp3, to 'chiclana.mp3':
    Stream #0.0: Audio: libmp3lame, 44100 Hz, stereo, s16, 160 kb/s
Stream mapping:
  Stream #0.1 > #0.0
Press [q] to stop encoding
size=    4003kB time=204.93 bitrate= 160.0kbits/s
video:0kB audio:4003kB global headers:0kB muxing overhead 0.000781%
 

angelv~/temp$ ls -lt *mp3
-rw-r--r-- 1 angelv dialout 4098644 2010-10-29 10:34 chiclana.mp3
angelv~/temp$ 


Despite its name, we can also use the script as-is to convert Youtube videos for example to mpeg format:

angelv@vaso:~$ yt2mp3 http://www.youtube.com/watch?v=McdD9Ng4VnY funny.mpg


UPDATE (10-Apr-2013): With Ubuntu 12.10, youtube-dl works perfect right now to download YouTube videos, and it even has an option to extract automatically the audio, so what I was achieving with this post is now a no-brainer:


angelv@pilas:~$ youtube-dl --extract-audio --audio-format=mp3 http://www.youtube.com/watch?v=suTKOmmoBtw
[youtube] Setting language
[youtube] suTKOmmoBtw: Downloading video webpage
[youtube] suTKOmmoBtw: Downloading video info webpage
[youtube] suTKOmmoBtw: Extracting video information
[download] Destination: suTKOmmoBtw.flv
[download] 100.0% of 13.89M at   80.60k/s ETA 00:00
[avconv] Destination: suTKOmmoBtw.mp3
angelv@pilas:~$