I am trying to split the audio of multiple audio files can you tell me where I am going wrong thanks :)
from pydub import AudioSegment
import os
folder_number = 1
folder_number_str = str(folder_number)
folder_type = 'short/'
audio_file_dir = os.listdir('for_analysis/' + folder_type + folder_number_str)[0]
audio_dir = 'for_analysis/' + folder_type + folder_number_str + '/'
print(audio_dir)
print(audio_file_dir)
length = len(audio_dir)
file_format = audio_dir[length - 3:]
full_path = audio_dir + audio_file_dir
print(full_path)
sound = AudioSegment.from_file(full_path)
print(sound)
first_cut_point = (1*00 + 0) * 1000
last_cut_point = (1*00 + 30) * 1000
sound_clip = sound[first_cut_point:last_cut_point]
sound_clip.export(os.path.join(audio_dir, 'CUT-' + audio_file_dir), format=file_format)
For Reference I am trying to get the program to open folders and find the audio file and take the first 30 seconds of that file and export it
It fails at the stage of AudioSegment.from_file(full_path) but I am not sure as to why I have tried AudioSegment.from_file(os.path.join(audio_dir + audio_file_dir)) but still no luck any suggestions?
I am expecting a 30 second audio file in the same directory as the original
EDIT: To get this to work I reinstalled FFMPEG using suggestions from https://github.com/jiaaro/pydub/issues/450 and @NickODell below is the adapted code:
from pydub import AudioSegment
import os
folder_number = 1
while folder_number <50:
folder_number_str = str(folder_number)
folder_type = 'short/'
audio_file_dir = os.listdir('for_analysis/' + folder_type + folder_number_str)[0]
audio_dir = 'for_analysis/' + folder_type + folder_number_str + '/'
print(audio_dir)
print(audio_file_dir)
length = len(audio_dir)
file_format = audio_dir[length - 3:]
full_path = audio_dir + audio_file_dir
print(full_path)
sound = AudioSegment.from_file(full_path)
print(sound)
first_cut_point = (1*00 + 0) * 1000
last_cut_point = (1*00 + 30) * 1000
sound_clip = sound[first_cut_point:last_cut_point]
sound_clip.export(os.path.join(audio_dir, 'CUT-' + audio_file_dir))
folder_number = folder_number + 1
To get this to work I reinstalled FFMPEG using suggestions from https://github.com/jiaaro/pydub/issues/450 and @NickODell below is the adapted code
from pydub import AudioSegment
import os
folder_number = 1
while folder_number <50:
folder_number_str = str(folder_number)
folder_type = 'short/'
audio_file_dir = os.listdir('for_analysis/' + folder_type + folder_number_str)[0]
audio_dir = 'for_analysis/' + folder_type + folder_number_str + '/'
print(audio_dir)
print(audio_file_dir)
length = len(audio_dir)
file_format = audio_dir[length - 3:]
full_path = audio_dir + audio_file_dir
print(full_path)
sound = AudioSegment.from_file(full_path)
print(sound)
first_cut_point = (1*00 + 0) * 1000
last_cut_point = (1*00 + 30) * 1000
sound_clip = sound[first_cut_point:last_cut_point]
sound_clip.export(os.path.join(audio_dir, 'CUT-' + audio_file_dir))
folder_number = folder_number + 1