ffmpeg合成音频视频
我想爬取B站视频import requests
import re
import json
import subprocess
url='https://www.'
headers={
'referer':'https://www.',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.61'
}
response = requests.get(url=url,headers=headers)
response.encoding=response.apparent_encoding
title=re.findall('"title":"(.*?)","pubdate"',response.text)[0] #查找视频标题
html_data=re.findall('<script>window.__playinfo__=(.*?)</script>',response.text)[0] #正则查找视频源码
json_data=json.loads(html_data)
video_url=json_data['data']['dash']['video'][0]['baseUrl'] #画面的地址
audio_url=json_data['data']['dash']['audio'][0]['baseUrl'] #音频地址
video_content=requests.get(url=video_url,headers=headers).content #获取视频地址的html
audio_content=requests.get(url=audio_url,headers=headers).content #获取音频地址的html
f1=open(title+'.mp4','wb')
f1.write(video_content)
f2=open(title+'.mp3','wb')
f2.write(audio_content)
cmd=f"ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental out{title}.mp4" #合成视频,音频
subprocess.run(cmd,shell=True)
print('----------Done!!!----------')
f1.close()
f2.close()
结果没有错误,但只有音频和视频画面两个文件,找不到合成的视频
终端输出
'ffmpeg' е
----------Done!!!----------
但是,当我手动打开命令窗口,将ffmpeg -i {title}.mp4 -i {title}.mp3 -c:v copy -c:a aac -strict experimental out{title}.mp4复制粘贴,并改{title}为视频标题时,
却可以正常合成视频(有声音有画面)