{
  "id": 309046,
  "title": "How to split audio file into 5 seconds clips ?",
  "url": "/competitions/birdclef-2022/discussion/309046",
  "author_name": "",
  "post_date": "2022-02-21T15:04:09.037193600Z",
  "votes": 11,
  "comment_count": 3,
  "views": 0,
  "content": "<p>Sorry if this is a noob question. I could not find any code snippet in which we can split audio clips into 5 seconds clips. Anybody know how to do it ? thanks for the help</p>",
  "messages": [
    {
      "id": "1699964",
      "postDate": "02/21/2022 15:04:09",
      "content": "<p>Sorry if this is a noob question. I could not find any code snippet in which we can split audio clips into 5 seconds clips. Anybody know how to do it ? thanks for the help</p>",
      "rawMarkdown": "Sorry if this is a noob question. I could not find any code snippet in which we can split audio clips into 5 seconds clips. Anybody know how to do it ? thanks for the help",
      "votes": null
    },
    {
      "id": "1700255",
      "postDate": "02/21/2022 19:03:52",
      "content": "<p>you can use   <code>pydub</code>   <code>make_chunks</code> function to easily create chunks of any length here is a simple code snippet </p>\n<pre><code>from pydub import AudioSegment\nfrom pydub.utils import make_chunks\n\naudio = AudioSegment.from_ogg(row['path'])\nchunk_length_ms = 5000  # get 5 second chunks\nchunks = make_chunks(audio, chunk_length_ms)\nfor i, chunk in enumerate(chunks):\n        chunk_name = f\"{filename}_{i}.ogg\"\n        chunk.export(chunk_name, format=\"ogg\")\n</code></pre>\n<p>hope this helps</p>",
      "rawMarkdown": "you can use   `pydub`   `make_chunks` function to easily create chunks of any length here is a simple code snippet \n```\nfrom pydub import AudioSegment\nfrom pydub.utils import make_chunks\n\naudio = AudioSegment.from_ogg(row['path'])\nchunk_length_ms = 5000  # get 5 second chunks\nchunks = make_chunks(audio, chunk_length_ms)\nfor i, chunk in enumerate(chunks):\n        chunk_name = f\"{filename}_{i}.ogg\"\n        chunk.export(chunk_name, format=\"ogg\")\n```\nhope this helps",
      "votes": null
    },
    {
      "id": "1721635",
      "postDate": "03/13/2022 21:49:01",
      "content": "<p>Try this function:</p>\n<pre><code># Cut the signal into frames duration 5 sec:\ndef framing(sig: np.ndarray, sample_rate: int, frame_len: int, duration_time: float) -&gt; np.ndarray:\n    num_frames = int(np.ceil(duration_time / 5))\n    framed_sig = np.zeros((num_frames, int(frame_len * sample_rate)))\n    start_time = 0\n    end_time = frame_len * sample_rate\n    if duration_time &lt; 5:\n        framed_sig[0][:sig.shape[0]] = sig\n    else:\n        for i in range(num_frames):\n            framed_sig[i][:end_time - start_time] = sig[start_time:end_time]\n            start_time = start_time + int(frame_len * sample_rate)\n            if i == num_frames - 2:\n                end_time = end_time + int(sig.shape[0] - start_time)\n            else:\n                end_time = end_time + int(frame_len * sample_rate)\n\n    return framed_sig\n</code></pre>\n<p><code>sig</code> - input audio signal<br>\n<code>frame_len</code> - duration of the window (5 sec)<br>\n<code>duration_time</code> - duration of the signal. You can get the signal duration using librosa: <code>librosa.get_duration(y=sig, sr=32000)</code><br>\nFinally, you get array <code>framed_sig</code> with 2 dim: <strong><em>frames number</em></strong> x <strong><em>frame with length 5 sec</em></strong></p>",
      "rawMarkdown": "Try this function:\n\n```\n# Cut the signal into frames duration 5 sec:\ndef framing(sig: np.ndarray, sample_rate: int, frame_len: int, duration_time: float) -> np.ndarray:\n    num_frames = int(np.ceil(duration_time / 5))\n    framed_sig = np.zeros((num_frames, int(frame_len * sample_rate)))\n    start_time = 0\n    end_time = frame_len * sample_rate\n    if duration_time < 5:\n        framed_sig[0][:sig.shape[0]] = sig\n    else:\n        for i in range(num_frames):\n            framed_sig[i][:end_time - start_time] = sig[start_time:end_time]\n            start_time = start_time + int(frame_len * sample_rate)\n            if i == num_frames - 2:\n                end_time = end_time + int(sig.shape[0] - start_time)\n            else:\n                end_time = end_time + int(frame_len * sample_rate)\n\n    return framed_sig\n```\n\n`sig` - input audio signal\n`frame_len` - duration of the window (5 sec)\n`duration_time` - duration of the signal. You can get the signal duration using librosa: `librosa.get_duration(y=sig, sr=32000)`\nFinally, you get array `framed_sig` with 2 dim: ***frames number*** x ***frame with length 5 sec***",
      "votes": null
    },
    {
      "id": "1746166",
      "postDate": "04/05/2022 14:35:12",
      "content": "<p>You can use <a href=\"https://pysoundfile.readthedocs.io/en/latest/\" target=\"_blank\">sound file</a></p>\n<pre><code>period = 5\nsr = 32000\nsegments = []\nfor block in sf.blocks(file_path, period*sr):\n    segments.append(block)\n\ndef get_audio_chunks(file_path: str, sec: int = 5) -&gt; List[np.array]:\n    return [block for block in sf.blocks(file_path, period*sr)]\n</code></pre>",
      "rawMarkdown": "You can use [sound file](https://pysoundfile.readthedocs.io/en/latest/)\n\n```\nperiod = 5\nsr = 32000\nsegments = []\nfor block in sf.blocks(file_path, period*sr):\n    segments.append(block)\n\ndef get_audio_chunks(file_path: str, sec: int = 5) -> List[np.array]:\n    return [block for block in sf.blocks(file_path, period*sr)]\n\n```",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1700255,
      "author_name": "mrigendraagrawal",
      "author_url": "",
      "post_date": "02/21/2022 19:03:52",
      "content": "<p>you can use   <code>pydub</code>   <code>make_chunks</code> function to easily create chunks of any length here is a simple code snippet </p>\n<pre><code>from pydub import AudioSegment\nfrom pydub.utils import make_chunks\n\naudio = AudioSegment.from_ogg(row['path'])\nchunk_length_ms = 5000  # get 5 second chunks\nchunks = make_chunks(audio, chunk_length_ms)\nfor i, chunk in enumerate(chunks):\n        chunk_name = f\"{filename}_{i}.ogg\"\n        chunk.export(chunk_name, format=\"ogg\")\n</code></pre>\n<p>hope this helps</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1721635,
      "author_name": "alexklyu",
      "author_url": "",
      "post_date": "03/13/2022 21:49:01",
      "content": "<p>Try this function:</p>\n<pre><code># Cut the signal into frames duration 5 sec:\ndef framing(sig: np.ndarray, sample_rate: int, frame_len: int, duration_time: float) -&gt; np.ndarray:\n    num_frames = int(np.ceil(duration_time / 5))\n    framed_sig = np.zeros((num_frames, int(frame_len * sample_rate)))\n    start_time = 0\n    end_time = frame_len * sample_rate\n    if duration_time &lt; 5:\n        framed_sig[0][:sig.shape[0]] = sig\n    else:\n        for i in range(num_frames):\n            framed_sig[i][:end_time - start_time] = sig[start_time:end_time]\n            start_time = start_time + int(frame_len * sample_rate)\n            if i == num_frames - 2:\n                end_time = end_time + int(sig.shape[0] - start_time)\n            else:\n                end_time = end_time + int(frame_len * sample_rate)\n\n    return framed_sig\n</code></pre>\n<p><code>sig</code> - input audio signal<br>\n<code>frame_len</code> - duration of the window (5 sec)<br>\n<code>duration_time</code> - duration of the signal. You can get the signal duration using librosa: <code>librosa.get_duration(y=sig, sr=32000)</code><br>\nFinally, you get array <code>framed_sig</code> with 2 dim: <strong><em>frames number</em></strong> x <strong><em>frame with length 5 sec</em></strong></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 1746166,
      "author_name": "mlneo07",
      "author_url": "",
      "post_date": "04/05/2022 14:35:12",
      "content": "<p>You can use <a href=\"https://pysoundfile.readthedocs.io/en/latest/\" target=\"_blank\">sound file</a></p>\n<pre><code>period = 5\nsr = 32000\nsegments = []\nfor block in sf.blocks(file_path, period*sr):\n    segments.append(block)\n\ndef get_audio_chunks(file_path: str, sec: int = 5) -&gt; List[np.array]:\n    return [block for block in sf.blocks(file_path, period*sr)]\n</code></pre>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1699964": "Sorry if this is a noob question. I could not find any code snippet in which we can split audio clips into 5 seconds clips. Anybody know how to do it ? thanks for the help",
    "1700255": "you can use   `pydub`   `make_chunks` function to easily create chunks of any length here is a simple code snippet \n```\nfrom pydub import AudioSegment\nfrom pydub.utils import make_chunks\n\naudio = AudioSegment.from_ogg(row['path'])\nchunk_length_ms = 5000  # get 5 second chunks\nchunks = make_chunks(audio, chunk_length_ms)\nfor i, chunk in enumerate(chunks):\n        chunk_name = f\"{filename}_{i}.ogg\"\n        chunk.export(chunk_name, format=\"ogg\")\n```\nhope this helps",
    "1721635": "Try this function:\n\n```\n# Cut the signal into frames duration 5 sec:\ndef framing(sig: np.ndarray, sample_rate: int, frame_len: int, duration_time: float) -> np.ndarray:\n    num_frames = int(np.ceil(duration_time / 5))\n    framed_sig = np.zeros((num_frames, int(frame_len * sample_rate)))\n    start_time = 0\n    end_time = frame_len * sample_rate\n    if duration_time < 5:\n        framed_sig[0][:sig.shape[0]] = sig\n    else:\n        for i in range(num_frames):\n            framed_sig[i][:end_time - start_time] = sig[start_time:end_time]\n            start_time = start_time + int(frame_len * sample_rate)\n            if i == num_frames - 2:\n                end_time = end_time + int(sig.shape[0] - start_time)\n            else:\n                end_time = end_time + int(frame_len * sample_rate)\n\n    return framed_sig\n```\n\n`sig` - input audio signal\n`frame_len` - duration of the window (5 sec)\n`duration_time` - duration of the signal. You can get the signal duration using librosa: `librosa.get_duration(y=sig, sr=32000)`\nFinally, you get array `framed_sig` with 2 dim: ***frames number*** x ***frame with length 5 sec***",
    "1746166": "You can use [sound file](https://pysoundfile.readthedocs.io/en/latest/)\n\n```\nperiod = 5\nsr = 32000\nsegments = []\nfor block in sf.blocks(file_path, period*sr):\n    segments.append(block)\n\ndef get_audio_chunks(file_path: str, sec: int = 5) -> List[np.array]:\n    return [block for block in sf.blocks(file_path, period*sr)]\n\n```"
  },
  "source": "meta"
}