Ubuntu – Convert a folder of mp4’s to mp3’s with ffmpeg


I have a folder mostly containing mp4 videos. I want to extract the audio of those videos.

for i in $(ls); do echo ffmpeg -i $i $i_mp3; done

This command doesn't take any concern for the none mp4 files, and it seems to be adding line breaks in the ls command.

Best Answer

  • The ls command is superfluous: filename expansion should be just enough. I did not understand the adding line breaks part of your question though... I suppose you had problems with blank spaces in filenames.

    I would personally use a variant of the following command:

    for f in *.mp4; do ffmpeg -i "$f" -vn "$(basename "$f" .mp4).mp3"; done

    The basename part is just to remove the .mp4 suffix from $f. You can use some bash trickery instead, but I never remember the syntax.