Skip to content

Instantly share code, notes, and snippets.

@mxwell
Created June 1, 2013 22:54
Show Gist options
  • Save mxwell/5691988 to your computer and use it in GitHub Desktop.
Save mxwell/5691988 to your computer and use it in GitHub Desktop.
Ruby script to convert *.flac to *.mp3 with ffmpeg in Windows
output_dir = "output"
if File.exists? output_dir
id = 1
while File.exists?(output_dir + id.to_s) && id < 10
id += 1
end
if id >= 10
puts "error: can't figure out new output directory name"
exit 0
end
output_dir = output_dir + id.to_s
end
Dir.mkdir output_dir
Dir.glob("*.flac").each do | file |
puts "processing '#{file}'"
file.gsub! /\.flac/, ''
cmd = 'C:/cmd/ffmpeg/bin/ffmpeg -v 1 -i "' + file + '".flac -f mp3 -b:a 320k "' + output_dir + "/" + file + '.mp3"'
puts " command: " + cmd
if not system(cmd)
puts " error"
else
puts " done."
end
end
@mxwell
Copy link
Author

mxwell commented Oct 12, 2013

Variant for conversion of m4a to mp3 at Ubuntu:

#! /usr/bin/ruby

# @ubuntu: depends on ffmpeg, libavcodec-extra-53

output_dir = "output"

if File.exists? output_dir
    id = 1
    while File.exists?(output_dir + id.to_s) && id < 10
        id += 1
    end
    if id >= 10
        puts "error: can't figure out new output directory name"
        exit 0
    end
    output_dir = output_dir + id.to_s
end

Dir.mkdir output_dir

Dir.glob("*.m4a").each do | file |
    puts "processing '#{file}'"
    file.gsub! /\.m4a/, ''
    cmd = 'ffmpeg -v 1 -i "' + file + '".m4a -f mp3 -b:a 320k "' + output_dir + "/" + file + '.mp3"'
    puts " command: " + cmd
    if not system(cmd)
        puts " error"
    else
        puts " done."
    end
end

@mxwell
Copy link
Author

mxwell commented Jan 7, 2021

Variant in Bash:

#! /bin/bash

OUTPUT_DIR="ffmpeg_output"
mkdir -p $OUTPUT_DIR

for flac in *.flac
do
    mp3=$(echo $flac | sed s/flac/mp3/)
    ffmpeg -i "$flac" -f mp3 -b:a 320k "$OUTPUT_DIR/$mp3"
 done

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