I have looked at Converting mp4 to mp3 which uses a tool called ffmpeg
. The process is great when you have a single file to convert, but I'm trying automate the mp4 to mp3 conversion for any given directory.
As an example, the directory below with all its sub directories is given, i.e. MusicVideos`:
.
├── Andra
│ └── Andra::Why.mp4
├── Ariana Grande
│ └── Dangerous Woman
│ ├── ArianaGrande::IntoYou.mp4
│ └── ArianaGrande::SideToSide.mp4
├── Justin Bieber
│ └── JustinBieber::LetMeLoveYou.mp4
├── Major Lazer
│ └── De Maxx 37
│ └── MajorLazer::ColdWater.mp4
├── Martin Garrix & Bebe Rexha
│ └── MartinGarrix&BebeRevha::InTheNameOfLove.mp4
├── Shawn Mendes
│ └── ShawnMendes::TreatYouBetter.mp4
├── Sia
│ └── The Greatest
│ └── Sia::TheGreatest.mp4
├── The Chainsmokers
│ ├── TheChainsmokers::AllWeKnow.mp4
│ └── TheChainsmokers::Closer.mp4
├── The Weekend
│ └── Starboy
│ └── TheWeekend::DaftPunk.mp4
└── TWENTY ØNE PILØTS
└── Suicide Squad
└── TwentyOnePilots::Heathens.mp4
After the script is ran the output directory should look like, i.e., MusicAudio
:
.
├── Andra
│ └── Andra::Why.mp3
├── Ariana Grande
│ └── Dangerous Woman
│ ├── ArianaGrande::IntoYou.mp3
│ └── ArianaGrande::SideToSide.mp3
├── Justin Bieber
│ └── JustinBieber::LetMeLoveYou.mp3
├── Major Lazer
│ └── De Maxx 37
│ └── MajorLazer::ColdWater.mp3
├── Martin Garrix & Bebe Rexha
│ └── MartinGarrix&BebeRevha::InTheNameOfLove.mp3
├── Shawn Mendes
│ └── ShawnMendes::TreatYouBetter.mp3
├── Sia
│ └── The Greatest
│ └── Sia::TheGreatest.mp3
├── The Chainsmokers
│ ├── TheChainsmokers::AllWeKnow.mp3
│ └── TheChainsmokers::Closer.mp3
├── The Weekend
│ └── Starboy
│ └── TheWeekend::DaftPunk.mp3
└── TWENTY ØNE PILØTS
└── Suicide Squad
└── TwentyOnePilots::Heathens.mp3
I was looking at how to do this conversion using bash scripts and i came across Script: Recursively convert wma files to MP3, then remove WMA files.
This seems a bit harder than what I was anticipating for, any help and guidance will be greatly appreciated.
Update
With some help I've written a script: Please confirm that this works!
cp -a /$1/. /$2/ #copy the whole dir from src to dest
#cd $2 #change dir to dest
cd $2
#convert .mp4 to .mp3
#find . -name ".mp4" -exec bash -c 'var=${1%.mp4}; var=${var#/};ffmpeg -i "${1#*/}" -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 "$dest/$var".mp3' - '{}' ;
find . -name "*.mp4" -exec bash -c 'ffmpeg -i "$1" -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 "${1%.mp4}".mp3' - '{}' ;
echo "Cleaning up"
find . -name ".mp4" -exec bash -c 'var=${1%.mp4}; var=${var#/}; rm "${1#*/}"' - '{}' ;
You can find the source code on my GitHub. Any further contribution will be appreciated greatly.
find . -name "*.mp4" -exec bash -c 'echo ffmpeg -i "$1" -vn -acodec libmp3lame -ac 2 -qscale:a 4 -ar 48000 "${1%.mp4}".mp3' - '{}' \;
– Bilal Baqar Oct 16 '16 at 16:21echo
did work for me. Just that the converted files are in the same directory as the original files! Is there a way to output to a different directory with the same file structure? – Amir Oct 16 '16 at 17:06VAR=value VAR2=value command args
is legal syntax (and only sets the variables for the command). – alexis Oct 16 '16 at 19:59"~/PATH/to/NewTopDIr/$var"
with the actual directory path. Only to find that it's not possible to do so. Then, I tried to replicate the file structure at destination with no mp4 files, and that failed too! sample of error message~/Videos/MusicAudio/Major Lazer/De Maxx 37/MajorLazer::ColdWater.mp3: No such file or directory
– Amir Oct 17 '16 at 02:51/home/amir/music-mp3
is your new directory then replace it before the$var
variable. i.e."/home/amir/music-mp3/$var"
.$var
will contain the rest of the file path i.e.Andra/Andra::Why.mp3
. – Bilal Baqar Oct 17 '16 at 04:30find . -type d >dirs.txt
then inside new directoryxargs mkdir -p <dirs.txt
– Bilal Baqar Oct 17 '16 at 05:40>dirs.txt
and<dirs.txt
do? – Amir Oct 17 '16 at 10:31>dirs.txt
stores the output of the command on the left into a file nameddirs.txt
.<dirs.txt
gives the contents to the command on the left. In this case itsxrgs
which specifically takes in arguments and performs some actions on them. – Bilal Baqar Oct 17 '16 at 10:47