title: How to split an audio file (.mp3 or .wav) into 10 secs parts to send its parts to a function?
body: <p>This is not part of the code, so indicate it with the <code>!</code> operator, since it is a command for the Google Colab command line (because I am first testing this code on a notebook, and then the same code can be used on a local PC). But this first line is necessary for the code to work in Google Colab.</p>
  <pre><code>! pip install -q transformers
  </code></pre>
  <p>And here the code:</p>
  <pre class="lang-py prettyprint-override"><code>import librosa
  import torch
  
  #for download Wav2Vec2 from the Transformers library of Hugging Face
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
  </code></pre>
  <p>In this case, I load the tokenizer and this pre-trained model</p>
  <pre class="lang-py prettyprint-override"><code>#load model and tokenizer
  tokenizer = Wav2Vec2Tokenizer.from_pretrained(&quot;facebook/wav2vec2-base-960h&quot;)
  model = Wav2Vec2ForCTC.from_pretrained(&quot;facebook/wav2vec2-base-960h&quot;)
  </code></pre>
  <p>Here is the problem, since I am loading the complete audio file and it probably saturates the RAM memory when trying to process an .mp3 file with a duration of 14:18 mins (6,54 Mb).</p>
  <p>I choose the 16000 Hz frequency because this facebook Wav2Vec2 model is pretrained in a 16 kHz rate.</p>
  <p>I'm not sure if the problem that causes the RAM to saturate is the weight of the file or if it is the length of the audio file? In any case I think the solution is to cut it into parts of 5 seconds, or 10 seconds or 30 seconds each (so it would be convenient to have a function with a parameter to adjust that).</p>
  <pre class="lang-py prettyprint-override"><code>input_audio_file = &quot;CLASE 9 HOMBRE MAQUINA.mp3&quot;
  speech, rate = librosa.load(input_audio_file, sr=16000)
  </code></pre>
  <p>Finally the audio processing process, and this is where my RAM gets clogged, so I needed some help for create some kind of loop, where if the input audio is longer than <strong>X amount of seconds</strong> then the algorithm split this original audio into <strong>n-amount of parts</strong> of <strong>X amount of seconds each of them</strong> (perhaps the last part is shorter as it is less than X seconds).</p>
  <p>Here I tokenize the inputs (in this case it is the complete audio, but I think it would be best if they were passed from audio fragments of X seconds duration each) and I would configure the tensors that contain the information of the audios in PyTorch objects.</p>
  <p>Here the processing process:</p>
  <pre class="lang-py prettyprint-override"><code>input_values = tokenizer(speech, return_tensors = 'pt').input_values
  
  #Store logits (non-normalized predictions)
  logits = model(input_values).logits
  </code></pre>
  <pre class="lang-py prettyprint-override"><code>#Store predicted id's
  predicted_ids = torch.argmax(logits, dim =-1)
  
  #decode the audio to generate text
  transcriptions = tokenizer.decode(predicted_ids[0])
  
  #And finally I would print the text after this text-to-speech process
  print(transcriptions)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/XQY6L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XQY6L.png" alt="enter image description here" /></a></p>
  <p>Here the saturation of the RAM that caused the connection to be closed (I think if I tried this code with this audio file on my PC it would just freeze)</p>
  <p><a href="https://i.stack.imgur.com/I36aG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I36aG.png" alt="enter image description here" /></a></p>
  <p>I think that for this question the Wav2Vec2 algorith operation could be taken as an information of the context in which the program is going to work, however what <strong>I was needing is how to fragment the audio into parts and send it part by part to the processing code (perhaps it is convenient to place it inside a function that is called inside the loop) and print what is converted to text line by line in the console.</strong></p>
  <p>I have been able to determine the length of a audio file in seconds with <code>librosa</code> methods to decide whether to split it or not</p>
  <pre class="lang-py prettyprint-override"><code>speech, rate = librosa.load(input_audio_file)
  
  x = 10
  
  len_data = len(speech)  # holds length of the numpy array
  duration_in_seconds = int( len_data / rate ) # returns duration in seconds but in floats
  print(duration_in_seconds)
  
  if(duration_in_seconds &gt;= x):
      #split the audio file in x secs audio chunks
  
  else:
      #dont split the audio file
  </code></pre>