如何给视频添加音频?

发布于 2024-09-12 07:19:30 字数 2540 浏览 6 评论 0原文

我录制一个没有声音的视频。我保存快照,然后将它们编译为 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.'

I record a video with no sound. I save a snapshots and then I compile them to .avi with 25FPS. Now I want to record audio (instead time.sleep(0.04) between following snapshots I will record audio in this time) and compile it wits video. Now I have avi file, and wave file, and I find a solution to mix them.

Python, Win32

It is my "video recorder" which use '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.'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-09-19 07:19:30

我认为最好的选择是使用外部程序来混合它们。 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 即可完成。

I think the best bet would be to use an external program to mux them. ffmpeg is a perennial favorite -- quality Windows builds are available at http://ffmpeg.arrozcru.org/autobuilds/ . With ffmpeg, just do ffmpeg -i your_video_file.avi -i your_audio_file.wav -vcodec copy -acodec copy muxed_file.avi and you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文