Merge branch 'master' of gitfub.space:Jmaa/memebot

This commit is contained in:
Jon Michael Aanes 2018-06-14 15:37:47 +02:00
commit 1eea02bc44
2 changed files with 53 additions and 19 deletions

View File

@ -5,12 +5,20 @@ local internet = require 'internet'
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function check_file_exists (filename)
assert(type(filename) == 'string')
local status = os.execute('test -e "'..filename..'"')
return status == 0
end
local function ensure_tmp_folder_exists () local function ensure_tmp_folder_exists ()
os.execute(('mkdir --parents "%s"'):format(TMP_FOLDER)) os.execute(('mkdir --parents "%s"'):format(TMP_FOLDER))
end end
local function video_length (video_filename) local function video_length (video_filename)
assert(type(video_filename) == 'string') assert(type(video_filename) == 'string')
assert(check_file_exists(video_filename))
--
local f = io.popen(('ffprobe -i "%s" -show_entries format=duration -v quiet -of csv="p=0"'):format(video_filename), 'r') local f = io.popen(('ffprobe -i "%s" -show_entries format=duration -v quiet -of csv="p=0"'):format(video_filename), 'r')
local str = f:read '*all' local str = f:read '*all'
f:close() f:close()
@ -24,12 +32,11 @@ end
local function video_silences (video_filename, decibel, duration) local function video_silences (video_filename, decibel, duration)
assert(type(video_filename) == 'string') assert(type(video_filename) == 'string')
assert(check_file_exists(video_filename))
assert(type(decibel) == 'number') assert(type(decibel) == 'number')
assert(type(duration) == 'number') assert(type(duration) == 'number')
-- --
local log_filename = os.tmpname() local f = io.popen(('ffmpeg -loglevel panic -y -i "%s" -af silencedetect=n=%sdB:d=%s -f null - 1> /dev/null'):format(video_filename, decibel, duration, log_filename))
os.execute(('ffmpeg -i "%s" -af silencedetect=n=%sdB:d=%s -f null - 1> /dev/null 2> %s'):format(video_filename, decibel, duration, log_filename))
local f = io.open(log_filename)
local silences = {} local silences = {}
for line in f:lines() do for line in f:lines() do
local silence_end, duration = line:gsub('\027%[.-;', ''):match '^%[silencedetect @ 0x%x+%]%s*silence_end:%s*([%d.]+)%s*|%s*silence_duration:%s*([%d.]+)$' local silence_end, duration = line:gsub('\027%[.-;', ''):match '^%[silencedetect @ 0x%x+%]%s*silence_end:%s*([%d.]+)%s*|%s*silence_duration:%s*([%d.]+)$'
@ -48,6 +55,7 @@ end
local function video_silence_at_end (video_filename, ...) local function video_silence_at_end (video_filename, ...)
assert(type(video_filename) == 'string') assert(type(video_filename) == 'string')
assert(check_file_exists(video_filename))
-- --
local silences = video_silences(video_filename, ...) local silences = video_silences(video_filename, ...)
local duration = video_length(video_filename) local duration = video_length(video_filename)
@ -66,16 +74,18 @@ local function paste_audio_onto_video (video_filename, audio_filename, timestamp
assert(type(timestamp) == 'number') assert(type(timestamp) == 'number')
assert(type(video_filename) == 'string') assert(type(video_filename) == 'string')
assert(type(audio_filename) == 'string') assert(type(audio_filename) == 'string')
print('Hah!', timestamp)
local silence_filename = TMP_FOLDER..'silence.mp3'
local output_filename = TMP_FOLDER..'output.webm'
-- Generate silence -- Generate silence
local silence_filename = TMP_FOLDER..'silence.mp3' os.execute(('ffmpeg -loglevel panic -y -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t %s %s'):format(timestamp, silence_filename))
os.execute(('ffmpeg -y -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t %s %s &> /dev/null'):format(timestamp, silence_filename))
-- Paste audio onto video -- Paste audio onto video
local output_filename = TMP_FOLDER..'output.webm' os.execute(('ffmpeg -loglevel panic -y -i "%s" -i "concat:%s|%s" -filter_complex "[0:a:0][1:a:0]amerge=inputs=2[a]" -map 0:v:0 -map "[a]" -c:v copy -c:a libvorbis -ac 2 -shortest %s'):format(video_filename, silence_filename, audio_filename, output_filename))
local cmd = (('ffmpeg -y -i "%s" -i "concat:%s|%s" -filter_complex "[0:a:0][1:a:0]amerge=inputs=2[a]" -map 0:v:0 -map "[a]" -c:v copy -c:a libvorbis -ac 2 -shortest %s &> /dev/null'):format(video_filename, silence_filename, audio_filename, output_filename))
os.execute(cmd) -- Remove redundant silence
os.remove(silence_filename)
return output_filename return output_filename
end end
@ -107,9 +117,12 @@ end
local function curb_your_video (video_url, timestamp) local function curb_your_video (video_url, timestamp)
assert(type(video_url) == 'string') assert(type(video_url) == 'string')
assert(type(timestamp) == 'number' or timestamp == nil) assert(type(timestamp) == 'number' or timestamp == nil)
-- Download video -- Download video
ensure_tmp_folder_exists() ensure_tmp_folder_exists()
local video_filename = internet.download_video(video_url) local video_filename = internet.download_video(video_url)
assert(check_file_exists(video_filename))
-- Figure out timestamp to start theme -- Figure out timestamp to start theme
if timestamp == nil then if timestamp == nil then
timestamp = determine_timestamp_for_curb_your_video(video_filename) timestamp = determine_timestamp_for_curb_your_video(video_filename)
@ -118,8 +131,10 @@ local function curb_your_video (video_url, timestamp)
elseif timestamp < 0 then elseif timestamp < 0 then
timestamp = video_length(video_filename) + timestamp timestamp = video_length(video_filename) + timestamp
end end
print('Timestamp', timestamp, video_filename)
-- Paste audio -- Paste audio
print(timestamp)
return paste_audio_onto_video(video_filename, CURB_YOUR_ENTHUSIASM_THEME_FILENAME, timestamp) return paste_audio_onto_video(video_filename, CURB_YOUR_ENTHUSIASM_THEME_FILENAME, timestamp)
end end

View File

@ -31,14 +31,29 @@ local function clean_text (text)
return text:gsub('%s+', ' '):match('^%s*(.-)%s*$') return text:gsub('%s+', ' '):match('^%s*(.-)%s*$')
end end
local function check_file_exists (filename)
assert(type(filename) == 'string')
local status = os.execute('test -e "'..filename..'"')
return status == 0
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Internet shit -- Internet shit
local function copy_remotely (origin_server, origin_path, target_server, target_path) local function copy_remotely (origin_server, origin_path, target_server, target_path)
local origin = origin_server and origin_server ~= 'localhost' and origin_server..':'..origin_path or origin_path local origin = origin_server and origin_server ~= 'localhost' and origin_server..':'..origin_path or origin_path
local target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path local target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path
os.execute('scp '..origin..' '..target..' > /dev/null') local cmd = (origin_server == 'localhost' and target_server == 'localhost') and 'cp' or 'scp'
--
if origin_server == 'localhost' and not check_file_exists(origin_path) then
error('File "'..origin_path..'" does not exist!')
end
--
local status = os.execute(cmd..' '..origin..' '..target)
if status ~= 0 then
error('Could not copy file! Got error code: '..tostring(status))
end
end end
local function save_file_to_dcav (filename) local function save_file_to_dcav (filename)
@ -237,7 +252,7 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font
-- Convert svg to png -- Convert svg to png
if url:match '%.svg$' then if url:match '%.svg$' then
local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png' local filename_2 = CONFIG.IMGGEN_PATH_OUTPUT..'topic_'..topic.topic..'.'..'png'
os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'"') os.execute('convert -density "1200" -resize 400x400 "'..filename..'" "'..filename_2..'" &> /dev/null')
filename = filename_2 filename = filename_2
end end
-- --
@ -561,14 +576,18 @@ function memes.generate_for_message (user, message)
do -- Curb your enthusiasm do -- Curb your enthusiasm
local url = message:match '^[Cc]urb%s+[Yy]our%s+(.+)$' local url = message:match '^[Cc]urb%s+[Yy]our%s+(.+)$'
if url then
local url2, timestamp = message:match '^(..-)%s+at%s+(-?[%d.]+)$' local url2, timestamp = message:match '^(..-)%s+at%s+(-?[%d.]+)$'
if url2 then url = url2 end if url2 then url = url2 end
timestamp = timestamp and tonumber(timestamp) or nil timestamp = timestamp and tonumber(timestamp) or nil
assert(type(timestamp) == 'number' or timestamp == nil) assert(type(timestamp) == 'number' or timestamp == nil)
local video_filename = curb.your_video(url, timestamp) local video_filename = curb.your_video(url, timestamp)
assert(check_file_exists(video_filename))
local video_url = save_file_to_dcav(video_filename) local video_url = save_file_to_dcav(video_filename)
os.remove(video_filename)
return video_url, 'CURB' return video_url, 'CURB'
end end
end
-- Vælter min skorsten -- Vælter min skorsten
do do