108 lines
2.3 KiB
Ruby
108 lines
2.3 KiB
Ruby
require 'uri'
|
|
require 'open-uri'
|
|
require 'tempfile'
|
|
require 'youtube-dl.rb'
|
|
require 'streamio-ffmpeg'
|
|
|
|
# @!visibility private
|
|
class QueueItem
|
|
def initialize uri
|
|
if uri.scheme == "file"
|
|
@fname = uri.path
|
|
elsif uri.host and uri.host.include? "youtube.com"
|
|
@fname = get_yt_vid_from uri
|
|
else
|
|
@fname = uri.to_s
|
|
end
|
|
|
|
if (mov = FFMPEG::Movie.new(@fname))
|
|
@time = mov.duration
|
|
@size = mov.size
|
|
else
|
|
throw ArgumentError, "File is invalid"
|
|
end
|
|
end
|
|
|
|
def get_file
|
|
open @fname
|
|
end
|
|
|
|
def cleanup
|
|
Vrobot4.log :DEBUGV, "cleaning up audio cruft"
|
|
File.delete @tmpf if @tmpf
|
|
end
|
|
|
|
private
|
|
def get_yt_vid_from uri
|
|
yt_tmp = get_tmp_name "vrobot4_yt_temp_", ".m4a"
|
|
mv_tmp = get_tmp_name "vrobot4_mv_temp_", ".mp3"
|
|
opt = {
|
|
output: yt_tmp,
|
|
extract_audio: true,
|
|
audio_format: "m4a"
|
|
}
|
|
YoutubeDL.get uri.to_s, opt
|
|
FFMPEG::Movie.new(yt_tmp).transcode(mv_tmp)
|
|
File.delete yt_tmp
|
|
@tmpf = mv_tmp
|
|
end
|
|
|
|
def get_tmp_name name, ext
|
|
fname = Dir::Tmpname.tmpdir + "/"
|
|
fname += Dir::Tmpname.make_tmpname name, ext
|
|
end
|
|
end
|
|
|
|
class Mod_Audio < Vrobot4::Module::Module
|
|
def self.type
|
|
"Audio"
|
|
end
|
|
|
|
Vrobot4::Module.add_module_type self, servflags: "A"
|
|
|
|
def initialize info
|
|
super
|
|
register :c_summon, "summon", "Brings the bot to your voice channel."
|
|
register :c_vanquish, "vanquish", "Kicks the bot from voice.", roles: "o"
|
|
register :c_play, "play", "Overwrites the queue.", roles: "o"
|
|
register :c_queue, "queue", "Adds an item to the queue."
|
|
@queue = []
|
|
end
|
|
|
|
def c_summon m, argv
|
|
m.serv.voice_join m
|
|
end
|
|
|
|
def c_vanquish m, argv
|
|
m.serv.voice_quit m
|
|
end
|
|
|
|
def c_play m, argv
|
|
@queue.clear
|
|
push_queue m, argv
|
|
start_playback m, qi
|
|
end
|
|
|
|
def c_queue m, argv
|
|
push_queue m, argv
|
|
start_playback m, qi unless m.serv.is_playing? m
|
|
end
|
|
|
|
private
|
|
def push_queue m, argv
|
|
m.reply "Queueing '" + uri.to_s + "'..."
|
|
|
|
uri = URI.parse argv
|
|
qi = @queue.push QueueItem.new(uri)
|
|
|
|
m.reply "'" + uri.to_s + "' loaded (%i sec, %i bytes)" %
|
|
[qi.time, qi.size]
|
|
end
|
|
|
|
def start_playback m, qi
|
|
m.serv.voice_play m, qi.get_file
|
|
end
|
|
end
|
|
|
|
## EOF
|