3

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.

Adam
  • 515
  • 1
  • 7
  • 18
  • basic audio extraction with `ffmep` is `ffmpeg -i video.mp4 -f mp3 -ab 192000 -vn music.mp3` where `vn` is *no video* and `acodec` copy says use the same audio stream that's already in there; from [Github](https://gist.github.com/protrolium/e0dbd4bb0f1a396fcb55) – damadam Dec 20 '19 at 08:59
  • I suggest you also to add a `sed` command, in order to remove the `.mp4` extension on the filename, or your music filename would be for example `music.mp4.mp3` – damadam Dec 20 '19 at 09:01

1 Answers1

7

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.

terdon
  • 98,183
  • 15
  • 197
  • 293
ntd
  • 168
  • 13
  • Not only is it superfluous, it is also [a very bad idea to parse the output of `ls`](https://mywiki.wooledge.org/ParsingLs), and doubly so for media files which often have strange names. – terdon Dec 20 '19 at 10:00
  • 4
    The bash trickery is: `"${f%.mp4}.mp3"` :) – andrew.46 Dec 21 '19 at 00:57