python中的全局变量,初始化和终止

时间:2016-04-14 18:25:41

标签: python function raspberry-pi global-variables

我正在制作一个程序,让我使用rapsberry pi相机lib录制视频用于python,这是一个小问题,当你运行camera = picamera.PiCamera()相机启用并被使用时在程序结束时,我想做的只是在录制时启用它,在录制完成时停止但仍保持程序活动。

我需要什么: 如何为picamera创建全局变量以及如何终止它。

我的部分代码相关:

camera = picamera.PiCamera()
camera.resolution = (1920, 1080)
filename = ""

#Start recording video into raw file
def start_record():
                print("Starting recording");
                reset_tmp()
                global filename
                filename = "vid/" + str(int(time.time()));
                camera.start_recording(filename+".h264");

#Stop recording and convert h264 raw file to mp4 and remove raw file
def stop_record():
                print("Stopping recording");
                reset_tmp()
                global filename
                camera.stop_recording()
                os.system("MP4Box -fps 30 -add "+filename+".h264"+" "+filename+".mp4");
                os.system("rm "+filename+".h264");

更新的代码版本,功能齐全 对于那些寻找标题解决方案的人,你必须使用del关键字去除变量,但picamera库有一个名为.close()的函数来终止对象,这里'我的固定代码:

camera = None
filename = ""

#Start recording video into raw file
def start_record():
                print("Starting recording");
                reset_tmp()
                global filename
                filename = "vid/" + str(int(time.time()));

                global camera
                camera = picamera.PiCamera()
                camera.resolution = (1920, 1080)
                camera.start_recording(filename+".h264");

#Stop recording and convert h264 raw file to mp4 and remove raw file
def stop_record():
                print("Stopping recording");
                reset_tmp()
                global filename
                global camera
                camera.stop_recording()
                camera.close()
                os.system("MP4Box -fps 30 -add "+filename+".h264"+" "+filename+".mp4");
                os.system("rm "+filename+".h264");

2 个答案:

答案 0 :(得分:2)

这里不需要全局变量。只需使用返回值:

import os
import subprocess
import time

import picamera

def start_record(resolution=(1920, 1080)):
    """Start recording video into raw file"""
    print("Starting recording")
    camera = picamera.PiCamera()
    camera.resolution = resolution
    reset_tmp()
    filename = os.path.join('vid', '{}.h264'.format(int(time.time())))
    camera.start_recording(filename)
    return camera, filename


def stop_record(camera, filename):
    """Stop recording and convert h264 raw file to mp4 and remove raw file"""
    print("Stopping recording")
    reset_tmp()
    camera.stop_recording()
    mp4_fn = os.path.splitext(filename)[0] + '.mp4'
    subprocess.call(['MP4Box', '-fps', '30', '-add', mp4_fn])
    os.remove(filename)

现在调用start函数:

camera, filename = start_record()

以及后来的停止功能:

stop_record(camera, filename)

答案 1 :(得分:-2)

怎么样:

camera = None
camera.resolution = (1920, 1080)
filename = ""

#Start recording video into raw file
def start_record():
                print("Starting recording");
                reset_tmp()
                global filename
                filename = "vid/" + str(int(time.time()));

                global camera
                camera = picamera.PiCamera()
                camera.start_recording(filename+".h264");

基本思想是移动代码以启动相机进入功能 - 全局可用于修改全局变量。