Intro
파이썬으로 audio 파일을 수정해야 할 일이 생겨 여러 라이브러리를 찾아 보았다. Python Audio Tools, pydub 등 여러 가지 라이브러리 중 가장 github star가 많고, 아직까지 상대적으로 활동이 활발한 pydub를 사용하기로 하였다.
Installing
pip install pydub로 간단히 설치 하거나 공식 홈페이지에서 다운받아 설치 가능하다.
Basic usage
from pydub import AudioSegment
# Open file
song = AudioSegment.from_mp3('song.mp3')
# Slice audio
# pydub는 milliseconds 단위를 사용한다
ten_seconds = 10 * 1000
one_min = ten_seconds * 6
first_10_seconds = song[:ten_seconds]
last_5_seconds = song[-5000:]
# up/down volumn
beginning = first_10_seconds + 6
# Save the result
# can give parameters-quality, channel, etc
beginning.exoprt('result.flac', format='flac', parameters=["-q:a", "10", "-ac", "1"])
Change stereo to mono
from pydub import AudioSegment
sound = AudioSegment.from_wav("/path/to/file.wav")
sound = sound.set_channels(1)
sound.export("/output/path.wav", format="wav")'backend > python' 카테고리의 다른 글
| 파이썬에서 실행 시간 체크하기 (check executed time in python) (0) | 2017.12.28 |
|---|---|
| django deploy with gunicorn, virtualenv (0) | 2017.10.19 |
| PIL (python image library, pillow) 사용해서 dpi, image size 변경 (0) | 2017.10.14 |
| Python으로 docx 파일을 다루자 - Python-docx (0) | 2017.10.13 |
| Django admin을 더 풍성하게, django jet (0) | 2017.10.11 |