mp4 to mp3 converter
Posted: Mon Nov 18, 2013 7:40 am
I was over at my better half's this weekend and she had a list of what I assumed were music videos she wanted. I broke out youtube-dl built into most if not all Ultimate Edition's and snatched them up. When she awoke I thought she would be plesently surprised only to find what she wanted was mp3's for her phone. Grrr.
No more searching, no more downloading lets rip the audio out of the mp4's was my solution, I have now enhanced it to do it with a GUI (graphical User Interface):
The program:
Majority of my work done with the assistance of our bash builder
Simple and straight forward right? Well yes and no. Some of the files youtube-dl yanked down as can be seen above have spaces and single quotes in thier filenames a true nightmare to program in bash. I resolved the issue by writing a bash completion script, I must say works wonderfully(/etc/bash_completion.d/mp4tomp3):
I deliberately tried to foul up the software by putting the files in a foldername mp:4/ etc. and rolls through them like a champ. Well we are lazy and don't want to be bothered by a terminal right? Enter a zenity script to handle the workload:
Great work huh? Well not good enough for me lets build a postinst script for it and package it up:
Detects if you have nautilus installed or caja etc. and sets up appropriately, let's box it up and ship it:
All in less then a days work. Enjoy, your feedback is appriciated.
TheeMahn
No more searching, no more downloading lets rip the audio out of the mp4's was my solution, I have now enhanced it to do it with a GUI (graphical User Interface):
- Code: Select all
theemahn@JackHammer:~$ mp4tomp3 --help
mp4tomp3 1.0, 11/17/2013
GNU mp4tomp3 home page: <http://ultimateedition.info/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word mp4tomp3 somewhere in the Subject: field.
Usage: mp4tomp3 -<-COMMAND>
Mandatory arguments to long options are identical for short options.
possible commands...
-c --convert convert mp4 to mp3 audio
-h --help this help message
-v --version dump version info
mp4tomp3 --help [COMMAND] for further information.
The program:
#!/bin/bash
# ==============================================================================
# title :mp4tomp3
# description :Converts mp4 videos to mp3 music
# author :theemahn <[email protected]>
# date :11/17/2013
# version :1.0
# usage :mp4tomp3 --help
# manual :man mp4tomp3
# notes :See change-log below for further information.
# ==============================================================================
# Change-log: 1.0: unreleased
# ==============================================================================
PROGNAME="mp4tomp3"
BUILDDATE="11/17/2013"
VERSION="1.0"
#File to process - Grab input before setting IFS tears it apart. Compgen should ensure
#this value is correct.
FTP=$2
# set colors so errors etc. stand out.
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
#Folder executed in.
CURRENT=`pwd`
function Process() {
#App Detection - prefer avconv over depriciated ffmpeg
AVCONV=`which avconv 2>/dev/null`
FFMPEG=`which ffmpeg 2>/dev/null`
if [[ $1 == "" ]]; then
echo -e "${bldgrn}Processing all MP4's in directory $CURRENT${txtrst}"
# Backup of the current IFS. One can never be too cautious.
OLDIFS=$IFS
IFS=:
for file in $(find $directory *.mp4 -type f -printf "%p$IFS")
do
echo -e "${bldgrn}Processing $file in directory $CURRENT${txtrst}"
OUTEXTENSION='.mp3'
INEXTENSION='.mp4'
RAWNAME=${file%.mp4}
if [[ $AVCONV ]]; then
avconv -i $file $RAWNAME$OUTEXTENSION
AC=1
fi
if [[ $FFMPEG && $AC == "" ]]; then
ffmpeg -i $file -f mp3 -ab 192000 -vn $RAWNAME$OUTEXTENSION
fi
done|sort
IFS=$OLDIFS
else
#Process an individual file.
# Backup of the current IFS. One can never be too cautious.
OLDIFS=$IFS
IFS=:
echo -e "${bldgrn}Processing $FTP in directory $CURRENT${txtrst}"
file=$FTP
OUTEXTENSION='.mp3'
INEXTENSION='.mp4'
RAWNAME=${file%.mp4}
if [[ $AVCONV ]]; then
avconv -i $file $RAWNAME$OUTEXTENSION
AC=1
fi
if [[ $FFMPEG && $AC == "" ]]; then
ffmpeg -i $file -f mp3 -ab 192000 -vn $RAWNAME$OUTEXTENSION
fi
IFS=$OLDIFS
fi
exit 0
}
function Help() {
VersionDump
if [[ $1 == "" ]];
then
PRAM="ALL"
else
PRAM=$1
fi
case $PRAM in
ALL)
echo -e "
${bldgrn}Usage: $PROGNAME -<-COMMAND>${txtrst}
Mandatory arguments to long options are identical for short options.
possible commands...
-c --convert convert mp4 to mp3 audio
-h --help this help message
-v --version dump version info
${bldgrn}$PROGNAME --help [COMMAND] for further information.${txtrst}";;
ALL|c|convert)
echo -e "
${bldwht}Usage convert;${txtrst}
${txtgrn}$PROGNAME -c${txtrst}
Converts MP4 to mp3 audio. Not specifying a file will process all mp4's
in the current directory'.";;
ALL|v|version)
echo -e "
${bldwht}Usage version;${txtrst}
${txtgrn}$PROGNAME -v${txtrst}
Displays $PROGNAMEs version number and exits.";;
ALL|h|help|\?)
echo -e "
${bldwht}Useage Help [COMMAND];${txtrst}
${txtgrn}$PROGNAME -h [COMMAND]${txtrst}
Displays this message. For futher information $PROGNAME help [COMMAND]
or refer to the manpages.
man $PROGNAME"
echo -e "${txtgrn}"
echo -e "Example: $PROGNAME -h version"
echo -e "${txtwht}Will display help about the command version${txtrst}"
esac
exit 0
}
function VersionDump {
echo -e "${undwht}$PROGNAME $VERSION, $BUILDDATE${txtrst}
GNU $PROGNAME home page: <http://ultimateedition.info/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word $PROGNAME somewhere in the Subject: field."
}
# Command line pre-processor
case "$1" in
-c|--convert) Process $FTP; exit 0;;
-h|--help|-\?) Help $2; exit 0;;
-v|--version) VersionDump; exit 0;;
*) Help; exit 0;;
esac
Majority of my work done with the assistance of our bash builder
Simple and straight forward right? Well yes and no. Some of the files youtube-dl yanked down as can be seen above have spaces and single quotes in thier filenames a true nightmare to program in bash. I resolved the issue by writing a bash completion script, I must say works wonderfully(/etc/bash_completion.d/mp4tomp3):
# Debian mp4tomp3(8) completion.
have mp4tomp3 &&
_mp4tomp3()
{
dashify()
{
local i
for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
if [ ${#COMPREPLY[i]} -le 2 ]; then
COMPREPLY[i]=-${COMPREPLY[i]}
else
COMPREPLY[i]=--${COMPREPLY[i]}
fi
done
}
local cur cur_nodash prev
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
cur_nodash=${cur#-}
prev=${COMP_WORDS[COMP_CWORD-1][COMP_CWORD-2]}
if [ $COMP_CWORD = 1 ]; then
# first parameter on line
case "$cur" in
-c*)
COMPREPLY=( $( compgen -W 'convert' \
$cur_nodash ) )
dashify
return 0
;;
-*)
COMPREPLY=( $( compgen -W 'convert version help' ${cur_nodash#-} ) )
dashify;
return 0
;;
--*)
COMPREPLY=( $( compgen -W 'convert version help' ${cur_nodash#-} ) )
dashify;
return 0
;;
*)
COMPREPLY=( $( compgen -W 'convert version help' ${cur_nodash#-} ) )
dashify;
return 0
;;
esac
fi
saveIFS=$IFS
IFS=$'\n' # this will allow filenames with spaces (but not filenames with newlines)
toks=( $(compgen -f -- "${cur}" )) # the -- protects against filenames that start with a hyphen
toks=("${toks[@]/%/ }") # add a trailing space to each element
if [ $COMP_CWORD = 2 ]; then
case "${COMP_WORDS[1]}" in
--convert*)
# filename completion only showing mp4s
COMPREPLY=($(compgen -o plusdirs -f -X '!*.mp4' \
-- "${COMP_WORDS[COMP_CWORD]}"))
if [ ${#COMPREPLY[@]} = 1 ]; then
[ -d "$COMPREPLY" ] && LASTCHAR=/
COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
else
for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
[ -d "${COMPREPLY[$i]}" ] && COMPREPLY[$i]=${COMPREPLY[$i]}/
done
fi
;;
-c)
# filename completion only showing mp4s
COMPREPLY=($(compgen -o plusdirs -f -X '!*.mp4' \
-- "${COMP_WORDS[COMP_CWORD]}"))
if [ ${#COMPREPLY[@]} = 1 ]; then
[ -d "$COMPREPLY" ] && LASTCHAR=/
COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
else
for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
[ -d "${COMPREPLY[$i]}" ] && COMPREPLY[$i]=${COMPREPLY[$i]}/
done
fi
;;
--h*)
# complete on list of relevant options
COMPREPLY=( $( compgen -W 'convert help version' ${cur_nodash#-} ) )
#dashify;
return 0
;;
-h)
# complete on list of relevant options
COMPREPLY=( $( compgen -W 'convert help version' ${cur_nodash#-} ) )
#dashify;
return 0
;;
esac
fi
}
complete -F _mp4tomp3 mp4tomp3
I deliberately tried to foul up the software by putting the files in a foldername mp:4/ etc. and rolls through them like a champ. Well we are lazy and don't want to be bothered by a terminal right? Enter a zenity script to handle the workload:
#!/bin/sh
# How many files to make the progress bar
PROGRESS=0
NUMBER_OF_FILES=`find -iname "*.mp4" -maxdepth 1 | wc -l`
let "INCREMENT=100/$NUMBER_OF_FILES"
(for i in *.mp4; do
echo "$PROGRESS";
echo "# Converting $i to mp3";
mp4tomp3 --convert "$i"
let "PROGRESS+=$INCREMENT"
done
) | zenity --progress --title "TheeMahn's MP4 Converter..." --percentage=0
Great work huh? Well not good enough for me lets build a postinst script for it and package it up:
#!/bin/bash
# title :mp4tomp3 post installation
# author :Glenn Cady <[email protected]>
# date :11/18/2013
# ============================================================================
set -e
#Pull info we will use to compare against later
IUSER=${SUDO_USER:-$USER}
IHOME="/home/$IUSER"
case "$1" in
configure)
if [[ -x "`which caja 2>/dev/null`" && -x "`which zenity 2>/dev/null`" ]]; then
echo "Installation of Caja & Zenity detected, setting up scripts for $IUSER."
if [ -x $IHOME"/.config/caja/scripts/" ]; then
cp "/usr/share/ultimate_edition/MP4 to MP3" $IHOME"/.config/caja/scripts/"
chown $IUSER:$IUSER $IHOME"/.config/caja/scripts/MP4 to MP3"
chmod 755 $IHOME"/.config/caja/scripts/MP4 to MP3"
else
mkdir -p $IHOME"/.config/caja/scripts/"
cp "/usr/share/ultimate_edition/MP4 to MP3" $IHOME"/.config/caja/scripts/"
chown $IUSER:$IUSER $IHOME"/.config/caja/scripts/MP4 to MP3"
chmod 755 $IHOME"/.config/caja/scripts/MP4 to MP3"
fi
else
echo "Zenity, Caja or both are not installed. Not setting up scripts for Caja."
fi
if [[ -x "`which nautilus 2>/dev/null`" && -x "`which zenity 2>/dev/null`" ]]; then
echo "Installation of Nautilus & Zenity detected, setting up scripts for $IUSER."
if [ -x $IHOME"/.config/nautilus/scripts/" ]; then
cp "/usr/share/ultimate_edition/MP4 to MP3" $IHOME"/.config/nautilus/scripts/"
chown $IUSER:$IUSER $IHOME"/.config/nautilus/scripts/MP4 to MP3"
chmod 755 $IHOME"/.config/nautilus/scripts/MP4 to MP3"
else
mkdir -p $IHOME"/.config/nautilus/scripts/"
cp "/usr/share/ultimate_edition/MP4 to MP3" $IHOME"/.config/nautilus/scripts/"
chown $IUSER:$IUSER $IHOME"/.config/nautilus/scripts/MP4 to MP3"
chmod 755 $IHOME"/.config/nautilus/scripts/MP4 to MP3"
fi
else
echo "Zenity, Nautilus or both are not installed. Not setting up scripts for Nautilus."
fi
esac
exit 0;
Detects if you have nautilus installed or caja etc. and sets up appropriately, let's box it up and ship it:
theemahn@JackHammer:~/Music$ repostorm --build ultimate-edition-mp4tomp3-1.0.0_all
repostorm 1.7.3 is beginning initial build.
First build or no previous errors.
=======================================================
Beginning build of ultimate-edition-mp4tomp3-1.0.0_all
=======================================================
Relaxing permissions of ultimate-edition-mp4tomp3-1.0.0_all
[sudo] password for theemahn:
Cleaning up any tmp or backup files.
Setting individual file permissions to 644.
Scanning /etc to build conffiles.
Scanning for executables in ultimate-edition-mp4tomp3-1.0.0_all Stage 1.
ultimate-edition-mp4tomp3-1.0.0_all/bin/mp4tomp3
ultimate-edition-mp4tomp3-1.0.0_all/usr/share/ultimate_edition/MP4 to MP3
Scanning for executables in ultimate-edition-mp4tomp3-1.0.0_all Stage 2.
Calculating Installed size & inserting into control file if necessary.
ultimate-edition-mp4tomp3-1.0.0_all is 72. Control file reporting: 72
Size indicated in control file is correct.
Calculating MD5SUMS in ultimate-edition-mp4tomp3-1.0.0_all
Setting ownership to root in ultimate-edition-mp4tomp3-1.0.0_all
Setting individual folder permissions to 755.
Setting permissions rights for control etc.
Building of ultimate-edition-mp4tomp3-1.0.0_all
dpkg-deb: building package `ultimate-edition-mp4tomp3' in `ultimate-edition-mp4tomp3-1.0.0_all.deb'.
Lintian resulting package ultimate-edition-mp4tomp3-1.0.0_all.deb.
We have built a totally perfect deb.
Recursively removing ultimate-edition-mp4tomp3-1.0.0_all since we have a perfect deb.
Moving ultimate-edition-mp4tomp3-1.0.0_all.deb to .debs/
All in less then a days work. Enjoy, your feedback is appriciated.
TheeMahn