From 77e6f43ef64a051c1aa8e7235426281c0455899c Mon Sep 17 00:00:00 2001
From: byte <byteturtle@byteturtle.eu>
Date: Fri, 7 Mar 2025 17:06:27 +0000
Subject: [PATCH] add colors to output to make it more readable

---
 stream.py | 55 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 33 insertions(+), 22 deletions(-)

diff --git a/stream.py b/stream.py
index 95c41b7..886e775 100644
--- a/stream.py
+++ b/stream.py
@@ -10,20 +10,30 @@ app = Flask(__name__)
 BASE_STORAGE_PATH = "/vPath"  # Root directory for MKV files
 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')
 def convert(filepath):
     # Log the incoming request
     if DEBUG_MODE:
-        print(f"[DEBUG] Received request: {request.url}")
-        print(f"[DEBUG] Mapped file path: {os.path.join(BASE_STORAGE_PATH, filepath + '.mkv')}")
+        print(f"{bcolors.OKGREEN}[DEBUG] Received request: {request.url}{bcolors.ENDC}")
+        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")
 
     # Check if the file exists before proceeding
     if not os.path.isfile(mkv_file):
         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
 
     def generate():
@@ -32,10 +42,10 @@ def convert(filepath):
         try:
             audio_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
             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:
             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"
 
         # Use ffprobe to get the video information
@@ -43,10 +53,10 @@ def convert(filepath):
         try:
             video_codec_info = subprocess.check_output(probe_cmd, stderr=subprocess.PIPE).decode('utf-8').strip()
             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:
             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"
 
         # Get available encoders in this FFmpeg installation
@@ -54,17 +64,17 @@ def convert(filepath):
         try:
             encoders_output = subprocess.check_output(["ffmpeg", "-encoders"], stderr=subprocess.PIPE).decode('utf-8')
             if DEBUG_MODE:
-                print("[DEBUG] Checking available encoders")
+                print("{bcolors.WARNING}[DEBUG] Checking available encoders{bcolors.ENDC}")
             for line in encoders_output.split('\n'):
                 if ' V' in line or ' A' in line:  # Video or Audio encoder
                     parts = line.split()
                     if len(parts) > 2:
                         available_encoders.append(parts[1])
             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:
             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
         ffmpeg_cmd = ["ffmpeg", "-i", mkv_file]
@@ -102,9 +112,10 @@ def convert(filepath):
         ])
         
         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
+        print(f"{bcolors.OKGREEN}Dispatching ffmpeg Task with: {' '.join(ffmpeg_cmd)}{bcolors.ENDC}")
         process = subprocess.Popen(
             ffmpeg_cmd,
             stdout=subprocess.PIPE,
@@ -122,7 +133,7 @@ def convert(filepath):
                 line_text = line.decode('utf-8', errors='replace').strip()
                 error_output.append(line_text)
                 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.daemon = True
@@ -142,21 +153,21 @@ def convert(filepath):
                     received_data = True
                     if byte_count == len(chunk):  # First chunk
                         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
                 else:
                     # Check if process has ended
                     if process.poll() is not None:
                         if DEBUG_MODE:
-                            print(f"[DEBUG] FFmpeg process completed with code: {process.returncode}")
-                            print(f"[DEBUG] Total bytes streamed: {byte_count}")
+                            print(f"{bcolors.WARNING}[DEBUG] FFmpeg process completed with code: {process.returncode}{bcolors.ENDC}")
+                            print(f"{bcolors.WARNING}[DEBUG] Total bytes streamed: {byte_count}{bcolors.ENDC}")
                         
                         # If we never got any data and process exited with error
                         if not received_data and process.returncode != 0:
                             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:
-                                    print(f"[ERROR] Last errors: {error_output[-10:]}")
+                                    print(f"{bcolors.FAIL}[ERROR] Last errors: {error_output[-10:]}{bcolors.ENDC}")
                         break
                     
                     # Short wait before trying again
@@ -165,12 +176,12 @@ def convert(filepath):
                 # Safety timeout - if no data after 30 seconds, abort
                 if not received_data and (time.time() - start_time) > 30:
                     if DEBUG_MODE:
-                        print("[ERROR] Timed out waiting for FFmpeg output")
+                        print("{bcolors.FAIL}[ERROR] Timed out waiting for FFmpeg output{bcolors.ENDC}")
                     break
         
         except Exception as e:
             if DEBUG_MODE:
-                print(f"[ERROR] Streaming exception: {str(e)}")
+                print(f"{bcolors.FAIL}[ERROR] Streaming exception: {str(e)}{bcolors.ENDC}")
                 
         finally:
             try:
@@ -185,10 +196,10 @@ def convert(filepath):
                 
             except Exception as e:
                 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:
-                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
     headers = {
@@ -218,6 +229,6 @@ if __name__ == "__main__":
 
     if args.debug:
         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)