问题描述
基本上(希望如此),我想从网络摄像头录制视频流并将其保存在特定目录中,然后在用户输入一些数字时将其杀死。我的解释不是很好,所以这是我目前正在做的事情:
Basically (hopefully) I want to record a video stream from a webcam and save it in a specific directory, then kill that when a user inputs some numbers. I'm not explaining this well so here's what I'm doing currently:
#!/bin/bash
while true
do
TIMESTAMP=$(date +"%Y.%m.%d_%H.%M")
read -p "Enter your number here: " YOURNUMBER
echo -e "Saving video stream:"
mkdir /home/$USER/orders/$YOURNUMBER
avconv -f video4linux2 -r 3 -fs 52428800 -i /dev/video0 /home/$USER/orders/$YOURNUMBER/$TIMESTAMP-$YOURNUMBER.avi
echo -e "Video complete!"
done
所以我想停止录制并开始新的录制在新的$ YOURNUMBER文件夹中。有任何想法吗? (对此非常陌生。请耐心等待!)
So I want to stop the recording and start a new on in the new $YOURNUMBER folder. Any ideas? (Quite new to this..be patient!)
更新:
感谢@TrueY
更新后的脚本(不需要mkdir如此删除):
Updated script (don't need to mkdir really so taken that out):
CPID=0
while :; do
read -p "Enter your number here: " YOURNUMBER
[ $CPID -ne 0 ] && kill -INT $CPID
TIMESTAMP=$(date +"%Y.%m.%d_%H.%M")
avconv -f video4linux2 -r 3 -fs 52428800 -i /dev/video0 /home/$USER/orders/$YOURNUMBER[packed-$TIMESTAMP].avi > /dev/null 2>&1 &
CPID=$!
done
唯一的麻烦是我(或最终用户)必须输入数字两次,然后再次开始记录。.
Only trouble is that I (or the end user) has to enter the number twice before it starts recording it again..
推荐答案
更新
尝试类似的操作(对OP的代码进行一些修改):
Try something like this (OP's code modified a little bit):
#!/usr/bin/bash
stop() { [ $CPID -ne 0 ] && kill -INT $CPID && wait $CPID && echo "Killed $CPID"; }
trap "stop; exit" INT
CPID=0
while :; do
read -p "Enter your number here: " YOURNUMBER
stop
[ "$YOURNUMBER" == quit ] && break;
TIMESTAMP=$(date +"%Y.%m.%d_%H.%M")
avconv -f video4linux2 -r 3 -fs 52428800 -i /dev/video0 /home/$USER/orders/$YOURNUMBER/$TIMESTAMP-$YOURNUMBER.avi&
CPID=$!
echo -e "Video complete!"
done
此操作开始,以便用户可以输入新号码。也许应该重定向到日志文件或/ dev / null。此外,还应该测试哪个信号会优雅地停止。 -INT
等同于ctrl + c。它不起作用,请尝试 -HUP
, -TERM
甚至是 -Kill
如果没有其他帮助。也许您应该实施陷阱
来捕获 INT
信号,以杀死最后一个如果按下ctrl + c,则将其按下。
This starts avconv in the background so the user can enter a new number. Maybe the stdout and stderr of avconv should be redirected to a log file or to /dev/null. Also it should be tested which signal stops avconv gracefully. -INT
is equivalent to ctrl+c. It it does not work, try -HUP
, -TERM
or even -KILL
if nothing else helps. Maybe You should implement a trap
to catch INT
signals to kill the last avconv it ctrl+c is pressed.
这篇关于在用户输入上停止avconv Bash脚本并继续循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!