jhj0517 commited on
Commit
7ab19f6
·
1 Parent(s): e56e825

Add extract_sound

Browse files
Files changed (1) hide show
  1. modules/video_utils.py +51 -4
modules/video_utils.py CHANGED
@@ -13,7 +13,6 @@ logger = get_logger()
13
  def extract_frames(
14
  vid_input: str,
15
  output_temp_dir: str = TEMP_DIR,
16
- quality: int = 2,
17
  start_number: int = 0
18
  ):
19
  """
@@ -26,7 +25,6 @@ def extract_frames(
26
  'ffmpeg',
27
  '-y', # Enable overwriting
28
  '-i', vid_input,
29
- '-q:v', str(quality),
30
  '-start_number', str(start_number),
31
  f'{output_path}'
32
  ]
@@ -35,7 +33,36 @@ def extract_frames(
35
  subprocess.run(command, check=True)
36
  except subprocess.CalledProcessError as e:
37
  logger.exception("Error occurred while extracting frames from the video")
38
- raise f"An error occurred: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
 
41
  def get_frames_from_dir(vid_dir: str,
@@ -63,6 +90,26 @@ def get_frames_from_dir(vid_dir: str,
63
  return frames
64
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def clean_image_files(image_dir: str):
67
  """Removes all image files from the dir"""
68
  image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp')
@@ -74,4 +121,4 @@ def clean_image_files(image_dir: str):
74
  os.remove(file_path)
75
  except Exception as e:
76
  logger.exception("Error while removing image files")
77
- raise f"Error removing {str(e)}"
 
13
  def extract_frames(
14
  vid_input: str,
15
  output_temp_dir: str = TEMP_DIR,
 
16
  start_number: int = 0
17
  ):
18
  """
 
25
  'ffmpeg',
26
  '-y', # Enable overwriting
27
  '-i', vid_input,
 
28
  '-start_number', str(start_number),
29
  f'{output_path}'
30
  ]
 
33
  subprocess.run(command, check=True)
34
  except subprocess.CalledProcessError as e:
35
  logger.exception("Error occurred while extracting frames from the video")
36
+ raise RuntimeError(f"An error occurred: {str(e)}")
37
+
38
+ return get_frames_from_dir(output_temp_dir)
39
+
40
+
41
+ def extract_sound(
42
+ vid_input: str,
43
+ output_temp_dir: str = TEMP_DIR,
44
+ ):
45
+ """
46
+ Extract audio from a video file and save it as a separate sound file. This needs FFmpeg installed.
47
+ """
48
+ os.makedirs(output_temp_dir, exist_ok=True)
49
+ output_path = os.path.join(output_temp_dir, "sound.mp3")
50
+
51
+ command = [
52
+ 'ffmpeg',
53
+ '-y', # Enable overwriting
54
+ '-i', vid_input,
55
+ '-vn',
56
+ output_path
57
+ ]
58
+
59
+ try:
60
+ subprocess.run(command, check=True)
61
+ except subprocess.CalledProcessError as e:
62
+ logger.exception("Error occurred while extracting sound from the video")
63
+ raise RuntimeError(f"An error occurred: {str(e)}")
64
+
65
+ return output_path
66
 
67
 
68
  def get_frames_from_dir(vid_dir: str,
 
90
  return frames
91
 
92
 
93
+ def clean_temp_dir(temp_dir: str = TEMP_DIR):
94
+ """Removes media files from the directory."""
95
+ clean_sound_files(temp_dir)
96
+ clean_image_files(temp_dir)
97
+
98
+
99
+ def clean_sound_files(sound_dir: str):
100
+ """Removes all sound files from the directory."""
101
+ sound_extensions = ('.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma')
102
+
103
+ for filename in os.listdir(sound_dir):
104
+ if filename.lower().endswith(sound_extensions):
105
+ file_path = os.path.join(sound_dir, filename)
106
+ try:
107
+ os.remove(file_path)
108
+ except Exception as e:
109
+ logger.exception("Error while removing sound files")
110
+ raise RuntimeError(f"Error removing {file_path}: {str(e)}")
111
+
112
+
113
  def clean_image_files(image_dir: str):
114
  """Removes all image files from the dir"""
115
  image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp')
 
121
  os.remove(file_path)
122
  except Exception as e:
123
  logger.exception("Error while removing image files")
124
+ raise RuntimeError(f"An error occurred: {str(e)}")