代码之家  ›  专栏  ›  技术社区  ›  NeonCop

(python 3)从.h264自动转换为.mp4

  •  2
  • NeonCop  · 技术社区  · 7 年前

    我有一个Python 3代码,应该用来录制视频。不幸的是,我不希望它出现在.h264中,我需要将其转换为.mp4。使用其他StackOverflow线程作为模板(特别是 this 一),我认为最简单的方法是使用 subprocess.Popen 插入 MP4Box -add filename.h264 filename.mp4 进入终端,让它自动为我做这件事。不幸的是,Python脚本没有做任何事情,我也没有收到任何错误消息,所以我不知道出了什么问题。h264文件会出现在我想要的文件夹中,如果我手动将命令放入终端,会出现.mp4,但当我让它运行时,什么也没有发生。剧本的其余部分很有魅力。代码如下:

    #!/usr/bin/python
    
    from gpiozero import MotionSensor
    from gpiozero import Motor
    from picamera import PiCamera
    import subprocess
    import os.path
    import shlex
    import datetime as dt
    from time import sleep
    
    camera = PiCamera()
    pir = MotionSensor(4, 1, 100, .6, False)
    motor = Motor(3,14) #first number is forwards, second is backwards
    startupTime = 1
    recordingTime = 1
    collectionTime = 3
    resetTime = 30
    
    
    
    while True:
        sleep(startupTime) #delay a bit so installation can take place
    
        #wait for motion, then move the motor back and forth
        pir.wait_for_motion() 
        print("Motion Detected")
        #moves motor forward for 3 seconds at 25% speed
        motor.forward(.25)
        print("Strip Extending")
        sleep(3) 
        motor.stop()
        #leaves strip out for given amount of time
        print("Collecting Sample")
        sleep(collectionTime) 
        #moves motor backward for 3 seconds at 50% speed
        motor.backward(.5)
        print("Strip Retracting")
        sleep(3) 
        motor.stop()
    
        #Prep file for correct saving
        filename = dt.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.h264") #saves file as a date
        save_path= "/home/pi/ANALYSIS"
        completed_video= os.path.join(save_path, filename)
    
        #Start recording
        camera.start_recording(completed_video) #starts recording and saves it as filename
        print("Camera Recording")
        camera.annotate_text = dt.datetime . now() . strftime("%Y-%m-%d_%H.%M.%S")
        start=dt.datetime.now()
    
        #Keep recording until time runs out, annotate to make sure we have reference frame
        while (dt.datetime.now() - start).seconds < recordingTime: 
            camera.annotate_text = dt.datetime.now(). strftime("%Y-%m-%d_%H.%M.%S")
            camera.wait_recording(.2)
        camera.stop_recording()
    
        #Conversion to usable file format
        print("Camera finished recording... Beginning Analysis")
        from subprocess import CalledProcessError
        command = shlex.split("MP4Box -add {} {}.mp4".format(completed_video, os.path.splitext(filename)[0]))
        try:
            output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
        except CalledProcessError as e:
            print('FAIL:\ncmd:{}\noutput:{}'.format(e.cmd, e.output))
    
        #starts detecting again after given time
        sleep(resetTime)
        print("Ready for next sample")
    

    >Traceback (most recent call last):  
    >
        File "/home/pi/Detector.py", line 62, in <module> output = 
          subprocess.check_output(command, stderr=subprocess.STDOUT) 
        File "/usr/lib/python3.4/subprocess.py", line 620, in check_output raise 
          CalledProcessError(retcode, process.args, output=output) 
          subprocess.CalledProcessError: 
            Command '['MP4Box', '-add', '2017-07-11_15.34.49.h264.h264', '2017-07-11_15.34.49.h264.mp4']' 
    >
    Returned non-zero exit status 1"
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   stovfl    7 年前

    议论 :2017-07-11_15.34.49.h264.h264

    笔记 :The .mp4
    启动Python脚本的目录。这可能不同于 .h264 已保存!
    想一想,把这也变成一条绝对的道路。

    command = "MP4Box -add {} {}.mp4".format(completed_video, os.path.splitext(filename)[0])
    try:
        output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
    except subprocess.CalledProcessError as e:
        print('FAIL:\ncmd:{}\noutput:{}'.format(e.cmd, e.output))
    

    操作系统。删除(路径,*,dir\u fd=无)


    问题


    要同时捕获结果中的标准误差,请使用 stderr=subprocess.STDOUT :

    command = shlex.split("MP4Box -add {f}.h264 {f}.mp4".format(f=filename))
    output = subprocess.check_output(command, stderr=subprocess.STDOUT)
    print(output)
    

    shell=True 因为你没有给出一个完整的路径 MP4Box 因此需要一个Shell环境来查找 MP4Box .