溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用python怎么將m4s緩存文件轉(zhuǎn)換為MP4格式

發(fā)布時間:2020-12-01 15:16:39 來源:億速云 閱讀:570 作者:Leah 欄目:開發(fā)技術(shù)

利用python怎么將m4s緩存文件轉(zhuǎn)換為MP4格式?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

源碼

import os
import json
import random
import time
import requests

# 清除所有空格
def clearSpace(str):
 return str.replace(" ", "").replace(" ", "");


# 獲取指定Uid的Up主名
def getUpNameByUid(uid):
 try:
 url = 'https://space.bilibili.com/' + str(uid)
 html = requests.get(url)
 html.encoding = 'UTF-8'
 html = html.text
 index1 = html.find("<title>") + len("<title>")
 index2 = html.find("的個人空間", index1)
 result = html[index1:index2]
 if (result != ""):
  return result
 else:
  return uid
 except Exception:
 return uid

# 獲取時間戳
def getTimeStamp():
 t = time.localtime(time.time())
 return str(t.tm_year) + '_' + str(t.tm_mon) + '_' + str(t.tm_mday) + '_' + str(t.tm_hour) + \
  str(t.tm_min) + str(t.tm_sec) + str(random.randint(10, 99))


# 更正文件名
def correctFileName(name):
 n_list = list(name)
 for i in range(0, len(n_list)):
 index = 0
 for i in n_list:
  if (
   i == '\\' or i == '/' or i == ':' or i == '*' or i == '?' or i == '\"' or i == '<' or i == '>' or i == '|'):
  n_list.pop(index)
  index = index + 1
 return ''.join(n_list)

# 讀取json文件
def getVideoName(path):
 f = open(path, encoding='utf-8')
 setting = json.load(f)
 try:
 result = setting['page_data']['download_subtitle'] # 注意多重結(jié)構(gòu)的讀取語法
 except KeyError:
 try:
  result = setting['title'] + ' 第' + setting['ep']['index'] + '話 ' + setting['ep']['index_title']
 except KeyError:
  try:
  result = setting['title']
  except KeyError:
  result = getTimeStamp()
 return result


def getVideoOwner(path):
 try:
 f = open(path, encoding='utf-8')
 setting = json.load(f)
 return clearSpace(getUpNameByUid(setting['owner_id']))
 except Exception:
 return ""

# 獲取文件列表
def getFileList(file_dir):
 # 定義四個列表
 title = []
 owner = []
 videoPath = []
 audioPath = []
 # 遍歷文件目錄
 for root, dirs, files in os.walk(file_dir):
 if ('entry.json' in files):
  title.append(getVideoName(str(root) + '\\entry.json'))
  owner.append(getVideoOwner(str(root) + '\\entry.json'))
 if ('video.m4s' in files and 'audio.m4s' in files):
  videoPath.append(str(root) + '\\video.m4s')
  audioPath.append(str(root) + '\\audio.m4s')
 if (len(title) < len(videoPath)):
  title.append(getTimeStamp())
 if ('0.blv' in files):
  title.pop()
 return [title, owner, videoPath, audioPath]


# 輸出mp4文件
def getMP4(title, owner, video_path, audio_path):
 # 生成輸出目錄
 if not os.path.exists("./output"):
 os.mkdir("./output")
 # 循環(huán)生成MP4文件
 for i in title:
 reName = correctFileName(i)
 # 開始生成MP4文件
 if not os.path.exists("./output/" + reName + ".mp4"):
  # 獲取臨時文件時間戳
  t_stamp = getTimeStamp()
  # 開始合成
  os.system(
  "ffmpeg -i " + video_path[title.index(i)] + " -i " + audio_path[
   title.index(i)] + " -codec copy ./output/" + t_stamp + ".mp4")
  # 設(shè)置所屬Up主
  curOwner = owner[title.index(i)]
  if curOwner != "":
  if not os.path.exists("./output/" + curOwner):
   os.mkdir("./output/" + curOwner)
  os.rename("./output/" + t_stamp + ".mp4", "./output/" + curOwner + "/" + reName + ".mp4")
  else:
  # 將臨時文件時間戳改為標(biāo)題名
  os.rename("./output/" + t_stamp + ".mp4", "./output/" + reName + ".mp4")
  print("正在合成...")
  print("標(biāo)題:" + reName)
  print("UP主:" + curOwner)
  print("視頻源:" + video_path[title.index(i)])
  print("音頻源:" + audio_path[title.index(i)])
  time.sleep(1)

print("歡迎使用批量合成M4S工具 ver2.5")
fileDir = str(input("請輸入含M4S文件的目錄:"))
f = getFileList(fileDir)
getMP4(f[0], f[1], f[2], f[3])
print("合成完畢")

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI