From f21e43f1478d40b32d009e291e31d825945caa09 Mon Sep 17 00:00:00 2001 From: jmaa Date: Wed, 13 Jun 2018 16:06:30 +0200 Subject: [PATCH 1/2] Curb your enthusiasm moster --- curb_your_enthusiasm.lua | 6 +++--- memes.lua | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/curb_your_enthusiasm.lua b/curb_your_enthusiasm.lua index 04cc0f0..dbc3e32 100644 --- a/curb_your_enthusiasm.lua +++ b/curb_your_enthusiasm.lua @@ -28,7 +28,7 @@ local function video_silences (video_filename, decibel, duration) assert(type(duration) == 'number') -- local log_filename = os.tmpname() - 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)) + 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 silences = {} for line in f:lines() do @@ -70,11 +70,11 @@ local function paste_audio_onto_video (video_filename, audio_filename, timestamp -- Generate silence local silence_filename = TMP_FOLDER..'silence.mp3' - os.execute(('ffmpeg -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 &> /dev/null'):format(timestamp, silence_filename)) -- Paste audio onto video local output_filename = TMP_FOLDER..'output.webm' - 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)) + 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) return output_filename diff --git a/memes.lua b/memes.lua index 10e6d5c..c26ecd3 100644 --- a/memes.lua +++ b/memes.lua @@ -38,7 +38,8 @@ end 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 - os.execute('scp '..origin..' '..target..' > /dev/null') + local status = os.execute('scp '..origin..' '..target..' &> /dev/null') + assert(status == 0) end local function save_file_to_dcav (filename) @@ -230,7 +231,7 @@ local function paste_topic_onto_image (target, topic, x, y, w, h, bg_color, font -- Convert svg to png if url:match '%.svg$' then 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 end -- From 8dec886059f5594de2a2e815b06c256ce5ca1258 Mon Sep 17 00:00:00 2001 From: jmaa Date: Wed, 13 Jun 2018 16:47:09 +0200 Subject: [PATCH 2/2] Curb your enthusiasm mostest --- curb_your_enthusiasm.lua | 35 +++++++++++++++++++++++++---------- memes.lua | 36 +++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/curb_your_enthusiasm.lua b/curb_your_enthusiasm.lua index dbc3e32..c66e331 100644 --- a/curb_your_enthusiasm.lua +++ b/curb_your_enthusiasm.lua @@ -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 diff --git a/memes.lua b/memes.lua index c26ecd3..eb29272 100644 --- a/memes.lua +++ b/memes.lua @@ -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