Spaces:
Build error
Build error
Commit
·
fc60743
1
Parent(s):
d21de7b
fix video
Browse files
video.py
CHANGED
|
@@ -1,56 +1,39 @@
|
|
| 1 |
-
from ffmpy import FFmpeg
|
| 2 |
import os
|
| 3 |
-
import subprocess
|
| 4 |
import logging
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
# Set up logging
|
| 8 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 9 |
|
| 10 |
def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
if not os.path.exists(stream_dir):
|
| 14 |
os.makedirs(stream_dir)
|
| 15 |
|
| 16 |
-
# Set
|
| 17 |
-
output_path = stream_dir
|
| 18 |
-
segment_filename = stream_dir
|
| 19 |
-
|
| 20 |
-
# Construct the FFmpeg command
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
'-
|
| 28 |
-
|
| 29 |
-
'-b:a', '192k',
|
| 30 |
-
'-f', 'hls',
|
| 31 |
-
'-hls_time', '10',
|
| 32 |
-
'-hls_list_size', '0',
|
| 33 |
-
'-hls_segment_filename', segment_filename,
|
| 34 |
-
output_path
|
| 35 |
-
]
|
| 36 |
|
| 37 |
try:
|
| 38 |
# Log the command being executed
|
| 39 |
-
logging.debug(f"Running FFmpeg command: {
|
| 40 |
|
| 41 |
# Run the FFmpeg command
|
| 42 |
-
|
| 43 |
-
ffmpeg_command,
|
| 44 |
-
stdout=subprocess.PIPE,
|
| 45 |
-
stderr=subprocess.PIPE
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# Capture stdout and stderr
|
| 49 |
-
stdout, stderr = process.communicate()
|
| 50 |
-
|
| 51 |
-
# Log FFmpeg output
|
| 52 |
-
logging.debug("FFmpeg stdout: %s", stdout.decode())
|
| 53 |
-
logging.error("FFmpeg stderr: %s", stderr.decode())
|
| 54 |
|
| 55 |
# Check if the output file was created
|
| 56 |
if os.path.exists(output_path):
|
|
@@ -58,17 +41,17 @@ def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
|
|
| 58 |
else:
|
| 59 |
logging.error(f"HLS playlist not created at {output_path}")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
return process
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
logging.error(f"Error using FFmpeg to stream file: {e}")
|
| 66 |
return None, None
|
| 67 |
|
|
|
|
| 68 |
# if __name__ == "__main__":
|
| 69 |
# url = "https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/films/Funky%20Monkey%202004/Funky%20Monkey%20(2004)%20Web-dl%201080p.mp4"
|
| 70 |
# token = os.getenv("TOKEN")
|
| 71 |
-
# stream_id,
|
| 72 |
# if stream_id:
|
| 73 |
# logging.info(f"HLS playlist created with stream ID: {stream_id}")
|
| 74 |
# else:
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import logging
|
| 3 |
import uuid
|
| 4 |
+
from ffmpy import FFmpeg
|
| 5 |
|
| 6 |
# Set up logging
|
| 7 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 8 |
|
| 9 |
def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
|
| 10 |
+
# Generate a unique directory for the stream
|
| 11 |
+
unique_id = str(uuid.uuid4())
|
| 12 |
+
stream_dir = os.path.join(output_dir, unique_id)
|
| 13 |
+
|
| 14 |
if not os.path.exists(stream_dir):
|
| 15 |
os.makedirs(stream_dir)
|
| 16 |
|
| 17 |
+
# Set the output paths
|
| 18 |
+
output_path = os.path.join(stream_dir, 'output.m3u8')
|
| 19 |
+
segment_filename = os.path.join(stream_dir, 'segment_%03d.ts')
|
| 20 |
+
|
| 21 |
+
# Construct the FFmpeg command using ffmpy
|
| 22 |
+
ff = FFmpeg(
|
| 23 |
+
inputs={file_url: None},
|
| 24 |
+
outputs={
|
| 25 |
+
output_path: f'-c:v libx264 -crf 23 -preset medium -c:a aac -b:a 192k -f hls '
|
| 26 |
+
f'-hls_time 10 -hls_list_size 0 -hls_segment_filename {segment_filename}'
|
| 27 |
+
},
|
| 28 |
+
global_options=f'-headers "Authorization: Bearer {token}"'
|
| 29 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
# Log the command being executed
|
| 33 |
+
logging.debug(f"Running FFmpeg command: {ff.cmd}")
|
| 34 |
|
| 35 |
# Run the FFmpeg command
|
| 36 |
+
ff.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Check if the output file was created
|
| 39 |
if os.path.exists(output_path):
|
|
|
|
| 41 |
else:
|
| 42 |
logging.error(f"HLS playlist not created at {output_path}")
|
| 43 |
|
| 44 |
+
return unique_id, output_path
|
|
|
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
logging.error(f"Error using FFmpeg to stream file: {e}")
|
| 48 |
return None, None
|
| 49 |
|
| 50 |
+
# Uncomment and modify the following lines if you want to test the script
|
| 51 |
# if __name__ == "__main__":
|
| 52 |
# url = "https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/films/Funky%20Monkey%202004/Funky%20Monkey%20(2004)%20Web-dl%201080p.mp4"
|
| 53 |
# token = os.getenv("TOKEN")
|
| 54 |
+
# stream_id, output_path = ffmpeg_stream(url, token)
|
| 55 |
# if stream_id:
|
| 56 |
# logging.info(f"HLS playlist created with stream ID: {stream_id}")
|
| 57 |
# else:
|