Python Forum
Python Flask Realtime system printout (console) ffmpeg
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Flask Realtime system printout (console) ffmpeg
#1
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

#!/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)
Reply
#2
(Mar-30-2022, 11:09 AM)jttolleson Wrote: I would like to use python CGI to display a realtime python subprocess stdout console
As mention in your other post you most stop using python CGI,
no one will help when you use CGI as it has been dead💀 in Python for a long time.
PEP 594 – Removing dead batteries from the standard library
Quote:cgi 3.11 (2.0**) 3.13 1995 no -
cgitb 3.11 (2.0**) 3.13 1995 no -
Reply
#3
While I am dense, i looked up more flask and dropped the CGI.

so, i have the code below that i am working on and it seems to have some serious promise as far as following the subprocess with print outs on web while in background the video is being compiled.

The main error now is that i am having trouble getting the form data from the html headers back into the flask situation.... using request.value.get on either post or get is not working...
long shot for some help again...


.mp4   pyconsole.mp4 (Size: 624.71 KB / Downloads: 71)

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import subprocess
from subprocess import Popen, PIPE, STDOUT
from flask import Flask, Response, request
import time
app = Flask(__name__)
@app.route('/', methods =["GET", "POST"])
def html():
    def input():
        yield """<html><head><meta http-equiv="content-type" content="text/html" /> <meta name="viewport" content="width=device-width, initial-scale=1"></head>
<style>body {background: #1339de;}</style>
<body><form action='/out' methods =['GET','POST']>
   Search Terms: <input type="text" name="searchterms" id="searchterms">
   Query Length: <input type="number" name="number" id="number" value="4">
   <input type="submit" value="Make a YouTube SmashUp!!!">
</form></body></html>"""
    return Response(input())
@app.route('/out', methods =["GET","POST"])
def html2():
    def output():
        yield """<html><head><meta http-equiv="content-type" content="text/html" /> <meta name="viewport" content="width=device-width, initial-scale=1"></head>
<style>body {background: #1339de;} #data { text-align: center; }</style>
<body><h1><div id="data">nothing received yet...</div></h1>
<script>var div = document.getElementById('data');</script></body></html>"""
    return Response(output())
@app.route('/out', methods =["GET","POST"])
def smash():
        searchterms = request.value.get("searchterms")
        number = request.value.get("number") 
        if searchterms != None:
            process = subprocess.Popen('sudo python3 /var/video/cgi-bin/ytsmash.py '+str(searchterms)+' '+str(number), shell=True, stdout=PIPE, stderr=STDOUT)
            while True:
                out = str((process.stdout.readline()).strip()) 
                if out != "b''":
                    print (out)
                    def print():
                        yield """<html><body><h1><script>div.innerHTML = "OUTPUT: {0}";</script></h1></body></html>""".format(out)
                    return Response(print())
                    time.sleep(.05)
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)
Reply
#4
Hi all, i have lightened up used flask, well drilled w flask and now IT WORKS GREAT!!! the console works and the youtube smash up script work, together also.....so ......here they are the python3 scripts with flask and ffmpeg and yt dlp.

input-console.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-


import subprocess
from subprocess import Popen, PIPE, STDOUT
from flask import Flask, Response, request, render_template, redirect, url_for
import time

app = Flask(__name__)

@app.route('/out/<searchterms>/<number>')
def out(searchterms, number):
  def output():
    yield """<html><body style='color:MediumSeaGreen;'><h1><div id='data' style='text-align: center;'>nothing received yet...for </div></h1><script>var div = document.getElementById('data');</script></body></html>"""
    p = subprocess.Popen('sudo python3 /var/video/cgi-bin/ytsmash.py '+str(searchterms)+' '+str(number), shell=True, stdout=subprocess.PIPE, stderr=STDOUT)
    while True:
        out = ((p.stdout.readline()).strip()) 
        out =str(out)
        if out != "b''":
            print (out)
            yield """<html><body><h1><script>div.innerHTML = "OUTPUT: """+out+""" " ;</script></h1></body></html>"""
            time.sleep(.27)
    else:
        yield """<html><body><h3>Watch Movie After Output is done...</h3><form action='https://lftr.biz:8080/movies/output2.mp4'>
<input type="submit" value="Watch Movie!!!"></form></body></html>"""
  return Response(output())

@app.route('/ytsmash',methods = ['POST', 'GET'])
def searchterms():
    if request.method == 'POST':
        searchterms = request.form['searchterms']
        number = request.form['number']
        return redirect(url_for('out',searchterms = searchterms, number = number))
    else:
        searchterms = request.args.get('searchterms')
        number = request.args.get('number')
        return """<html><style>body {background: #1339de;} #data { text-align: center; }</style><body>
<form method ='POST'>
   Search Terms: <input type="text" name="searchterms" id="searchterms">
   Query Length: <input type="number" name="number" id="number" value="4">
   <input type="submit" value="Make a YouTube SmashUp!!!">
</form></body></html>"""

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 -*-

import os
import subprocess
from subprocess import Popen, PIPE, STDOUT
from pytube import YouTube
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') 
make = ('cd /var/video && sudo mkdir videos && sudo mkdir videos2')
subprocess.call(remove, shell=True, stderr=subprocess.STDOUT)
subprocess.call(make, shell=True, stderr=subprocess.STDOUT)



#download yt vids
for v in videos:
    url_suffix = v['url_suffix']
    link = 'https://youtube.com'+(str(url_suffix))
    try: 
        dl = ('cd /var/video/videos && sudo yt-dlp -f 135 %s')%(link)
        subprocess.call(dl, shell=True, stderr=subprocess.STDOUT)
   #     YouTube(link).streams.filter(res="720p",file_extension='mp4').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/')
print (list)
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, stderr=subprocess.STDOUT)
        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, stderr=subprocess.STDOUT)
        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, stderr=subprocess.STDOUT)
        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, stderr=subprocess.STDOUT)
        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, stderr=subprocess.STDOUT)
        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, stderr=subprocess.STDOUT)
        f.write("file end'%s'\r\n" % l)
f.close()
print ('finished slicing videos')
print ('Starting FFMPEG...')
#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''')
proc = subprocess.Popen(ffmpeg, shell=True, stdout=subprocess.PIPE)
#proc = subprocess.Popen('npx kill-port 8000', shell=True, stdout=subprocess.STDOUT)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  In Console,Python anna17 0 184 Mar-23-2024, 08:24 PM
Last Post: anna17
  Mirror Video Image in realtime makingwithheld 1 426 Oct-30-2023, 02:45 PM
Last Post: Larz60+
  How to 'soft-embedd' subtitles with python-ffmpeg Pavel_47 2 2,098 Jul-04-2022, 12:33 PM
Last Post: Pavel_47
  time setup for realtime plotting of serial datas at high sampling rate alice93 6 3,735 Jan-07-2022, 05:41 PM
Last Post: deanhystad
  Python library for win32 console commands eldiener 3 3,447 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  MovieWriter ffmpeg unavailable; using Pillow instead. Joni_Engr 1 29,103 Aug-13-2021, 03:39 PM
Last Post: deanhystad
  Where does the array printout come from? Mark17 2 1,627 Aug-02-2021, 05:42 PM
Last Post: Mark17
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,602 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Problem: Restart kernel onPydev console when trying to install a python package poppy2020 1 7,670 Nov-25-2020, 06:13 PM
Last Post: Larz60+
  to call via console a python-programm Liki 9 3,209 Nov-22-2020, 01:10 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020