Skip to content

Commit

Permalink
need local logging for dev , adding basic config logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thelamer committed Jul 21, 2019
1 parent 3545c6b commit 71deb01
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 30 deletions.
12 changes: 5 additions & 7 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
---

interval: 60
enabled: false

commands:
- name: "test1"
extension: ".mkv"
delete: false
command: |
ffmpeg -i "${INPUTFILE}" \
ffmpeg -i "INPUTFILE" \
-vf scale=-1:720 \
-c:v h264 \
-c:a copy \
-b:v 4000k \
"${OUTPUTFILE}"
"OUTPUTFILE"
- name: "test2"
extension: ".mkv"
delete: true
command: |
ffmpeg -i "${INPUTFILE}" \
ffmpeg -i "INPUTFILE" \
-vf scale=-1:720 \
-c:v h264 \
-c:a copy \
-b:v 4000k \
"${OUTPUTFILE}"
"OUTPUTFILE"
34 changes: 24 additions & 10 deletions public/ffmpeg-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ function renderconfig(){
}
socket.on('sendconfig', function(rules){
pagepurge();
var interval = rules.interval;
var enabled = rules.enabled;
if (enabled){
var options = '<option selected value=1>on</option>\
<option value=0>off</option>'
}
else{
var options = '<option selected value=0>off</option>\
<option value=1>on</option>'
}
$('#headerform').append('\
<select class="custom-select form-control mr-sm-2" id="interval">\
<option selected value="' + interval + '">' + interval + '</option>\
<option value"Never">Never</option>\
<option value="30">30 seconds</option>\
<option value="60">60 seconds</option>\
<option value="300">5 minutes</option>\
</select>\
<div class="form-group">\
<label for="email">Processing: </label>\
<select class="custom-select form-control mr-sm-2" id="enabled">\
' + options + '\
</select>\
</div>\
<button style="cursor:pointer;" onclick="saveall()" class="btn btn-primary my-2 my-sm-0" type="submit">Save Config</button>\
');
var editor = [];
Expand Down Expand Up @@ -126,8 +133,14 @@ function addconfig(){
}
// Save all config data
function saveall(){
var interval = $('#interval').val();
var dataset = {'interval':interval,'commands':[]};
var enabled = $('#enabled').val();
if (enabled == 0){
var enabled = false;
}
else{
var enabled = true;
}
var dataset = {'enabled':enabled,'commands':[]};
var ids = $('.rule').map(function(){
return this.id;
}).get();
Expand All @@ -141,6 +154,7 @@ function saveall(){
dataset.commands.push(single_command);
}).promise().done(function(){
socket.emit('saveconfig',dataset);
renderconfig();
});
}

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
flask
flask_socketio
pyyaml
16 changes: 16 additions & 0 deletions root/etc/cont-init.d/30-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/with-contenv bash

# check if config file exists in /config
[[ ! -f /config/config.yml ]] && \
cp /app/ffmpeg-web/config.yml /config/config.yml

# permissions
chown -R abc:abc \
/config
mkdir -p \
/in \
/out
chown abc:abc \
/in \
/out

7 changes: 6 additions & 1 deletion root/etc/services.d/web/run
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#!/usr/bin/with-contenv bash

# Make log output for dev
mkdir -p /applogs
chown -R abc:abc /applogs

# Run App in development mode
cd /app/ffmpeg-web
exec \
s6-setuidgid abc \
watchmedo auto-restart \
-d . \
-p './server.py' \
-- python3 server.py
-- python3 server.py \
>> /applogs/applog.txt 2>&1
59 changes: 47 additions & 12 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask, send_from_directory, request
from flask_socketio import SocketIO
from pathlib import Path
import glob
import os
import pty
Expand All @@ -9,16 +10,14 @@
import time
import yaml
# Error logging only
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
#import logging
#log = logging.getLogger('werkzeug')
#log.setLevel(logging.ERROR)

# Websocket server
app = Flask(__name__,static_folder="public")
sio = SocketIO(app)



################################
# Job functions #
################################
Expand All @@ -31,15 +30,47 @@ def build_list(extension):
all_files = glob.glob('/in/**/*' + extension_regex, recursive=True)
# Check if anything in this array has a processed log or is not a file and pull it out
for file in all_files:
# Remove files from processing list if they have log files and no processing pid
if not os.path.isfile(file) or os.path.isfile(file + '.ffmpeg_log'):
all_files.remove(file)
if not os.path.isfile(file + '.ffmpeg_processing'):
all_files.remove(file)
return all_files

# run individual job
def run_job(file,job_command):
try:
# Create a processing file on disk
os.remove(file + '.ffmpeg_processing')
os.mknod(file + '.ffmpeg_processing')
# Create output structure
filename = os.path.basename(file)
outpath = Path('/out/' + os.path.dirname(os.path.abspath(file)).replace('/in/', '', 1))
outpath.mkdir(parents=True)
# Remove processing file on disk
time.sleep(5)
os.remove(file + '.ffmpeg_processing')
except OSError:
pass

# run jobs based on config
def run_jobs(config):
for command in config['commands']:
file_list = build_list(command['extension'])
for file in file_list:
run_job(file,command['command'])

# Background job thread loop for file processing
def processor():
while True:
files = build_list('.mkv')
sio.emit('testout', files)
with open("/config/config.yml", 'r') as stream:
try:
config = yaml.safe_load(stream)
if config['enabled'] is True:
run_jobs(config)
except yaml.YAMLError as e:
print(e)
# Loop every 5 seconds to check config file and run jobs
stream.close()
time.sleep(5)
sio.start_background_task(processor)

Expand All @@ -56,7 +87,7 @@ def index():
# Send the current config to the user to render
@sio.on('getconfig')
def config():
with open("./config.yml", 'r') as stream:
with open("/config/config.yml", 'r') as stream:
try:
config = yaml.safe_load(stream)
sio.emit('sendconfig', config, room=request.sid)
Expand All @@ -75,9 +106,13 @@ def commands():

# Main page for rendering processing history and current
@sio.on('getmain')
def commands():
data = 'test'
sio.emit('sendmain', data, room=request.sid)
def main():
with open("/config/config.yml", 'r') as stream:
try:
config = yaml.safe_load(stream)
sio.emit('sendmain', config, room=request.sid)
except yaml.YAMLError as e:
print(e)

# Save user set config
@sio.on('saveconfig')
Expand Down

0 comments on commit 71deb01

Please sign in to comment.