最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】用Python代码从视频中提取出音频mp3文件

Python crifan 1407浏览 0评论
现有很多,上万个视频文件,需要提取其中的音频文件,保存为常用的mp3格式。
且需要每个文件保存的位置都是和原视频同目录,且还要按照一定规则命名,所以也没法用工具软件批量处理,只能考虑用Python代码批量处理。
python 视频 音频
Python将视频转换为全字符视频(含音频) – CSDN博客
#调用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)
[ python音频处理 ] python使用pydub+ffmpeg进行音频视频处理(简单易用) – pytorch中文网
发现内部也是调用ffmpeg的方法-》那我不如直接自己调用呢
jiaaro/pydub: Manipulate audio with a simple and easy high level interface
介绍几个python的音频处理库 – lyrichu – 博客园
如何快速的提取视频文件中的音频-Mac篇 – 简书
也还是ffmpeg,教程简单易懂
音频 python 从视频文件中提取 wav_audio_帮酷编程问答
Python视频编辑库:MoviePy
https://m.pythontab.com/article/1270
也可以操作视频,获取片段,提取其中音频。
然后就可以去试试了。
期间:
【已解决】Python中获取某文件夹的子目录的列表
然后再去:
【已解决】Python解析.srt字幕文件
然后再去处理音频,此处发现好像不需要从mp4中提取整个mp3音频,只需要提取单个音频片段。
每个片段的起始时间从srt字幕中获取。
在写代码之前,先去终端中用命令行试试ffmpeg提取音频的效果:
【已解决】Mac中用ffmpeg从mp4视频中提取mp3音频
其中关于字符串格式化,参考:
Python String Formatting Best Practices – Real Python
去试试:
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文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.186 seconds, using 22.05MB memory