Spaces:
Paused
Paused
from ffmpy import FFmpeg | |
import os | |
import logging | |
import subprocess | |
# Set up logging | |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') | |
def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"): | |
stream_dir = output_dir | |
if not os.path.exists(stream_dir): | |
os.makedirs(stream_dir) | |
# Set up the FFmpeg command for HLS | |
output_path = os.path.join(stream_dir, 'output.m3u8') | |
segment_filename = os.path.join(stream_dir, 'segment_%03d.ts') | |
ffmpeg = FFmpeg( | |
inputs={file_url: None}, | |
outputs={ | |
output_path: ( | |
'-c:v libx264 ' | |
'-crf 23 ' | |
'-preset medium ' | |
'-c:a aac ' | |
'-b:a 192k ' | |
'-f hls ' | |
'-hls_time 10 ' | |
'-hls_list_size 0 ' | |
'-hls_segment_filename ' + segment_filename | |
) | |
}, | |
global_options='-headers "Authorization: Bearer ' + token + '"' | |
) | |
try: | |
# Log the command being executed | |
command_str = ' '.join(ffmpeg.cmd) | |
logging.debug(f"Running FFmpeg command: {command_str}") | |
# Run the FFmpeg command | |
ffmpeg.run(stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
# Check if the output file was created | |
if os.path.exists(output_path): | |
logging.info(f"HLS playlist created at {output_path}") | |
else: | |
logging.error(f"HLS playlist not created at {output_path}") | |
except Exception as e: | |
logging.error(f"Error using FFmpeg to stream file: {e}") | |
# if __name__ == "__main__": | |
# url = "https://huggingface.co/Unicone-Studio/jellyfin_media/resolve/main/films/Funky%20Monkey%202004/Funky%20Monkey%20(2004)%20Web-dl%201080p.mp4" | |
# token = os.getenv("TOKEN") | |
# ffmpeg_stream(url, token) | |