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