Skip to content

Instantly share code, notes, and snippets.

@TheLinx
Created March 10, 2011 13:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheLinx/864062 to your computer and use it in GitHub Desktop.
Save TheLinx/864062 to your computer and use it in GitHub Desktop.
Recursively encode any ffmpeg-supported audio tracks to Ogg Vorbis
#!/usr/bin/env lua
-- Recursively encode any ffmpeg-supported audio tracks to Ogg Vorbis
local lfs = require("lfs") -- LuaFileSystem
local exec = os.execute
local function is_directory(fullname)
return lfs.attributes(fullname, "mode") == "directory"
end
local function recurse(path, target)
if not lfs.attributes(target..path:sub(2)) then -- if the target directory doesn't have this folder,
lfs.mkdir(target..path:sub(2)) -- create it
end
for name in lfs.dir(path) do -- for every file in the directory
if not (name == "." or name == "..") then
local fullname = path.."/"..name
if is_directory(fullname) then -- if a directory
if name:lower() ~= "scans" then -- and it's not useless
recurse(fullname, target) -- recurse that
end
else -- if not a directory
local outname = target..fullname:sub(2):match("(.+/[^.]+)")..".ogg" -- determine our filename
exec(("ffmpeg -i %q -map_metadata 0:0 -acodec libvorbis -ab %dk -threads 2 %q"):format(fullname, BITRATE or 64, outname)) -- re-encode
end
end end
end
if select("#", ...) < 2 then
print("usage: rogg.lua from-directory to-directory [bitrate k]")
os.exit()
end
if select(3, ...) then
BITRATE = tonumber(select(3, ...))
end
recurse(...)
#!/usr/bin/env python3
# Recursively encode any ffmpeg-supported audio tracks to Ogg Vorbis
import argparse, os, subprocess
from sys import exit
parser = argparse.ArgumentParser(description='Recursively encode any ffmpeg-supported audio tracks to Ogg Vorbis')
parser.add_argument('-v', '--verbose', action='store_true', help='print out actions as they are happening')
parser.add_argument('--dry-run', action='store_true', help='don\'t actually do anything')
parser.add_argument('-b', '--bitrate', default=64, type=int, help='bitrate in kilobits per second')
parser.add_argument('fromd', metavar='from-directory', help='original directory to recurse')
parser.add_argument('tod', metavar='to-directory', help='directory to place converted file tree')
verbose = False
dry_run = False
bitrate = None
devnull = open(os.devnull, "w")
def recurse(path, target, indent=0):
full_target = os.path.join(target, os.path.basename(path))
ind = " " * indent
if not os.access(full_target, os.F_OK):
if verbose: print("%screating `%s'" % (ind, full_target))
if not dry_run: os.mkdir(full_target)
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
new_file = os.path.splitext(file)[0] + ".ogg"
full_newfile = os.path.join(full_target, new_file)
if os.access(full_newfile, os.F_OK):
print("`%s' already exists" % new_file)
continue
if verbose: print("%sconverting `%s' -> `%s'" % (ind, file, new_file))
if not dry_run:
popen = subprocess.Popen(["ffmpeg", "-i", os.path.join(path, file), "-map_metadata", "0:0", "-acodec", "libvorbis", "-ab", "%dk" % bitrate, os.path.join(full_target, new_file)], stdout=devnull, stderr=devnull)
popen.wait()
if popen.returncode != 0:
print("error while converting, stopping")
exit(1)
else:
if verbose: print("%sentering `%s'" % (ind, file))
recurse(os.path.join(path, file), full_target, indent=indent + 1)
if verbose: print("%sleaving `%s'" % (ind, file))
def main():
options = parser.parse_args()
if options.verbose:
global verbose
verbose = True
if options.dry_run:
global dry_run
dry_run = True
global bitrate
bitrate = options.bitrate
if not os.access(os.path.abspath(options.fromd), os.X_OK):
exit()
recurse(os.path.abspath(options.fromd), os.path.abspath(options.tod))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment