现有很多,上万个视频文件,需要提取其中的音频文件,保存为常用的mp3格式。
且需要每个文件保存的位置都是和原视频同目录,且还要按照一定规则命名,所以也没法用工具软件批量处理,只能考虑用Python代码批量处理。
python 视频 音频
#调用ffmpeg获取mp3音频文件 def video2mp3(file_name): outfile_name = file_name.split('.')[0]+'.mp3' subprocess.call('ffmpeg -i '+file_name+' -f mp3 '+outfile_name,shell = True)
发现内部也是调用ffmpeg的方法-》那我不如直接自己调用呢
也还是ffmpeg,教程简单易懂
Python视频编辑库:MoviePy
也可以操作视频,获取片段,提取其中音频。
然后就可以去试试了。
期间:
【已解决】Python中获取某文件夹的子目录的列表
然后再去:
【已解决】Python解析.srt字幕文件
然后再去处理音频,此处发现好像不需要从mp4中提取整个mp3音频,只需要提取单个音频片段。
每个片段的起始时间从srt字幕中获取。
在写代码之前,先去终端中用命令行试试ffmpeg提取音频的效果:
【已解决】Mac中用ffmpeg从mp4视频中提取mp3音频
其中关于字符串格式化,参考:
去试试:
Python 2.7.10 (default, Oct 6 2017, 22:29:07) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> "a%2db" % (1) 'a 1b' >>> "a%02db" % (1) 'a01b' >>> "a%03db" % (1) 'a001b' >>>
还是用这个简单的
【总结】
最后用代码:
import os import codecs import json from datetime import datetime,timedelta import time from datetime import timedelta import logging import shutil import pysrt import subprocess from crifanLib import crifanLogging ... def processSingleShow(userId, showId): getSubOk = False audioSegmentList = [] userShowPath = generateUserShowFoler(userId, showId) showInfoFilename = "show_%s_info.json" % showId showInfoFullpath = os.path.join(userShowPath, showInfoFilename) if os.path.exists(showInfoFullpath): audioOutputPath = userShowPath showInfoDict = loadJsonFromFile(showInfoFullpath) courseId = showInfoDict["course_id"] getSubOk, subtitleList = getCourceSubtileInfo(courseId) logging.info("getSubOk=%s, subtitleList=%s", getSubOk, subtitleList) if getSubOk: # check video file showVideoFilename = "show_%s_video.mp4" % showId logging.info("showVideoFilename=%s", showVideoFilename) showVideoFullpath = os.path.join(userShowPath, showVideoFilename) logging.info("showVideoFullpath=%s", showVideoFullpath) if os.path.exists(showVideoFullpath): # process each subtitle for curIdx, eachSubtitle in enumerate(subtitleList): curNum = curIdx + 1 subtitleEn = "" subtitleZhcn = "" subtitleText = eachSubtitle.text startTime = eachSubtitle.start endTime = eachSubtitle.end if "\n" in subtitleText: subtitleTextList = subtitleText.split("\n") subtitleEn = subtitleTextList[0] if len(subtitleTextList) > 1: subtitleZhcn = subtitleTextList[1] else: subtitleEn = subtitleText startTimeStr = "%02d:%02d:%02d.%03d" % (startTime.hours, startTime.minutes, startTime.seconds, startTime.milliseconds) endTimeStr = "%02d:%02d:%02d.%03d" % (endTime.hours, endTime.minutes, endTime.seconds, endTime.milliseconds) startTimeStrNoSeperator = "%02d%02d%02d%03d" % (startTime.hours, startTime.minutes, startTime.seconds, startTime.milliseconds) endTimeStrNoSeperator = "%02d%02d%02d%03d" % (endTime.hours, endTime.minutes, endTime.seconds, endTime.milliseconds) curAudioSegmentFilename = "show_%s_audio_%s_%s.mp3" % (showId, startTimeStrNoSeperator, endTimeStrNoSeperator) curAudioSegmentFullpath = os.path.join(userShowPath, curAudioSegmentFilename) # startTimeDelta = timedelta( # hours=startTime.hours, # minutes=startTime.minutes, # seconds=startTime.seconds, # milliseconds=startTime.milliseconds # ) # endTimeDelta = timedelta( # hours=endTime.hours, # minutes=endTime.minutes, # seconds=endTime.seconds, # milliseconds=endTime.milliseconds # ) audioSegmentInfoDict = { # "startTimeDelta": startTimeDelta, # "endTimeDelta": endTimeDelta, "segmentFilename": curAudioSegmentFilename, "startTime": startTimeStr, "endTime": endTimeStr, "subtitle_en": subtitleEn, "subtitle_zhcn": subtitleZhcn } audioSegmentList.append(audioSegmentInfoDict) logging.info("[%d][%s-%s] %s | %s", curNum, startTime, endTime, subtitleEn, subtitleZhcn) # extract audio segment from video # ffmpeg -i show_157712932_video.mp4 -ss 00:00:11.270 -to 00:00:14.550 -b:a 128k show_157712932_audio_000011270_000014550.mp3 if not os.path.exists(curAudioSegmentFullpath): ffmpegCmd = "ffmpeg -i %s -ss %s -to %s -b:a 128k %s" % (showVideoFullpath, startTimeStr, endTimeStr, curAudioSegmentFullpath) subprocess.call(ffmpegCmd, shell=True) logging.info("++++++++++ Complete use ffmpeg extract audio: %s", ffmpegCmd) # save audio info audioInfoDict = { "show_id": showId, "course_id": courseId, "uid": userId, "audios": audioSegmentList } audioInfoFilename = "show_%s_audio.json" % showId audioInfoFullpath = os.path.join(audioOutputPath, audioInfoFilename) saveJsonToFile(audioInfoFullpath, audioInfoDict) saveOkResult(audioInfoDict) else: errMsg = "Can not find show video file: %s" % showVideoFullpath errInfoDict = { "errMsg": errMsg, "userId" : userId, "showId": showId, "crouseId": "", } saveErrorResult(errInfoDict) else: errMsg = "Can not find show info file: %s" % showInfoFullpath errInfoDict = { "errMsg": errMsg, "userId" : userId, "showId": showId, "crouseId": "", } saveErrorResult(errInfoDict) return getSubOk, audioSegmentList
可以从mp4中提取出mp3音频:
转载请注明:在路上 » 【已解决】用Python代码从视频中提取出音频mp3文件