ChandimaPrabath commited on
Commit
24381da
·
1 Parent(s): 0201f08
Files changed (1) hide show
  1. video.py +45 -27
video.py CHANGED
@@ -1,7 +1,8 @@
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,45 +14,62 @@ def ffmpeg_stream(file_url, token, output_dir="tmp/cache/stream"):
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)
 
 
 
 
 
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
  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
+ # Construct the FFmpeg command
21
+ ffmpeg_command = [
22
+ 'ffmpeg',
23
+ '-headers', f'Authorization: Bearer {token}',
24
+ '-i', file_url,
25
+ '-c:v', 'libx264',
26
+ '-crf', '23',
27
+ '-preset', 'medium',
28
+ '-c:a', 'aac',
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: {' '.join(ffmpeg_command)}")
 
 
 
 
40
 
41
+ # Run the FFmpeg command
42
+ process = subprocess.Popen(
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):
57
  logging.info(f"HLS playlist created at {output_path}")
58
  else:
59
  logging.error(f"HLS playlist not created at {output_path}")
60
 
61
+ # Return the unique stream ID and process
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, process = ffmpeg_stream(url, token)
72
+ # if stream_id:
73
+ # logging.info(f"HLS playlist created with stream ID: {stream_id}")
74
+ # else:
75
+ # logging.error("Failed to create HLS playlist.")