You are not logged in.

#1 2012-03-26 18:55:35

linuxcrow
Member
Registered: 2012-03-23
Posts: 6
Website

Simple Script to Convert mp4 to mp3

#! /bin/bash

track=`echo "$@"`
music='/home/lnk/Music/'
video='/home/lnk/Videos/'

ffmpeg -i "$video$track".mp4 -acodec libmp3lame -ab 160k -ar 44100 -ac 2 "$music$track".mp3

Save the above code using your favorite text editor to 'convert.sh' or whatever you want.
Simply change the video variable from '/home/lnk/Music' to whatever folder you want the final conversion to end up.
Change the video variable from '/home/lnk/Videos/' to whatever folder you have your videos located.

Run

chmod +x convert.sh

Make sure you have 'lame' and 'ffmpeg' installed. Both of these can be found with pacman and installed.

pacman -S lame
pacman -S ffmpeg

When you're ready to do the conversions simple traverse in the terminal to the folder you have your script located and once there type

./convert.sh "filename without extension"

And you're all done. ;)
Hope this helps a few beginners create something useful.

Offline

#2 2012-03-26 19:33:01

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Simple Script to Convert mp4 to mp3

We already have it in the wiki https://wiki.archlinux.org/index.php/FF … ting_audio but thanks anyway :-)

Offline

#3 2012-03-26 22:57:50

VCoolio
Member
From: Netherlands
Registered: 2010-01-05
Posts: 120

Re: Simple Script to Convert mp4 to mp3

Also, to lose the subshell for `echo "$@"` and to enable use of filenames including extension (or script *.mp4 to get all):

#!/bin/bash

target='/home/lnk/Videos/' # remember to end this with /

for i in "$@"; do
    ffmpeg -i "$i" -acodec libmp3lame -ab 160k -ar 44100 -ac 2 "$target${i%*.mp4}".mp3
done

You could also include check for availability of lame and ffmpeg, and give an error if not. There are a lot of converting scripts out there by the way, but if you see this as a learning experience, I hope this helps.

Offline

#4 2012-03-27 08:40:49

linuxcrow
Member
Registered: 2012-03-23
Posts: 6
Website

Re: Simple Script to Convert mp4 to mp3

@VCoolio  I'm not sure I understand your method. The reason I used $@ was because my video names often had spaces and dashes which it mistook for multiple variables. The only way I found to resolve that issue was to assign the full input as a variable. I don't see what a for loop would do here..it's still going to break the words apart and try to find 4 different files that don't exist instead of the full video name right? Can you explain? Also what does the i% section of your code do? I'm just now beginning to learn so i hope you'll understand.

Offline

#5 2012-03-27 16:15:59

VCoolio
Member
From: Netherlands
Registered: 2010-01-05
Posts: 120

Re: Simple Script to Convert mp4 to mp3

The "@" means "all arguments given to the script read separately". So all filenames are read, and if you have given the filename as one argument, so spaces escaped like autocomplete would do or *.mkv, then that stays. So "$@" is what you want

Now we make a loop where we handle each file after another, everytime we call it "i". So the line starting with ffmpeg is done for each file, where file = i.

ffmpeg -i "$i" : this is where we give ffmpeg the input file

"$target${i%*.mp4}".mp3 is where we point to the target folder and target filename. We don't want the output file to be filename.mp4.mp3, so we tell it to use the filename without the ".mp4" part at the end. That's what %*.mp4 does. Check here: http://www.tldp.org/LDP/abs/html/string … ation.html

Last edited by VCoolio (2012-03-27 16:18:19)

Offline

#6 2012-03-27 21:09:11

Rolinh
Member
From: Switzerland
Registered: 2011-05-07
Posts: 144
Website

Re: Simple Script to Convert mp4 to mp3

Funny, I also adapted one not too long ago: http://blog.rolinh.ch/dev/convertir-sim … un-script/
It preserves the tags and offer a possibility to delete original m4a files after the conversion.

Offline

#7 2012-03-28 09:33:02

linuxcrow
Member
Registered: 2012-03-23
Posts: 6
Website

Re: Simple Script to Convert mp4 to mp3

@VCoolio thanks for all the information. Very helpful smile I appreciate you taking the time to explain it.

Offline

Board footer

Powered by FluxBB