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


Messages In This Thread
RE: Python Flask Realtime system printout (console) ffmpeg - by jttolleson - Apr-18-2022, 06:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  In Console,Python anna17 0 242 Mar-23-2024, 08:24 PM
Last Post: anna17
  Mirror Video Image in realtime makingwithheld 1 487 Oct-30-2023, 02:45 PM
Last Post: Larz60+
  How to 'soft-embedd' subtitles with python-ffmpeg Pavel_47 2 2,210 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,857 Jan-07-2022, 05:41 PM
Last Post: deanhystad
  Need Help With Python RPG Battle System SBanya 1 6,552 Nov-01-2021, 10:38 PM
Last Post: Garudyne
  Python library for win32 console commands eldiener 3 3,557 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  MovieWriter ffmpeg unavailable; using Pillow instead. Joni_Engr 1 30,084 Aug-13-2021, 03:39 PM
Last Post: deanhystad
  Where does the array printout come from? Mark17 2 1,691 Aug-02-2021, 05:42 PM
Last Post: Mark17
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,780 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Problem: Restart kernel onPydev console when trying to install a python package poppy2020 1 7,800 Nov-25-2020, 06:13 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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