add colors to output to make it more readable
This commit is contained in:
parent
caaa3482d7
commit
77e6f43ef6
1 changed files with 33 additions and 22 deletions
55
stream.py
55
stream.py
|
@ -10,20 +10,30 @@ app = Flask(__name__)
|
||||||
BASE_STORAGE_PATH = "/vPath" # Root directory for MKV files
|
BASE_STORAGE_PATH = "/vPath" # Root directory for MKV files
|
||||||
DEBUG_MODE = True # Default: Debugging on
|
DEBUG_MODE = True # Default: Debugging on
|
||||||
|
|
||||||
|
class bcolors:
|
||||||
|
HEADER = '\033[95m'
|
||||||
|
OKBLUE = '\033[94m'
|
||||||
|
OKCYAN = '\033[96m'
|
||||||
|
OKGREEN = '\033[92m'
|
||||||
|
WARNING = '\033[93m'
|
||||||
|
FAIL = '\033[91m'
|
||||||
|
ENDC = '\033[0m'
|
||||||
|
BOLD = '\033[1m'
|
||||||
|
UNDERLINE = '\033[4m'
|
||||||
|
|
||||||
@app.route('/convert/vpath/<path:filepath>.webm')
|
@app.route('/convert/vpath/<path:filepath>.webm')
|
||||||
def convert(filepath):
|
def convert(filepath):
|
||||||
# Log the incoming request
|
# Log the incoming request
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Received request: {request.url}")
|
print(f"{bcolors.OKGREEN}[DEBUG] Received request: {request.url}{bcolors.ENDC}")
|
||||||
print(f"[DEBUG] Mapped file path: {os.path.join(BASE_STORAGE_PATH, filepath + '.mkv')}")
|
print(f"{bcolors.OKGREEN}[DEBUG] Mapped file path: {os.path.join(BASE_STORAGE_PATH, filepath + '.mkv')}{bcolors.ENDC}")
|
||||||
|
|
||||||
mkv_file = os.path.join(BASE_STORAGE_PATH, filepath + ".mkv")
|
mkv_file = os.path.join(BASE_STORAGE_PATH, filepath + ".mkv")
|
||||||
|
|
||||||
# Check if the file exists before proceeding
|
# Check if the file exists before proceeding
|
||||||
if not os.path.isfile(mkv_file):
|
if not os.path.isfile(mkv_file):
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] File not found: {mkv_file}")
|
print(f"{bcolors.FAIL}[ERROR] File not found: {mkv_file}{bcolors.ENDC}")
|
||||||
return "File not found", 404
|
return "File not found", 404
|
||||||
|
|
||||||
def generate():
|
def generate():
|
||||||
|
@ -32,10 +42,10 @@ def convert(filepath):
|
||||||
try:
|
try:
|
||||||
audio_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
|
audio_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Audio codec: {audio_codec_info}")
|
print(f"{bcolors.OKGREEN}[DEBUG] Audio codec: {audio_codec_info}{bcolors.ENDC}")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] Audio probe failed: {e}")
|
print(f"{bcolors.FAIL}[ERROR] Audio probe failed: {e}{bcolors.ENDC}")
|
||||||
audio_codec_info = "unknown"
|
audio_codec_info = "unknown"
|
||||||
|
|
||||||
# Use ffprobe to get the video information
|
# Use ffprobe to get the video information
|
||||||
|
@ -43,10 +53,10 @@ def convert(filepath):
|
||||||
try:
|
try:
|
||||||
video_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
|
video_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Video codec: {video_codec_info}")
|
print(f"{bcolors.WARNING}[DEBUG] Video codec: {video_codec_info}{bcolors.ENDC}")
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] Video probe failed: {e}")
|
print(f"{bcolors.FAIL}[ERROR] Video probe failed: {e}{bcolors.ENDC}")
|
||||||
video_codec_info = "unknown"
|
video_codec_info = "unknown"
|
||||||
|
|
||||||
# Get available encoders in this FFmpeg installation
|
# Get available encoders in this FFmpeg installation
|
||||||
|
@ -54,17 +64,17 @@ def convert(filepath):
|
||||||
try:
|
try:
|
||||||
encoders_output = subprocess.check_output(["ffmpeg", "-encoders"], stderr=subprocess.PIPE).decode('utf-8')
|
encoders_output = subprocess.check_output(["ffmpeg", "-encoders"], stderr=subprocess.PIPE).decode('utf-8')
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print("[DEBUG] Checking available encoders")
|
print("{bcolors.WARNING}[DEBUG] Checking available encoders{bcolors.ENDC}")
|
||||||
for line in encoders_output.split('\n'):
|
for line in encoders_output.split('\n'):
|
||||||
if ' V' in line or ' A' in line: # Video or Audio encoder
|
if ' V' in line or ' A' in line: # Video or Audio encoder
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) > 2:
|
if len(parts) > 2:
|
||||||
available_encoders.append(parts[1])
|
available_encoders.append(parts[1])
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Found encoders: {', '.join(available_encoders)}")
|
print(f"{bcolors.WARNING}[DEBUG] Found encoders: {', '.join(available_encoders)}{bcolors.ENDC}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] Failed to get encoder list: {str(e)}")
|
print(f"{bcolors.FAIL}[ERROR] Failed to get encoder list: {str(e)}{bcolors.ENDC}")
|
||||||
|
|
||||||
# Build FFmpeg command with fallback options
|
# Build FFmpeg command with fallback options
|
||||||
ffmpeg_cmd = ["ffmpeg", "-i", mkv_file]
|
ffmpeg_cmd = ["ffmpeg", "-i", mkv_file]
|
||||||
|
@ -102,9 +112,10 @@ def convert(filepath):
|
||||||
])
|
])
|
||||||
|
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Executing command: {' '.join(ffmpeg_cmd)}")
|
print(f"{bcolors.WARNING}[DEBUG] Executing command: {' '.join(ffmpeg_cmd)}{bcolors.ENDC}")
|
||||||
|
|
||||||
# Start FFmpeg process
|
# Start FFmpeg process
|
||||||
|
print(f"{bcolors.OKGREEN}Dispatching ffmpeg Task with: {' '.join(ffmpeg_cmd)}{bcolors.ENDC}")
|
||||||
process = subprocess.Popen(
|
process = subprocess.Popen(
|
||||||
ffmpeg_cmd,
|
ffmpeg_cmd,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
|
@ -122,7 +133,7 @@ def convert(filepath):
|
||||||
line_text = line.decode('utf-8', errors='replace').strip()
|
line_text = line.decode('utf-8', errors='replace').strip()
|
||||||
error_output.append(line_text)
|
error_output.append(line_text)
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[FFMPEG] {line_text}")
|
print(f"{bcolors.OKCYAN}[FFMPEG] {line_text}{bcolors.ENDC}")
|
||||||
|
|
||||||
stderr_thread = threading.Thread(target=log_stderr)
|
stderr_thread = threading.Thread(target=log_stderr)
|
||||||
stderr_thread.daemon = True
|
stderr_thread.daemon = True
|
||||||
|
@ -142,21 +153,21 @@ def convert(filepath):
|
||||||
received_data = True
|
received_data = True
|
||||||
if byte_count == len(chunk): # First chunk
|
if byte_count == len(chunk): # First chunk
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] First chunk received after {time.time() - start_time:.2f} seconds, size: {len(chunk)} bytes")
|
print(f"{bcolors.WARNING}[DEBUG] First chunk received after {time.time() - start_time:.2f} seconds, size: {len(chunk)} bytes{bcolors.ENDC}")
|
||||||
yield chunk
|
yield chunk
|
||||||
else:
|
else:
|
||||||
# Check if process has ended
|
# Check if process has ended
|
||||||
if process.poll() is not None:
|
if process.poll() is not None:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] FFmpeg process completed with code: {process.returncode}")
|
print(f"{bcolors.WARNING}[DEBUG] FFmpeg process completed with code: {process.returncode}{bcolors.ENDC}")
|
||||||
print(f"[DEBUG] Total bytes streamed: {byte_count}")
|
print(f"{bcolors.WARNING}[DEBUG] Total bytes streamed: {byte_count}{bcolors.ENDC}")
|
||||||
|
|
||||||
# If we never got any data and process exited with error
|
# If we never got any data and process exited with error
|
||||||
if not received_data and process.returncode != 0:
|
if not received_data and process.returncode != 0:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print("[ERROR] FFmpeg failed to produce any output")
|
print("{bcolors.FAIL}[ERROR] FFmpeg failed to produce any output{bcolors.ENDC}")
|
||||||
if error_output:
|
if error_output:
|
||||||
print(f"[ERROR] Last errors: {error_output[-10:]}")
|
print(f"{bcolors.FAIL}[ERROR] Last errors: {error_output[-10:]}{bcolors.ENDC}")
|
||||||
break
|
break
|
||||||
|
|
||||||
# Short wait before trying again
|
# Short wait before trying again
|
||||||
|
@ -165,12 +176,12 @@ def convert(filepath):
|
||||||
# Safety timeout - if no data after 30 seconds, abort
|
# Safety timeout - if no data after 30 seconds, abort
|
||||||
if not received_data and (time.time() - start_time) > 30:
|
if not received_data and (time.time() - start_time) > 30:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print("[ERROR] Timed out waiting for FFmpeg output")
|
print("{bcolors.FAIL}[ERROR] Timed out waiting for FFmpeg output{bcolors.ENDC}")
|
||||||
break
|
break
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] Streaming exception: {str(e)}")
|
print(f"{bcolors.FAIL}[ERROR] Streaming exception: {str(e)}{bcolors.ENDC}")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
try:
|
try:
|
||||||
|
@ -185,10 +196,10 @@ def convert(filepath):
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[ERROR] Process cleanup error: {str(e)}")
|
print(f"{bcolors.FAIL}[ERROR] Process cleanup error: {str(e)}{bcolors.ENDC}")
|
||||||
|
|
||||||
if DEBUG_MODE:
|
if DEBUG_MODE:
|
||||||
print(f"[DEBUG] Streaming ended for: {mkv_file}")
|
print(f"{bcolors.WARNING}[DEBUG] Streaming ended for: {mkv_file}{bcolors.ENDC}")
|
||||||
|
|
||||||
# Set proper headers for streaming
|
# Set proper headers for streaming
|
||||||
headers = {
|
headers = {
|
||||||
|
@ -218,6 +229,6 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
if args.debug:
|
if args.debug:
|
||||||
DEBUG_MODE = True
|
DEBUG_MODE = True
|
||||||
print("[DEBUG] Debug mode enabled")
|
print(f"{bcolors.WARNING}[DEBUG] Debug mode enabled{bcolors.ENDC}")
|
||||||
|
|
||||||
app.run(host="0.0.0.0", port=8080, debug=DEBUG_MODE, threaded=True)
|
app.run(host="0.0.0.0", port=8080, debug=DEBUG_MODE, threaded=True)
|
||||||
|
|
Loading…
Add table
Reference in a new issue