Spaces:
Sleeping
Sleeping
File size: 3,620 Bytes
12b350f d80df3d 12b350f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
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)
|