2010-07-30 182 views
4

我錄製了沒有聲音的視頻。我保存了快照,然後用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.' 
+0

您使用哪些庫來錄製視頻?你能否提供一些示例代碼? – 2010-07-30 12:40:00

+0

在這裏;) – CarolusPl 2010-07-30 12:45:02

回答

3

我認爲最好的辦法是使用外部程序來混流。 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即可完成。

+0

在我看來,它不起作用......但mayby我做錯了。 您是否知道使用ffmpeg在一個函數中錄製帶有音頻的視頻的命令? – CarolusPl 2010-07-30 13:55:17

+0

你說你有AVI和WAV文件;上面的命令會將它們複合成帶有聲音的AVI。 – AKX 2010-07-30 14:52:23

+0

@AKX你可以plz指導我如何在Python中使用上述語句'ffmpeg -i your_video_file.avi -i your_audio_file.wav -vcodec copy -acodec copy muxed_file.avi' – Fahadkalis 2015-01-29 15:34:38