0

I'm a beginner programmer! I'm triyng to make a program that takes three audio file from a specific path, then divides this in random slices and save this slices in a new specific path. than the program should take the slices and marge them in random order creating a new audio file with 3 minuts length.

from pydub import AudioSegment
from pydub.utils import make_chunks
from pydub import AudioSegment 
from pydub.utils import make_chunks 

import random

myaudio = AudioSegment.from_file('Theoretical Girls - You Got Me.mp3') 

N_SPLIT = 10 #number of differents lenghts of the slices
chunk_sizes = []
for _ in range(N_SPLIT):
    chunk_sizes.append(random.randint(10000, 15000)) #random between this values
    
for chunk_length_ms in chunk_sizes:
    chunks = make_chunks(myaudio,chunk_length_ms)
    for i, chunk in enumerate(chunks): 
        chunk_name = '{0}.mp3'.format(i) 
        print ('exporting', chunk_name) 
        chunk.export(chunk_name, format='mp3')

this is my code at the moment. I can slice one audio track in random length chunks but I can't the other things. if you could help me it would be nice!

Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • I suggest using placeholder comments to build out all the parts of the code, even parts that you haven't written yet - that way, you have a clear idea of the design before you implement it, and don't get stuck partway through coding when you realize you haven't fully planned out the design. That way, you can replace the comments one by one, and whichever ones remain let you know what you have left to do, and where the code needs to go. In this case, I think you didn't have a design in mind beforehand, and so it seems like you got stuck once you realized you had no plan. – Random Davis Apr 19 '23 at 15:23

1 Answers1

0

Try this:
To merge the audio slices in random order, you can use the random.sample() function to create a random sample of the chunk names and then concatenate them using the AudioSegment's + operator. Here's an example:

import os
from pydub import AudioSegment
from pydub.utils import make_chunks
import random

input_path = '/path/to/input/files'
output_path = '/path/to/output/files'

audio_files = []
for i in range(1, 4):
    audio_files.append(AudioSegment.from_file(os.path.join(input_path, f'file{i}.mp3')))

chunk_sizes = []
for _ in range(N_SPLIT):
    chunk_sizes.append(random.randint(10000, 15000))

for i, audio_file in enumerate(audio_files):
    for j, chunk_length_ms in enumerate(chunk_sizes):
        chunks = make_chunks(audio_file, chunk_length_ms)
        for k, chunk in enumerate(chunks):
            chunk.export(os.path.join(output_path, f'file{i}_chunk{j}_{k}.mp3'), format='mp3')

merged_chunks = AudioSegment.empty()
chunk_names = []
for i in range(3):
    chunk_names.extend(random.sample(os.listdir(output_path), N_SPLIT))

for chunk_name in chunk_names:
    chunk = AudioSegment.from_file(os.path.join(output_path, chunk_name))
    merged_chunks += chunk

merged_chunks.export(os.path.join(output_path, 'merged_file.mp3'), format='mp3')

This code assumes that your three audio files are named 'file1.mp3', 'file2.mp3', and 'file3.mp3' and are located in the input_path directory. It also assumes that the sliced audio chunks are saved in the output_path directory. Finally, it exports the merged audio file as 'merged_file.mp3' in the output_path directory.

BGOPC
  • 201
  • 9