Picamera将视频录制到不同的文件错误

时间:2017-09-03 14:09:22

标签: python raspberry-pi3

我正在使用Picamera V3录制一系列视频并将其存储在一系列文件中。例如,我录制了5个视频,每个视频都是2个。我将它们存储在files1到files5中。请参阅以下代码:

import os
from io import BytesIO
from time import sleep
import picamera
from datetime import datetime

""" initialization """
destination = '/home/pi/Desktop/video'

stop = 0

""" camera setup """
camera=picamera.PiCamera()

camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilization = False
camera.exposure_compensation = 0
camera.exposure_mode = 'auto'
camera.meter_mode = 'average'
camera.awb_mode = 'auto'
camera.image_effect = 'none'
camera.color_effects = None
camera.rotation = 0
camera.hflip = False
camera.vflip = False
camera.crop = (0.0, 0.0, 1.0, 1.0)
camera.resolution=(1024,768)

""" datetime get """
now = datetime.now()

""" file name def """
def get_file_name():
    return os.path.join(destination, \
                        "%s-%s-%s %s:%s:%s %d.h264" \
                        %(now.year, now.month, \
                          now.day, now.hour, now.minute, now.second, i))

# start loop 
while(stop==0):

    if stop==1:
        camera.stop_recording()
        camera.stop_preview()
        camera.close

    else:
        for i in range(1,5):
            # open file
            filename = get_file_name()
            video_file = open(filename,'wb')

            #start recording
            camera.start_preview()
            camera.start_recording(video_file)
            sleep(2)

            if i==4:
                stop = 1
                break

当我运行此代码时: 1,我只能获得2个文件而不是5个文件 2,我会收到错误:“相机已经在使用端口1” 我能做什么? 非常感谢你!

1 个答案:

答案 0 :(得分:0)

我对while循环进行了一些更改,现在我想在每次录制后检查键盘输入。

import os
from io import BytesIO
from time import sleep
import picamera
from datetime import datetime

""" initialization """
destination = '/home/pi/Desktop/video'
i = 0
stop = 0

""" camera setup """
camera=picamera.PiCamera()

camera.sharpness = 0
camera.contrast = 0
camera.brightness = 50
camera.saturation = 0
camera.ISO = 0
camera.video_stabilization = False
camera.exposure_compensation = 0
camera.exposure_mode = 'auto'
camera.meter_mode = 'average'
camera.awb_mode = 'auto'
camera.image_effect = 'none'
camera.color_effects = None
camera.rotation = 0
camera.hflip = False
camera.vflip = False
camera.crop = (0.0, 0.0, 1.0, 1.0)
camera.resolution=(1024,768)

""" datetime get """
now = datetime.now()

""" file name def """
def get_file_name():
    return os.path.join(destination, \
                        "%s-%s-%s %s:%s:%s %d.h264" \
                        %(now.year, now.month, \
                          now.day, now.hour, now.minute, now.second, i))

# start loop 
while(stop == 0):
#while(True):

    i = i + 1
    # open file
    filename = get_file_name()
    video_file = open(filename,'wb')

    #start recording
    camera.start_preview()
    camera.start_recording(video_file)
    sleep(2)

    camera.stop_recording()
    camera.stop_preview()

    video_file.close()

    stop = raw_input("input 1 to stop or 0 continue\n")

else:
    camera.close()
    print ("Done!")

现在我发现仍有问题: 1,无论输入1还是0,程序都会显示"完成!"并退出while循环。 2,有没有办法可以用来实现以下功能:如果我输入" 1"录音停止并退出while循环,否则循环将继续。 谢谢。