업무자동화

mp3파일 자르기 자동화 구현 사례

웍투오토(work2auto) 2024. 10. 27. 08:55

현존하는 영상 편집기에서 수작업으로 음원을 자르는 불편함을 없애기 위해 구간 정보만 입력하면 

음원이 잘려지는 프로그램이 요구됨

요구사항

  • 동영상 제작시 필요한 음원 특정구간만 자르는 기능 구현

코드 구현

from moviepy.editor import *

class CutMp3file:
    def __init__(self,inputfile,outputfile):
        self.inputfile = inputfile
        self.outputfile = outputfile
          
    def cut(self, start_string,end_string):
        
        start_hour, start_min, start_sec = map(int, start_string.split(':'))
        end_hour, end_min, end_sec = map(int, end_string.split(':'))

        start = ((start_hour*3600)+(start_min*60)+start_sec)*1
        end = ((end_hour*3600)+(end_min*60)+end_sec)*1

        snd = AudioFileClip(path, fps = 44100)

        snd = snd.subclip(start,end)
        
        snd.write_audiofile(self.outputfile)

 

코드 실행

start_string = '00:56:14'
end_string = '01:05:05'
inputfile = '경로/파일명.mp3'
outputfile = '결과파일명.mp3'

mp3 = CutMp3file(inputfile, outputfile)
mp3.cut(start_string,end_string)

 

구현을 위한 핵심내용

기존에 많이 사용되던 Library가 모두 더이상 관리되지 않아 MoviePy를 활용하여 구현하였습니다.

실행결과

 

개발문의 환영합니다.

Start an Open Chat on KakaoTalk!
Tap the link to get started.

웍투오토] 웹크롤링 & 업무자동화 개발문의
https://open.kakao.com/o/sF2plaVg