AudioCutter.ipynb 4.21 KB
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"AudioCutter.ipynb","provenance":[],"toc_visible":true},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"code","metadata":{"id":"sHHBf1bPIikR","colab_type":"code","outputId":"78f3777d-b504-419e-92d2-c7f8ec8ba3d7","executionInfo":{"status":"ok","timestamp":1591327904716,"user_tz":-540,"elapsed":55988,"user":{"displayName":"‍노경환[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"12302221434260696963"}},"colab":{"base_uri":"https://localhost:8080/","height":121}},"source":["from google.colab import drive\n","drive.mount('/content/gdrive')"],"execution_count":1,"outputs":[{"output_type":"stream","text":["Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n","\n","Enter your authorization code:\n","··········\n","Mounted at /content/gdrive\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"Y57bL4vQX_q0","colab_type":"text"},"source":["## pydub 설치"]},{"cell_type":"code","metadata":{"id":"ijgvGXw2I79q","colab_type":"code","outputId":"e9310cd3-f1d9-4f74-ad2b-8df3ef2fada8","executionInfo":{"status":"ok","timestamp":1591327917988,"user_tz":-540,"elapsed":5733,"user":{"displayName":"‍노경환[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"12302221434260696963"}},"colab":{"base_uri":"https://localhost:8080/","height":84}},"source":["pip install pydub"],"execution_count":2,"outputs":[{"output_type":"stream","text":["Collecting pydub\n","  Downloading https://files.pythonhosted.org/packages/7b/d1/fbfa79371a8cd9bb15c2e3c480d7e6e340ed5cc55005174e16f48418333a/pydub-0.24.1-py2.py3-none-any.whl\n","Installing collected packages: pydub\n","Successfully installed pydub-0.24.1\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"3q0weJwUMQNe","colab_type":"code","outputId":"a60a9368-0ed2-4012-cc2e-4c60b817746f","executionInfo":{"status":"ok","timestamp":1591328204085,"user_tz":-540,"elapsed":701,"user":{"displayName":"‍노경환[학생](소프트웨어융합대학 컴퓨터공학과)","photoUrl":"","userId":"12302221434260696963"}},"colab":{"base_uri":"https://localhost:8080/","height":34}},"source":["cd /content/gdrive/Shared drives/반려견/데이터/sound/barking"],"execution_count":5,"outputs":[{"output_type":"stream","text":["/content/gdrive/Shared drives/반려견/데이터/sound/barking\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"yj1y1himXUGY","colab_type":"text"},"source":["## 오디오 편집 코드\n","> 오디오 편집 프로그램 Audacity를 이용하는 것보다는 편리하다\n","> 오디오 파일을 하나 밖에 부르지 못해서 파일 명과 label_num을 바꿔주어야 한다."]},{"cell_type":"code","metadata":{"id":"SG5iHu95JXNH","colab_type":"code","colab":{}},"source":["from pydub import AudioSegment\n","\n","def cutAudio(input_file, maxtime):\n","  t1 = 0\n","  t2 = t1+10   # 10초 단위로 끊음\n","  label_num = 5867\n","\n","  while t1 < maxtime:   # t1이 전체 오디오 시간보다 작으면 계속 반복\n","    # pydub은 miliseconds 단위 사용 (1000milis -> 1초)\n","    start = t1 * 1000   # 편집할 오디오 시작 시간\n","    end = t2 * 1000   # 편집할 오디오 끝나는 시간\n","    newAudio = AudioSegment.from_wav(input_file)\n","    newAudio = newAudio[start:end]\n","    newAudio = newAudio.set_channels(1) # 오디오 채널 1개로 만듦\n","    newAudio.export('/content/gdrive/Shared drives/반려견/데이터/sound_data/barking/'+'barking_'+str(label_num)+'.wav', format=\"wav\") # 새로운 형태로 export\n","    label_num += 1  # 라벨 번호 1증가\n","    t1 += 10\n","    t2 += 10\n","\n","# 메인 프로그램\n","cutAudio(\"barking_16.wav\", 7190)\n"],"execution_count":0,"outputs":[]}]}