Mandy/mandy.lua

148 lines
3.3 KiB
Lua
Raw Normal View History

2013-07-23 21:23:31 -05:00
#!/usr/bin/lua5.2
require("iuplua")
require("iupluacontrols")
require('progress')
2013-07-27 01:52:23 -05:00
require('ffmpeg_progress')
2013-07-26 20:36:15 -05:00
2013-07-23 21:23:31 -05:00
--Main url entry box
function url_entry()
local url = ""
local format = 0
res, url, format = iup.GetParam("Mandy 0.4 - Enter URL", nil,
"Enter Youtube URL: %s\n"..
"Select an audio format: %l|mp3|ogg|flac|\n", url, format)
gFormat = sel_Codec(format)
2013-07-27 11:51:07 -05:00
if res == true then
if string.match(url, "youtube.com/watch") then
return url
else
2013-07-27 11:51:07 -05:00
iup.Message("Error", "The URL is not a youtube link")
iup.Flush()
iup.Close()
end
2013-07-27 11:51:07 -05:00
else
return false
end
2013-07-23 21:23:31 -05:00
end
--make format dropdown menu items work
function sel_Codec(x)
if x == 0 then
local mp3 = ".mp3"
print("Selected "..mp3)
return mp3
elseif x == 1 then
local ogg = ".ogg"
print("Selected "..ogg)
return ogg
elseif x == 2 then
local flac = ".flac"
print("Selected "..flac)
return flac
end
2013-07-23 21:23:31 -05:00
end
--Ask user to set directory
function set_dir()
2013-07-27 11:51:07 -05:00
getPath = iup.filedlg{dialogtype = "SAVE", title="Save file...",
filter="*.mp3; *.ogg; *.flac",
2013-07-27 11:51:07 -05:00
directory=".",
file=""}
2013-07-23 21:23:31 -05:00
2013-07-27 11:51:07 -05:00
getPath:popup(iup.ANYWHERE, iup.ANYWHERE)
2013-07-23 21:23:31 -05:00
2013-07-27 11:51:07 -05:00
local status = getPath.status
2013-07-23 21:23:31 -05:00
if status == "1" or status == "0" then
if string.match(getPath.value, "%"..gFormat) then
2013-07-27 11:51:07 -05:00
local savedPath = getPath.value
return savedPath
2013-07-27 11:51:07 -05:00
else
local savedPath = getPath.value..gFormat
2013-07-27 11:51:07 -05:00
return savedPath
2013-07-27 11:51:07 -05:00
end
2013-07-23 21:23:31 -05:00
2013-07-27 11:51:07 -05:00
elseif status == "-1" then
return false
2013-07-27 11:51:07 -05:00
end
2013-07-23 21:23:31 -05:00
end
2013-07-26 21:07:19 -05:00
function ytDl(x)
local tmpName = math.random(0,999999999999)
local tmpNameTwo = math.random(0,999999999999)
2013-07-26 21:32:28 -05:00
local tmpPath = x.." --output=/tmp/youtube-dl-"..tmpName.."-"..tmpNameTwo..".flv"
2013-07-26 21:07:19 -05:00
local tmpPath2 = "/tmp/youtube-dl-"..tmpName.."-"..tmpNameTwo..".flv"
2013-07-23 21:23:31 -05:00
2013-07-29 22:55:12 -05:00
local flag = popin.ytdl(tmpPath)
if not flag then return false end
2013-07-26 21:07:19 -05:00
return tmpPath2
2013-07-23 21:23:31 -05:00
end
--optional function to implement autoplay
function ask_play(x)
play = ""
res, play = iup.GetParam("Play now?", nil,
"Do you want to play the file now? %t\n", play)
if res == true then
io.popen("notify-send 'The file was saved to' "..x)
io.popen("xdg-open "..x)
else
return false
end
end
2013-07-26 21:07:19 -05:00
function ffmpeg(tmpPath, dirD)
2013-07-30 15:47:03 -05:00
local subSpaces = string.gsub(dirD, " ", "\\ ")
local mp3 = "ffmpeg -i "..tmpPath.." -acodec libmp3lame -ac 2 -ab 192k -vn -y "..subSpaces
local ogg = "ffmpeg -i "..tmpPath.." -acodec libvorbis -ac 2 -ab 192k -vn -y "..subSpaces
local flac = "ffmpeg -i "..tmpPath.." -acodec flac -ac 2 -ab 192k -vn -y "..subSpaces
if gFormat == ".mp3" then
local flag = convert_mp3.go(mp3)
elseif gFormat == ".ogg" then
local flag = convert_mp3.go(ogg)
elseif gFormat == ".flac" then
local flag = convert_mp3.go(flac)
else
if not flag then return false end
end
2013-07-27 11:51:07 -05:00
io.popen("notify-send 'The file was saved to' "..subSpaces)
2013-07-23 21:23:31 -05:00
end
2013-07-27 11:51:07 -05:00
2013-07-23 21:23:31 -05:00
--put everything into a table to use local functions globally
global = {}
global.url = function ()
local url = url_entry()
return url
end
global.dir = function()
local dir = set_dir()
return dir
end
2013-07-26 21:07:19 -05:00
global.path = function(url)
local yt = ytDl(url)
return yt
2013-07-23 21:23:31 -05:00
end
2013-07-26 21:07:19 -05:00
global.ffmpeg = function(tmpPath, saveDir)
local convert = ffmpeg(tmpPath, saveDir)
return convert
end
2013-07-23 21:23:31 -05:00