Mar-30-2022, 11:09 AM
Hey everyone - Jayson again; and now that i have started with flask i have attempted one of my older programming ideas with the addition of a realtime printout or 'CONSOLE', of course in Python3.
I would like to use python CGI to display a realtime python subprocess stdout console.
I am having trouble now, getting the console iframe to update, only works when i right click and say reload frame....kinda still my last error is javascript/python
[attachment=1679]
[attachment=1678]
ANY HELP???
here are the 3 pages that are in /cgi-bin serving the web program:
ostemplate.py
I would like to use python CGI to display a realtime python subprocess stdout console.
I am having trouble now, getting the console iframe to update, only works when i right click and say reload frame....kinda still my last error is javascript/python
[attachment=1679]
[attachment=1678]
ANY HELP???
here are the 3 pages that are in /cgi-bin serving the web program:
ostemplate.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- # enable debugging import cgitb cgitb.enable() import cgi import subprocess from youtube_search import YoutubeSearch print ('Content-Type: text/html\n') print ('''<html> <head> <title>J^2</title> <meta name="description" content="=100jay" /> <meta name="keywords" content="Music, Art, Forum" /> <meta http-equiv="content-type" content="text/html" /> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <style> body {background: #1339de;} div#container { background: black; width: 50%; margin: 100px auto; color: white; border-radius: 1em; width: 1200px; height: 720px; overflow:hidden; /* if you don't want a scrollbar, set to hidden */ overflow-x:hidden; /* hides horizontal scrollbar on newer browsers */ /* resize and min-height are optional, allows user to resize viewable area */ -webkit-resize:vertical; -moz-resize:vertical; resize:vertical; // min-height:1600px; } iframe#embed { width:1200px; /* set this to approximate width of entire page you're embedding */ height:720px; /* determines where the bottom of the page cuts off */ margin-left:0px; /* clipping left side of page */ margin-top:0px; /* clipping top of page */ overflow:hidden; /* resize seems to inherit in at least Firefox */ -webkit-resize:none; -moz-resize:none; resize:none; } </style> <head> <script> function refreshIFrame() { var x = document.getElementById("embed"); x.contentWindow.location.reload(); var t = setTimeout(refreshIFrame, 500); } </script> </head> <body> </body> </html> ''') print ('''<html><body onload="refreshIFrame()"><h1>Console Printout</h1><iframe id='embed' src='https://lftr.biz:8000'></iframe> <form> input search terms: <input type = 'name' name = 'searchterms'> input search length(more than 4 takes longer time): <input type = 'number' name = 'number' value = '4'> <br /><input type = 'submit' value = 'Submit' />Make Smash-Up Video</button></form> <script></script></body></html>''') #python programming form = cgi.FieldStorage() searchterms = form.getvalue('searchterms') number = form.getvalue('number') if searchterms != None: subprocess.Popen('sudo python3 /var/video/cgi-bin/run.py '+str(searchterms)+' '+str(number), shell=True) print ('''<html><body> <script type="text/javascript" > function go() { window.location.href = "https://lftr.biz:8080/movies/output2.mp4"; } </script> <h3>Watch Movie After Output is done...</h3> <form action='https://lftr.biz:8080/movies/output2.mp4' onsubmit='go();'> <input type="submit" value="Watch Movie!!!"> </form> </body></html>''')Run.py
#!/usr/bin/env python3 # -*- coding: UTF-8 -*- # enable debugging import cgitb cgitb.enable() import cgi import sys import subprocess from subprocess import Popen import time from flask import Flask, Response searchterms = (sys.argv[1]) number = (sys.argv[2]) app = Flask(__name__) @app.route('/') def index(): def g(): x = int(0) yield """<!doctype html><html><body><title>YTSmashUp Console</title><style> #data { text-align: center; }</style><div id="data">nothing received yet...</div>""" proc = subprocess.Popen('sudo python3 /var/video/cgi-bin/ytsmash.py '+str(searchterms)+' '+str(number), shell=True) yield """<script>parent.document.getElementById("embed").reload();</script><script>var div = document.getElementById('data');</script></body></html>""" while proc.poll() is None: x = x + 1 output = str(proc.stdout) yield """<html><body><script>div.innerHTML = 'OUTPUT line # {0}' + ' : ' + '{1} --Response';</script></body></html>""".format(x, output) return Response(g()) if __name__ == "__main__": app.run(host='0.0.0.0', debug=True, ssl_context=('/var/security/lftr.biz.crt', '/var/security/lftr.biz.key'), port=8000)ytsmash.py
#!/usr/bin/env python # -*- coding: UTF-8 -*- # enable debugging import cgitb cgitb.enable() import cgi import os import subprocess import bs4 import requests from pytube import YouTube import re from youtube_search import YoutubeSearch import sys #python programming searchterms = str(sys.argv[1]) number = int(sys.argv[2]) videos = [] videos = YoutubeSearch(searchterms, max_results=number).to_dict() remove = ('cd /var/video/ && sudo rm -r videos && sudo rm -r videos2 && sudo mkdir videos && sudo mkdir videos2') subprocess.call(remove, shell=True) #download yt vids for v in videos: url_suffix = v['url_suffix'] link = 'https://youtube.com'+(str(url_suffix)) try: YouTube(link).streams.filter(res="720p").first().download('/var/video/videos') except: print ('1 dload failed!') print ('Download Completed!') print ('Download Task Completed!') #slice up video os.chdir('/var/video/videos') for count, f in enumerate(os.listdir()): f_name, f_ext = os.path.splitext(f) f_name = str(count) new_name = f'{f_name}{f_ext}' os.rename(f, new_name) list = os.listdir('/var/video/videos/') f= open("/var/video/videos2/vidslist.txt","w+") for l in list: intro = ('sudo ffmpeg -ss 0:00 -i /var/video/videos/'+str(l)+' -t 7.0 -c copy -y /var/video/videos2/intro'+str(l)) subprocess.call(intro, shell=True) f.write("file intro'%s'\r\n" % l) for l in list: middle1 = ('sudo ffmpeg -ss 0:08 -i /var/video/videos/'+str(l)+' -t 22.0 -c copy -y /var/video/videos2/middle1'+str(l)) subprocess.call(middle1, shell=True) f.write("file middle1'%s'\r\n" % l) for l in list: middle2 = ('sudo ffmpeg -ss 0:28 -i /var/video/videos/'+str(l)+' -t 12.0 -c copy -y /var/video/videos2/middle2'+str(l)) subprocess.call(middle2, shell=True) f.write("file middle2'%s'\r\n" % l) for l in list: middle3 = ('sudo ffmpeg -ss 0:45 -i /var/video/videos/'+str(l)+' -t 25.0 -c copy -y /var/video/videos2/middle3'+str(l)) subprocess.call(middle3, shell=True) f.write("file middle3'%s'\r\n" % l) for l in list: middle4 = ('sudo ffmpeg -ss 1:00 -i /var/video/videos/'+str(l)+' -t 7.0 -c copy -y /var/video/videos2/middle4'+str(l)) subprocess.call(middle4, shell=True) f.write("file middle4'%s'\r\n" % l) for l in list: end = ('sudo ffmpeg -ss 1:24 -i /var/video/videos/'+str(l)+' -t 6.0 -c copy -y /var/video/videos2/end'+str(l)) subprocess.call(end, shell=True) f.write("file end'%s'\r\n" % l) f.close() print ('Slicing Completed!') #make smashup video and serve it #ffmpeg = ('''cd /var/video/videos2/ && sudo ffmpeg -f concat -i /var/video/videos2/vidslist.txt -c copy -y /var/video/movies/input.mp4 && sudo ffmpeg -i /var/video/movies/input.mp4 -map 0:v -c:v copy -bsf:v h264_mp4toannexb -y /var/video/movies/raw.h264 && sudo ffmpeg -fflags +genpts -r 120 -i /var/video/movies/raw.h264 -i /var/video/movies/input.mp4 -map 0:v -c:v copy -map 1:a -af atempo=2 -movflags faststart -y /var/video/movies/output.mp4 && sudo ffmpeg -i /var/video/movies/output.mp4 -vf mpdecimate,setpts=N/FRAME_RATE/TB -y /var/video/movies/output2.mp4 && cd /var/video/movies && sudo cp output2.mp4 '''+str(searchterms).replace(' ',',').replace('/',',')+'''.mp4''') #subprocess.call(ffmpeg, shell=True) proc = subprocess.Popen('npx kill-port 8000', shell=True, stdout=subprocess.PIPE) while proc.poll() is None: out = proc.stdout.readline() print (out)