(Python 3)从.h264到.mp4的自动转换 [英] (python 3) Auto-Conversion from .h264 to .mp4
问题描述
我有一个应该录制视频的Python 3代码。不幸的是,我不想在.h264中使用它,我需要将其转换为.mp4。使用其他StackOverflow线程作为模板(具体是这一个),我认为最简单的方法是使用 subprocess.Popen
插入 MP4Box -add filename.h264 filename.mp4
进入终端,并自动为我完成操作。不幸的是,Python脚本无法执行任何操作,并且我也没有收到任何错误消息,因此我不知道出了什么问题。 .h264文件出现在我想要的文件夹中,如果我手动将命令放入终端,则会出现.mp4,但是当我让它运行时,什么也没发生。脚本的其余部分就像一个吊饰。代码在这里:
#!/ usr / bin / python
从gpiozero import MotionSensor
来自gpiozero导入电机
来自picamera导入PiCamera
导入子过程
导入os.path
导入shlex
导入日期时间为dt
源自时间导入睡眠
相机= PiCamera()
pir = MotionSensor(4、1、100,.6,False)
motor = Motor(3,14)#第一个数字为正,第二个倒退
startupTime = 1
recordingTime = 1
collectionTime = 3
resetTime = 30
而True :
sleep(startupTime)#延迟一点,以便可以进行安装
#等待运动,然后来回移动电机
pir.wait_for_motion()
print(检测到运动)
#以25%的速度向前移动电机3秒钟
motor.forward(.25)
print( Strip Extending)Strip
sleep (3)
motor.stop()
#叶子在给定的时间内剥离
print( Col选择样本)
sleep(collectionTime)
#以50%的速度向后移动马达3秒钟
motor.backward(.5)
print( Strip Retracting)
sleep(3)
motor.stop()
#Prep文件,用于正确保存
filename = dt.datetime.now()。strftime(%Y- %m-%d_%H.%M.%S.h264)#将文件另存为日期
save_path = / home / pi / ANALYSIS
complete_video = os.path.join(save_path ,filename)
#开始记录
camera.start_recording(completed_video)#开始记录并将其另存为文件名
print( Camera Recording)
camera.annotate_text = dt.datetime。 now()。 strftime(%Y-%m-%d_%H.%M.%S)
start = dt.datetime.now()
#保持记录直到时间用完,注释以确保在(dt.datetime.now()-开始).seconds<时具有参考框架
。 recordingTime:
camera.annotate_text = dt.datetime.now()。 strftime(%Y-%m-%d_%H.%M.%S)
camera.wait_recording(.2)
camera.stop_recording()
#转换为可用的文件格式
print(摄像机完成了记录……开始分析)
从子流程导入CalledProcessError
命令= shlex.split( MP4Box -add {} {} .mp4 .format(completed_video,os.path.splitext(filename)[0]))
尝试:
output = subprocess.check_output(command,stderr = subprocess.STDOUT,shell = True)
除了CalledProcessError以外为e:
print('FAIL:\ncmd:{} \noutput:{}'。format(e.cmd,e.output))
#starts在给定时间后再次检测
sleep(resetTime)
print( Ready for next sample)
>追踪(最近一次通话):
>
文件 /home/pi/Detector.py,第62行,位于< module>输出=
subprocess.check_output(命令,stderr = subprocess.STDOUT)
文件 /usr/lib/python3.4/subprocess.py,行620,在check_output中提高
CalledProcessError( retcode,process.args,output = output)
subprocess.CalledProcessError:
命令'['MP4Box','-add','2017-07-11_15.34.49.h264.h264','2017 -07-11_15.34.49.h264.mp4']'
>
返回的非零退出状态1
评论:2017-07-11_15.34.49.h264.h264
您的文件路径/文件名错误,请更改为以下内容:
注意:
.mp4
保存到执行子进程的当前目录中,
Python脚本启动的目录中,这可能与不同。 h264
已保存!
考虑将其更改为绝对路径。
command = MP4Box -add {} {} .mp4 .format(completed_video,os.path.splitext(filename)[0])
尝试:
输出= subprocess.check_output(命令,stderr = subprocess.STDOUT,shell = True)
除了subprocess.CalledProcessError为e:
print('FAIL:\ncmd:{} \noutput: {}'。format(e.cmd,e.output))
os.remove(path,*,dir_fd = None)
删除(删除)文件路径。
问题:我什么也没得到错误消息,所以我不知道出了什么问题。
尝试以下操作:
要在结果中同时捕获标准错误,请使用 stderr = subprocess.STDOUT
:
command = shlex.split( MP4Box -add {f} .h264 {f} .mp4 .format(f =文件名))
输出= subprocess.check_output(命令,stderr = subprocess.STDOUT)
打印(输出)
我假设您需要 shell = True
,因为您没有给 MP4Box
,因此需要一个Shell环境才能找到 MP4Box
。
I have a Python 3 code that is supposed to record video. Unfortunately, I do not want it in .h264, I need it to convert to .mp4. Using other StackOverflow threads as a template (specifically this one), I thought the easiest way to do this would have been to use subprocess.Popen
to insert MP4Box -add filename.h264 filename.mp4
into the terminal and have it do it for me automatically. Unfortunately, the Python script doesn't do anything and I don't get any error messages, so I don't know what's going wrong. The .h264 file appears in the folder I want it to, if I manually put in the command into the terminal the .mp4 appears, but when I just let it run nothing happens. The rest of the script works like a charm. The code is here:
#!/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"
Comment: 2017-07-11_15.34.49.h264.h264
Your Filepath/Filename is wrong, change to the following:
Note: The
.mp4
is saved in to the Current Directory the Subprocess is executed,
the Directory the Python Script is started. This could be different as the.h264
are saved!
Think about to change that also to be a absolute Path.
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))
os.remove(path, *, dir_fd=None)
Remove (delete) the file path.
Question: I don't get any error messages, so I don't know what's going wrong.
Try the following:
To also capture standard error in the result, use 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)
I assume you need shell=True
as you dont give a Fullpath to MP4Box
so a Shell Environment is needed to find MP4Box
.
这篇关于(Python 3)从.h264到.mp4的自动转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!