Curb your enthusiasm mostest

This commit is contained in:
jmaa 2018-06-13 16:47:09 +02:00
parent f21e43f147
commit 8dec886059
2 changed files with 52 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 ()
os.execute(('mkdir --parents "%s"'):format(TMP_FOLDER))
end
local function video_length (video_filename)
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 str = f:read '*all'
f:close()
@ -24,12 +32,11 @@ end
local function video_silences (video_filename, decibel, duration)
assert(type(video_filename) == 'string')
assert(check_file_exists(video_filename))
assert(type(decibel) == 'number')
assert(type(duration) == 'number')
--
local log_filename = os.tmpname()
os.execute(('ffmpeg -loglevel panic -y -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 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))
local silences = {}
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.]+)$'
@ -48,6 +55,7 @@ end
local function video_silence_at_end (video_filename, ...)
assert(type(video_filename) == 'string')
assert(check_file_exists(video_filename))
--
local silences = video_silences(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(video_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
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 &> /dev/null'):format(timestamp, silence_filename))
os.execute(('ffmpeg -loglevel panic -y -f lavfi -i anullsrc=channel_layout=5.1:sample_rate=48000 -t %s %s'):format(timestamp, silence_filename))
-- Paste audio onto video
local output_filename = TMP_FOLDER..'output.webm'
local cmd = (('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 &> /dev/null'):format(video_filename, silence_filename, audio_filename, output_filename))
os.execute(cmd)
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))
-- Remove redundant silence
os.remove(silence_filename)
return output_filename
end
@ -107,9 +117,12 @@ end
local function curb_your_video (video_url, timestamp)
assert(type(video_url) == 'string')
assert(type(timestamp) == 'number' or timestamp == nil)
-- Download video
ensure_tmp_folder_exists()
local video_filename = internet.download_video(video_url)
assert(check_file_exists(video_filename))
-- Figure out timestamp to start theme
if timestamp == nil then
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
timestamp = video_length(video_filename) + timestamp
end
print('Timestamp', timestamp, video_filename)
-- Paste audio
print(timestamp)
return paste_audio_onto_video(video_filename, CURB_YOUR_ENTHUSIASM_THEME_FILENAME, timestamp)
end

View File

@ -31,15 +31,29 @@ local function clean_text (text)
return text:gsub('%s+', ' '):match('^%s*(.-)%s*$')
end
local function check_file_exists (filename)
assert(type(filename) == 'string')
local status = os.execute('test -e "'..filename..'"')
return status == 0
end
--------------------------------------------------------------------------------
-- Internet shit
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 target = target_server and target_server ~= 'localhost' and target_server..':'..target_path or target_path
local status = os.execute('scp '..origin..' '..target..' &> /dev/null')
assert(status == 0)
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
local function save_file_to_dcav (filename)
@ -523,13 +537,17 @@ function memes.generate_for_message (user, message)
do -- Curb your enthusiasm
local url = message:match '^[Cc]urb%s+[Yy]our%s+(.+)$'
local url2, timestamp = message:match '^(..-)%s+at%s+(-?[%d.]+)$'
if url2 then url = url2 end
timestamp = timestamp and tonumber(timestamp) or nil
assert(type(timestamp) == 'number' or timestamp == nil)
local video_filename = curb.your_video(url, timestamp)
local video_url = save_file_to_dcav(video_filename)
return video_url, 'CURB'
if url then
local url2, timestamp = message:match '^(..-)%s+at%s+(-?[%d.]+)$'
if url2 then url = url2 end
timestamp = timestamp and tonumber(timestamp) or nil
assert(type(timestamp) == 'number' or timestamp == nil)
local video_filename = curb.your_video(url, timestamp)
assert(check_file_exists(video_filename))
local video_url = save_file_to_dcav(video_filename)
os.remove(video_filename)
return video_url, 'CURB'
end
end
-- Vælter min skorsten