python – 如何将音频添加到视频?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 如何将音频添加到视频?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2466字,纯文字阅读大概需要4分钟。
内容图文
![python – 如何将音频添加到视频?](/upload/InfoBanner/zyjiaocheng/783/c85ad519f4eb4688843af62e314d3051.jpg)
我录制了没有声音的视频.我保存了一个快照,然后用25FPS将它们编译成.avi.现在我想录制音频(而不是在以下快照之间的time.sleep(0.04),我将在这段时间录制音频)并编辑它的视频.现在我有avi文件和wave文件,我找到了一个混合它们的解决方案.
Python,Win32
这是我的“录像机”使用’mencoder’:
import os
import re
from VideoCapture import *
import time
def makeVideo(device, ftime):
folder = 'foto/forVideo/'
mencoder = "C:\mencoder.exe"
for i in range(ftime * 25) :
FN = "foto\\forVideo\\ola-%(#)04d.jpg" % {"#" : i}
device.saveSnapshot(FN, quality=100, timestamp=0, boldfont=0)
time.sleep(0.04)
# Set up regular expressions for later
RE = re.compile('.*-(\d*)\.jpg')
fbRE = re.compile('(.*)-.*\.jpg')
# How many frames to use per movie
framestep = 2000
print '\n\n\tlisting contents of %s . . .'%folder
files = os.listdir(folder)
print '%s files found.\n\n' % len(files)
# If a file is called "asdf-003.jpg", the basename will be 'asdf'.
basenames = [fbRE.match(i).groups()[0] for i in files if fbRE.match(i)]
# Get the set of unique basenames. In the
basenames = list(set(basenames))
print '\t***There are %s different runs here.***' % len(basenames)
# This loop will only execute once if there was only a single experiment
# in the folder.
for j,bn in enumerate(basenames):
these_files = [i for i in files if bn in i]
# Sort using the "decorate-sort-undecorate" approach
these_sorted_files = [(int(RE.match(i).groups()[0]),i) for i in these_files if RE.match(i)]
these_sorted_files.sort()
these_sorted_files = [i[1] for i in these_sorted_files]
# these_sorted_files is now a list of the filenames a_001.jpg, a_002.jpg, etc.
for k in range(0, len(these_sorted_files), framestep):
frame1 = k
frame2 = k+framestep
this_output_name = 'C:\_%s_%s_%s-%s.avi' % (bn,j,frame1,frame2)
print '\n\n\toutput will be %s.' % this_output_name
f = open('temp.txt','w')
filenames = [os.path.join(folder,i)+'\n' for i in these_sorted_files[frame1:frame2]]
f.writelines(filenames)
f.close()
# Finally! Now execute the command to create the video.
cmd = '%s "mf://@temp.txt" -mf fps=25 -o %s -ovc lavc -lavcopts vcodec=mpeg4' % (mencoder,this_output_name)
os.system(cmd)
print '\n\nDONE with %s' % this_output_name
print 'Done with all marked folders.'
解决方法:
我认为最好的办法是使用外部程序来复用它们. ffmpeg是常年的最爱 – 优质的Windows版本可在http://ffmpeg.arrozcru.org/autobuilds/获得.使用ffmpeg,只需执行ffmpeg -i your_video_file.avi -i your_audio_file.wav -vcodec copy -acodec copy muxed_file.avi即可.
内容总结
以上是互联网集市为您收集整理的python – 如何将音频添加到视频?全部内容,希望文章能够帮你解决python – 如何将音频添加到视频?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。