video_translator / validator.py
sergey.agapov
initial commit
d80df3d
from flask import Flask, render_template, request, jsonify, send_from_directory
import subprocess
import json
import psutil
import time
import signal
import os
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # This will allow all domains to make requests to your server
script_process = None
SCRIPT_NAME = 'translator.py'
def find_process_by_name(name):
for process in psutil.process_iter(['pid', 'name', 'cmdline']):
cmdline = process.info['cmdline']
if cmdline and any(name in arg for arg in cmdline):
return process
return None
def terminate_script():
global script_process
process = find_process_by_name(SCRIPT_NAME)
if process:
print(f"Terminating existing script process (PID: {process.pid})")
process.send_signal(signal.SIGTERM)
try:
process.wait(timeout=20) # Wait up to 10 seconds for the process to terminate
except psutil.TimeoutExpired:
print(f"Process {process.pid} did not terminate in time, forcing...")
process.kill() # Force kill if it doesn't terminate
# Double-check if the process is really terminated
if not find_process_by_name(SCRIPT_NAME):
print(f"Process {SCRIPT_NAME} successfully terminated")
else:
print(f"Warning: Process {SCRIPT_NAME} could not be terminated")
else:
print(f"No running process found with name: {SCRIPT_NAME}")
script_process = None
@app.route('/', methods=['GET'])
def index():
return render_template('tiktok_player.html')
@app.route('/terminate', methods=['POST'])
def terminate():
terminate_script()
return jsonify({'status': 'success', 'message': 'Stream stopped'})
@app.route('/stream/<path:filename>')
def serve_file(filename):
return send_from_directory('/tmp/dash/test_stream', filename)
@app.route('/check_flv', methods=['POST'])
def check_flv():
global script_process
flv_url = request.form['url']
model = request.form['model']
try:
# Use ffprobe to check the FLV stream
result = subprocess.run([
'ffprobe',
'-v', 'quiet',
'-print_format', 'json',
'-show_streams',
flv_url
], capture_output=True, text=True, timeout=10)
if result.returncode == 0:
# Parse the JSON output
probe_data = json.loads(result.stdout)
# Check if there are any streams in the output
if 'streams' in probe_data and len(probe_data['streams']) > 0:
# Stream is valid
# Terminate existing script if running
terminate_script()
# Start new script
new_process = subprocess.Popen(['python', 'translator.py', '--rtmp_url', flv_url,
'--output_directory', '/tmp/dash/test_stream/', '--model', model])
script_process = psutil.Process(new_process.pid)
return jsonify({'status': 'success', 'message': 'Buffering...'})
else:
return jsonify({'status': 'error', 'message': 'No valid streams found in the FLV'})
else:
# Stream is invalid
return jsonify({'status': 'error', 'message': 'Invalid FLV stream'})
except subprocess.TimeoutExpired:
return jsonify({'status': 'error', 'message': 'Timeout while checking FLV stream'})
except Exception as e:
return jsonify({'status': 'error', 'message': f'Error: {str(e)}'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)