Last active
April 13, 2023 13:04
-
-
Save aglitchman/e947d5938e82d15f758590442343db57 to your computer and use it in GitHub Desktop.
Convert to Ogg Vorbis - editor script for Defold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 1. Put this script into your project's dir as `convert_sound.editor_script` (the extension is important!) | |
-- 2. Click "Project / Reload Editor Scripts" | |
-- 3. Download the latest ffmpeg from https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full.7z or | |
-- from https://ffmpeg.org/download.html for other platforms | |
-- 4. Put ffmpeg executable into FFMPEG_LOCATION or modify the FFMPEG_LOCATION path | |
-- 5. Tune output audio file quality: | |
-- Range is -1.0 to 10.0, where 10.0 is highest quality. | |
-- Default is 3 with a target of 112kbps, and it's enough for the most games. | |
-- The formula 16×(q+4) is used below 4, 32×q is used below 8, and 64×(q-4) otherwise. | |
-- Examples: 112=16×(3+4), 160=32×5, 200=32×6.25, 384=64×(10-4). | |
local FFMPEG_LOCATION = "" | |
local M = {} | |
local function ends_with(str, ending) | |
return ending == "" or str:sub(-#ending) == ending | |
end | |
local function join(t1, t2) | |
local t = {} | |
for _, v in ipairs(t1) do | |
table.insert(t, v) | |
end | |
for _, v in ipairs(t2) do | |
table.insert(t, v) | |
end | |
return t | |
end | |
local function make_command(opts, quality) | |
local path = editor.get(opts.selection, "path") | |
path = path:sub(2) | |
local output_path = path:sub(1, -5) .. ".ogg" | |
if ends_with(string.lower(path), ".ogg") then | |
output_path = path:sub(1, -5) .. "_q" .. tostring(quality) .. ".ogg" | |
end | |
local ffmpeg_args = { | |
"-hide_banner", | |
"-loglevel", "warning", | |
"-stats", | |
"-i", path, | |
"-vn", -- i.e. no video | |
"-c:a", "libvorbis", | |
"-q:a", tostring(quality), | |
"-ar", "44100", | |
"-map_metadata", "-1", -- to strip metadata and save space | |
"-y", output_path | |
} | |
if editor.platform == "x86_64-win32" then | |
return { | |
{ | |
action = "shell", | |
command = join({"cmd", "/C", FFMPEG_LOCATION .. "ffmpeg.exe"}, ffmpeg_args) | |
} | |
} | |
elseif editor.platform == "x86_64-darwin" or editor.platform == "x86_64-linux" then | |
return { | |
{ | |
action = "shell", | |
command = join({FFMPEG_LOCATION .. "ffmpeg"}, ffmpeg_args) | |
} | |
} | |
else | |
error("ERROR: Not supported platform") | |
end | |
end | |
function M.get_commands() | |
return { | |
{ | |
label = "Convert to Ogg Vorbis (Quality 3)", | |
locations = {"Edit", "Assets"}, | |
query = {selection = {type = "resource", cardinality = "one"}}, | |
active = function(opts) | |
local path = string.lower(editor.get(opts.selection, "path")) | |
return ends_with(path, ".wav") or ends_with(path, ".mp3") or ends_with(path, ".ogg") | |
end, | |
run = function(opts) | |
return make_command(opts, 3) | |
end | |
}, | |
{ | |
label = "Convert to Ogg Vorbis (Quality 1)", | |
locations = {"Edit", "Assets"}, | |
query = {selection = {type = "resource", cardinality = "one"}}, | |
active = function(opts) | |
local path = string.lower(editor.get(opts.selection, "path")) | |
return ends_with(path, ".wav") or ends_with(path, ".mp3") or ends_with(path, ".ogg") | |
end, | |
run = function(opts) | |
return make_command(opts, 1) | |
end | |
} | |
} | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment