Skip to content

Instantly share code, notes, and snippets.

@matthew-e-brown
Last active March 13, 2025 08:11
Show Gist options
  • Save matthew-e-brown/f13af9241a9fc0a2251f0f948cf25700 to your computer and use it in GitHub Desktop.
Save matthew-e-brown/f13af9241a9fc0a2251f0f948cf25700 to your computer and use it in GitHub Desktop.
FFmpeg command I wanna remember.

One-liner to convert all FLAC to MP3

find "[MP3]"* -name "*.flac" -exec bash -c 'ffmpeg -i "${0}" -ab 320k -c:v copy "${0/.flac/.mp3}" && rm "${0}"' {} \;

I prefer to duplicate my .flac files before ffmpeg-ing them all, hence the "[MP3]"* prefix on the find and the rm at the end. Sometimes, this may be unnecessary, since I might put the MP3 files in their own subdirectory, at which point find . will work fine. In the case this example is pulled from, I had two [MP3] folders and two [FLAC] folders in the same directory.

find
  "[MP3]"*
  -name "*.flac"          # Files ending with .flac extension
  -exec
    bash -c               # run a subshell to allow for command expansion. -c = use a command_string
      ffmpeg
        -i "${0}"         # the filename passed from find down to the bash instance
        -ab 320k          # MP3 output audio quality
        -c:v copy         # copy the "video" stream (the album art) instead of transcoding it
        "${0/.flac/.mp3}" # replace .flac with .mp3 in output filename
      && rm "${0}"        # delete the .flac when you're done
  \;

Note that this explanation is missing the '' around the -exec string. I wanted GitHub Gist to highlight my comments properly. 😋

Where I ended up finding the final answers to my find -exec and Bash expansion questions:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment