Maximofn commited on
Commit
8dd5680
1 Parent(s): 6f76ded

Add seconds of chuncks as input argument into slice_audio.py

Browse files
Files changed (1) hide show
  1. slice_audio.py +10 -8
slice_audio.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import argparse
3
 
4
  START = 00
5
- SECONDS = 150
6
  FOLDER = "chunks"
7
 
8
  def seconds_to_hms(seconds):
@@ -27,29 +27,30 @@ def main(args):
27
  # name, extension = input.split(".")
28
  path, filename = os.path.split(input)
29
  name, extension = os.path.splitext(filename)
 
30
 
31
  # Get audio duration in seconds
32
  duration = float(os.popen(f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {input}').read())
33
  hour, minute, second = seconds_to_hms(int(duration))
34
 
35
  # Number of chunks
36
- num_chunks = -(-int(duration) // SECONDS) # Redondeo hacia arriba
37
 
38
- # Slice audio into SECONDS chunks
39
- hour, minute, second = seconds_to_hms(SECONDS) # Duration of each chunk
40
  output_files = []
41
  for chunk in range(num_chunks):
42
- start_time = chunk * SECONDS
43
  hour_start, minute_start, second_start = seconds_to_hms(start_time) # Start time of each chunk
44
 
45
- if start_time + SECONDS > duration:
46
  hour, minute, second = seconds_to_hms(duration - start_time)
47
  else:
48
- hour, minute, second = seconds_to_hms(SECONDS)
49
 
50
  output = f"{FOLDER}/{name}_chunk{chunk:003d}{extension}"
51
 
52
- if start_time + SECONDS > duration:
53
  command = f'ffmpeg -i {input} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -loglevel error {output}'
54
  else:
55
  command = f'ffmpeg -i {input} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -t {hour:02}:{minute:02}:{second:02} -loglevel error {output}'
@@ -65,5 +66,6 @@ def main(args):
65
  if __name__ == "__main__":
66
  argparser = argparse.ArgumentParser(description='Slice audio into smaller chunks')
67
  argparser.add_argument('input', help='Input audio file')
 
68
  args = argparser.parse_args()
69
  main(args)
 
2
  import argparse
3
 
4
  START = 00
5
+ # SECONDS = 150
6
  FOLDER = "chunks"
7
 
8
  def seconds_to_hms(seconds):
 
27
  # name, extension = input.split(".")
28
  path, filename = os.path.split(input)
29
  name, extension = os.path.splitext(filename)
30
+ seconds = int(args.seconds)
31
 
32
  # Get audio duration in seconds
33
  duration = float(os.popen(f'ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 {input}').read())
34
  hour, minute, second = seconds_to_hms(int(duration))
35
 
36
  # Number of chunks
37
+ num_chunks = -(-int(duration) // seconds) # Redondeo hacia arriba
38
 
39
+ # Slice audio into seconds chunks
40
+ hour, minute, second = seconds_to_hms(seconds) # Duration of each chunk
41
  output_files = []
42
  for chunk in range(num_chunks):
43
+ start_time = chunk * seconds
44
  hour_start, minute_start, second_start = seconds_to_hms(start_time) # Start time of each chunk
45
 
46
+ if start_time + seconds > duration:
47
  hour, minute, second = seconds_to_hms(duration - start_time)
48
  else:
49
+ hour, minute, second = seconds_to_hms(seconds)
50
 
51
  output = f"{FOLDER}/{name}_chunk{chunk:003d}{extension}"
52
 
53
+ if start_time + seconds > duration:
54
  command = f'ffmpeg -i {input} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -loglevel error {output}'
55
  else:
56
  command = f'ffmpeg -i {input} -ss {hour_start:02d}:{minute_start:02d}:{second_start:02d} -t {hour:02}:{minute:02}:{second:02} -loglevel error {output}'
 
66
  if __name__ == "__main__":
67
  argparser = argparse.ArgumentParser(description='Slice audio into smaller chunks')
68
  argparser.add_argument('input', help='Input audio file')
69
+ argparser.add_argument('seconds', help='Duration of each chunk in seconds')
70
  args = argparser.parse_args()
71
  main(args)