Spaces:
Paused
Paused
Commit
·
7f9534f
1
Parent(s):
4587aee
fix video
Browse files- app.py +5 -6
- requirements.txt +2 -1
- video.py +28 -33
app.py
CHANGED
@@ -19,11 +19,11 @@ CACHE_DIR = os.getenv("CACHE_DIR")
|
|
19 |
if not os.path.exists(CACHE_DIR):
|
20 |
os.makedirs(CACHE_DIR)
|
21 |
|
22 |
-
indexer()
|
23 |
-
|
24 |
if not os.path.exists(INDEX_FILE):
|
25 |
raise FileNotFoundError(f"{INDEX_FILE} not found. Please make sure the file exists.")
|
26 |
|
|
|
|
|
27 |
with open(INDEX_FILE, 'r') as f:
|
28 |
file_structure = json.load(f)
|
29 |
|
@@ -102,10 +102,9 @@ def generate(file_url):
|
|
102 |
|
103 |
# Set up HLS streaming
|
104 |
token = TOKEN
|
105 |
-
|
106 |
return stream_id
|
107 |
|
108 |
-
|
109 |
thread = Thread(target=start_prefetching)
|
110 |
thread.daemon = True
|
111 |
thread.start()
|
@@ -219,8 +218,8 @@ def stream_video():
|
|
219 |
|
220 |
@app.route('/stream/<stream_id>')
|
221 |
def stream_file(stream_id):
|
222 |
-
stream_dir = CACHE_DIR
|
223 |
-
playlist_path = stream_dir
|
224 |
|
225 |
if os.path.exists(playlist_path):
|
226 |
return Response(
|
|
|
19 |
if not os.path.exists(CACHE_DIR):
|
20 |
os.makedirs(CACHE_DIR)
|
21 |
|
|
|
|
|
22 |
if not os.path.exists(INDEX_FILE):
|
23 |
raise FileNotFoundError(f"{INDEX_FILE} not found. Please make sure the file exists.")
|
24 |
|
25 |
+
indexer()
|
26 |
+
|
27 |
with open(INDEX_FILE, 'r') as f:
|
28 |
file_structure = json.load(f)
|
29 |
|
|
|
102 |
|
103 |
# Set up HLS streaming
|
104 |
token = TOKEN
|
105 |
+
_, _ = ffmpeg_stream(file_url, token, output_dir=output_dir, stream_id=stream_id)
|
106 |
return stream_id
|
107 |
|
|
|
108 |
thread = Thread(target=start_prefetching)
|
109 |
thread.daemon = True
|
110 |
thread.start()
|
|
|
218 |
|
219 |
@app.route('/stream/<stream_id>')
|
220 |
def stream_file(stream_id):
|
221 |
+
stream_dir = os.path.join(CACHE_DIR, "stream", stream_id)
|
222 |
+
playlist_path = os.path.join(stream_dir, 'output.m3u8')
|
223 |
|
224 |
if os.path.exists(playlist_path):
|
225 |
return Response(
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ flask
|
|
2 |
Flask-Cors
|
3 |
requests
|
4 |
python-dotenv
|
5 |
-
ffmpy
|
|
|
|
2 |
Flask-Cors
|
3 |
requests
|
4 |
python-dotenv
|
5 |
+
ffmpy
|
6 |
+
ffmpeg-python
|
video.py
CHANGED
@@ -1,15 +1,14 @@
|
|
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 |
-
|
12 |
-
stream_dir = os.path.join(output_dir, unique_id)
|
13 |
|
14 |
if not os.path.exists(stream_dir):
|
15 |
os.makedirs(stream_dir)
|
@@ -17,23 +16,28 @@ def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
|
|
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 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
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}\r\n"'
|
29 |
-
)
|
30 |
|
31 |
try:
|
32 |
# Log the command being executed
|
33 |
-
logging.debug(f"
|
34 |
-
|
35 |
-
# Run the FFmpeg command
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
# Check if the output file was created
|
39 |
if os.path.exists(output_path):
|
@@ -41,18 +45,9 @@ def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
|
|
41 |
else:
|
42 |
logging.error(f"HLS playlist not created at {output_path}")
|
43 |
|
44 |
-
return
|
45 |
-
|
46 |
-
except
|
47 |
-
|
48 |
-
|
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:
|
58 |
-
logging.error("Failed to create HLS playlist.")
|
|
|
1 |
import os
|
2 |
import logging
|
3 |
+
import ffmpeg
|
4 |
import uuid
|
|
|
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", stream_id=None):
|
10 |
# Generate a unique directory for the stream
|
11 |
+
stream_dir = os.path.join(output_dir, stream_id) if stream_id else os.path.join(output_dir, str(uuid.uuid4()))
|
|
|
12 |
|
13 |
if not os.path.exists(stream_dir):
|
14 |
os.makedirs(stream_dir)
|
|
|
16 |
# Set the output paths
|
17 |
output_path = os.path.join(stream_dir, 'output.m3u8')
|
18 |
segment_filename = os.path.join(stream_dir, 'segment_%03d.ts')
|
19 |
+
|
20 |
+
# Log the URL and output paths
|
21 |
+
logging.debug(f"URL: {file_url}")
|
22 |
+
logging.debug(f"Output Path: {output_path}")
|
23 |
+
logging.debug(f"Segment Filename: {segment_filename}")
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
try:
|
26 |
# Log the command being executed
|
27 |
+
logging.debug(f"Starting FFmpeg process with URL: {file_url}")
|
28 |
+
|
29 |
+
# Run the FFmpeg command using ffmpeg-python
|
30 |
+
process = (
|
31 |
+
ffmpeg
|
32 |
+
.input(file_url, headers=f"Authorization: Bearer {token}")
|
33 |
+
.output(output_path,
|
34 |
+
vcodec='libx264', crf=23, preset='medium',
|
35 |
+
acodec='aac', audio_bitrate='192k', format='hls',
|
36 |
+
hls_time=10, hls_list_size=0,
|
37 |
+
hls_segment_filename=segment_filename)
|
38 |
+
)
|
39 |
+
|
40 |
+
process.run()
|
41 |
|
42 |
# Check if the output file was created
|
43 |
if os.path.exists(output_path):
|
|
|
45 |
else:
|
46 |
logging.error(f"HLS playlist not created at {output_path}")
|
47 |
|
48 |
+
return output_path
|
49 |
+
|
50 |
+
except ffmpeg.Error as e:
|
51 |
+
error_message = e.stderr.decode('utf8') if e.stderr else str(e)
|
52 |
+
logging.error(f"Error using FFmpeg to stream file: {error_message}")
|
53 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|