Skip to content

Instantly share code, notes, and snippets.

@JacobHacker
Last active August 29, 2015 14:03
Show Gist options
  • Save JacobHacker/a9925bbe69ebed1b735a to your computer and use it in GitHub Desktop.
Save JacobHacker/a9925bbe69ebed1b735a to your computer and use it in GitHub Desktop.
#!/usr/bin/lua
---
-- Created 2014 by Jacob Hacker <jacobhacker@outlook.com>
-- Licensed under the MIT license: http://opensource.org/licenses/MIT
---
-- Install: sudo apt-get install lua5.2 libav-tools googlecl python-pip && sudo pip install megacl googlecl
--(------------)--
--(--- Util ---)--
--(------------)--
local cli = {}
function cli.run(command, args)
local handle = io.popen(command, args)
local result = handle:read("*a")
handle:close()
return result
end
function fileExists(n)
local f = io.open(n, "r");
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function getAudioDuration(audioFilename)
local out = cli.run("avconv -i '" .. audioFilename .. "' 2>&1")
local audioLen = out:match("Duration: (%d%d:%d%d:%d%d%.%d%d)")
if audioLen == nil then
print("Failed to extract audio length")
print("Program out: " .. out)
os.exit()
end
return audioLen
end
function cli.packageInstalled(package)
local out = cli.run("dpkg -l | grep -E '^ii' | grep '^ii\\s*" .. package .. "\\s'")
return out ~= ''
end
--(-----------------------)--
--(-- Program Functions --)--
--(-----------------------)--
function mp3ToMkv(audioFilename, thumbnailFilename, audioLen)
local outFilename = audioFilename:match("(.*)%.")
local cmd = "avconv -loop 1 -r 1 -i '" .. thumbnailFilename .. "' " ..
" -i '" .. audioFilename .."' -c:a copy -preset medium -threads 2" ..
" -t " .. audioLen .. " -c:v libx264 '".. outFilename..".mkv' -y"
print("<<" .. cmd .. ">>")
cli.run(cmd)
end
function getThumbnail(thumbnailFilename)
if thumbnailFilename == nil then
cli.run("wget 'https://i.imgur.com/XYTACtb.jpg' -O 'video-thumb.jpg'")
end
if not fileExists(thumbnailFilename) then
print("Thumbnail file '" .. thumbnailFilename .. "' not found.")
os.exit()
end
return thumbnailFilename
end
local ansiColor = {
red = "\27[31m",
green = "\27[32m",
clear = "\27[0m",
}
function checkPackages(packages)
local toInstall = {}
for i=1, #packages do
--io.write(packages[i])
if not cli.packageInstalled(packages[i]) then
io.write(packages[i] .. ansiColor.red .. " [Not Installed]\n" .. ansiColor.clear)
toInstall[#toInstall+1] = packages[i]
end
--io.write("\n")
end
if #toInstall ~= 0 then
io.write("sudo apt-get install")
for i=1, #toInstall do
io.write(" " .. toInstall[i])
end
io.write("\n")
end
end
--(-------------)--
--(-- Program --)--
--(-------------)--
local audioFilename = arg[1]
if audioFilename == nil then
print("You must pass the audio file's location.")
os.exit()
end
if not fileExists(audioFilename) then
print("Audio file '" .. audioFilename .. "' not found.")
os.exit()
end
local thumbnailFilename = getThumbnail(arg[2] or "./video-thumb.jpg")
if string.find(audioFilename, ".+%.mp3", 1) == nil then
local outFilename = audioFilename:match("(.*)%.") .. ".mp3"
cli.run("avconv -i '" .. audioFilename .. "' -b 128k '" .. outFilename .. "'")
audioFilename = outFilename
end
local audioLen = getAudioDuration(audioFilename)
mp3ToMkv(audioFilename, thumbnailFilename, audioLen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment