Spaces:
Sleeping
Sleeping
Chandima Prabhath
commited on
Commit
·
21b1e7f
1
Parent(s):
c685973
Fix DEBUG flag and TEMP_DIR path in EncodingConfig; enhance logging in FFmpegEncoder
Browse files- video_encoder/config.py +2 -2
- video_encoder/worker/encoder.py +19 -19
video_encoder/config.py
CHANGED
|
@@ -10,8 +10,8 @@ import logging
|
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
class EncodingConfig:
|
| 13 |
-
DEBUG = os.getenv("DEBUG", "
|
| 14 |
-
TEMP_DIR = "
|
| 15 |
LOG_LEVEL = logging.DEBUG if DEBUG else logging.INFO
|
| 16 |
|
| 17 |
logging.basicConfig(level=EncodingConfig.LOG_LEVEL)
|
|
|
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
class EncodingConfig:
|
| 13 |
+
DEBUG = os.getenv("DEBUG", "True") == "True"
|
| 14 |
+
TEMP_DIR = "/tmp"
|
| 15 |
LOG_LEVEL = logging.DEBUG if DEBUG else logging.INFO
|
| 16 |
|
| 17 |
logging.basicConfig(level=EncodingConfig.LOG_LEVEL)
|
video_encoder/worker/encoder.py
CHANGED
|
@@ -3,6 +3,9 @@ import os
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import List, Dict
|
| 5 |
from ..config import EncodingConfig
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class FFmpegEncoder:
|
| 8 |
def __init__(self, input_path: str, output_dir: str):
|
|
@@ -39,25 +42,22 @@ class FFmpegEncoder:
|
|
| 39 |
commands.append(cmd)
|
| 40 |
return commands
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def encode(self) -> str:
|
| 46 |
-
"""Execute encoding commands and return master playlist path"""
|
| 47 |
-
master_playlist = os.path.join(self.output_dir, f"{self.base_name}_master.m3u8")
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from typing import List, Dict
|
| 5 |
from ..config import EncodingConfig
|
| 6 |
+
import logging
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
|
| 10 |
class FFmpegEncoder:
|
| 11 |
def __init__(self, input_path: str, output_dir: str):
|
|
|
|
| 42 |
commands.append(cmd)
|
| 43 |
return commands
|
| 44 |
|
| 45 |
+
def encode(self) -> str:
|
| 46 |
+
"""Execute encoding commands and return master playlist path"""
|
| 47 |
+
master_playlist = os.path.join(self.output_dir, f"{self.base_name}_master.m3u8")
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
with open(master_playlist, "w") as f:
|
| 50 |
+
f.write("#EXTM3U\n")
|
| 51 |
+
for res in reversed(EncodingConfig.RESOLUTIONS):
|
| 52 |
+
f.write(f"#EXT-X-STREAM-INF:BANDWIDTH={res['video_bitrate'].replace('k', '000')},"
|
| 53 |
+
f"RESOLUTION={res['width']}x{res['height']}\n")
|
| 54 |
+
f.write(f"{self.base_name}_{res['name']}.m3u8\n")
|
| 55 |
|
| 56 |
+
for cmd in self.generate_commands():
|
| 57 |
+
result = subprocess.run(cmd, capture_output=True)
|
| 58 |
+
logger.debug(f"FFmpeg command: {' '.join(cmd)}")
|
| 59 |
+
logger.debug(f"FFmpeg output: {result.stdout.decode()}")
|
| 60 |
+
logger.debug(f"FFmpeg error: {result.stderr.decode()}")
|
| 61 |
+
result.check_returncode()
|
| 62 |
|
| 63 |
+
return master_playlist
|