User avatar
jbeale
Posts: 3345
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

recording audio 24/7 from USB device

Tue Jul 05, 2016 2:26 pm

I'm recording audio continuously from a USB webcam (PS3 Eye, on Amazon for $5 each) and recording it into separate mp3 files on the SD card that are each 5 minutes long, with the simple bash script shown below. This generally works, but I see from comparing filenames there is a variable sized gap between recordings that is sometimes as long as 4 seconds. I don't know the reason for that delay interval (but it might have something to do with this Pi sometimes recording video from a RPi camera when motion is detected.) Is there a better way to do this, that would minimize the gaps between recordings?

Code: Select all

#!/bin/bash
# Record NSEC long MP3 files (8 kHz rate) from PS3 Eye device as mp3 files

NSEC=300  # how many seconds long each mp3 file should be
DIR="/home/pi/audio"

while true;
 do
  DT=$(date +"%Y-%m-%d_%H-%M-%S")  # current date & time for filename
  arecord -f S16_LE -c2 -r8000 -t raw -D plughw:1,0 -d $NSEC | lame -r -s 8 -b 64 - $DIR/$DT.mp3
 done

User avatar
RaTTuS
Posts: 9892
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: recording audio 24/7 from USB device

Tue Jul 05, 2016 2:34 pm

record to a RAM disk and move the files in the background ?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
jbeale
Posts: 3345
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: recording audio 24/7 from USB device

Tue Jul 05, 2016 6:20 pm

@RaTTuS: thanks; that is a good suggestion which I will try. I feel silly for not thinking of that myself. It also occurs to me that there will always be some gap due to delay with stopping and re-starting the recording process, but I ought to be able to get perfectly joined files with no samples missing, if the 'arecord' process simply ran continually (with a large enough internal buffer for sample data) and the file segmentation was done by the encoder, either lame or something else. I haven't figured out how to do that.

EDIT: Note to self: it may be that avconv or ffmpeg can do this. Below example from stackoverflow.com is splitting an existing mp3 file. "Segment" options at https://www.ffmpeg.org/ffmpeg-formats.h ... c-ssegment

Code: Select all

split mp3 file into parts:
avconv -i interview.mp3 -codec copy -map 0 -f segment -segment_format mp3 -segment_time 600 "iview%03d.mp3"

record audio from webcam: 
ffmpeg -f alsa -i plughw:CARD=U0x46d0x821,DEV=0 -acodec libmp3lame -t 20 output.mp4

from http://stackoverflow.com/questions/28602281/specify-timestamp-in-ffmpeg-video-segment-command
ffmpeg -i your_input -f segment -strftime 1 -segment_time 60 -segment_format mp4 out%Y-%m-%d_%H-%M-%S.mp4

User avatar
RaTTuS
Posts: 9892
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: recording audio 24/7 from USB device

Wed Jul 06, 2016 7:00 am

yeah....
I've not done this for a long time .. but record to a large circular buffer
and have processes that encode / strip parts out you should be able to time / trigger parts so you never over/under run the buffer
probably easier to strip 5 min chunks out and encode that in a separate thread
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
jbeale
Posts: 3345
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: recording audio 24/7 from USB device

Thu Jul 07, 2016 6:16 am

Note to self: this shows some signs of working. syntax still seems a little obscure to me.

Code: Select all

arecord -Dplughw:1,0 -f S16_LE -t raw -r 8000 -c 2 -N - | avconv \
 -f s16le -ar 8000 -ac 2 -i - -codec:a libmp3lame -map 0 -qscale:a 1 \
 -f segment -segment_time 60 -segment_format mp3 audio%04d.mp3
Synopsis: Continuously record raw signed 16-bit little-endian raw samples from 2nd found audio device (here, USB PS3-Eye-cam on RPi) at a 8kHz rate, 2-channel (stereo) and convert that to mp3 files which are each 60 seconds long, numbered sequentially.

I'd really like to have the current time&date in the filename as provided for by the current ffmpeg -strftime option, but it seems that feature is not available in the version of avconv in raspbian currently.

Edit: after compiling my own ffmpeg program, including the alsa library:

Code: Select all

sudo apt-get update; sudo apt-get upgrade
sudo apt-get -y --force-yes install libmp3lame-dev libx264-dev yasm git autoconf automake build-essential \ 
 libass-dev libfreetype6-dev libtheora-dev libtool libvorbis-dev pkg-config texi2html zlib1g-dev libjpeg-dev
cd
wget ftp://ftp.alsa-project.org/pub/lib/alsa-lib-1.1.1.tar.bz2
tar xjf alsa-lib-1.1.1.tar.bz2
cd alsa-lib-1.1.1/
./configure --host=arm-unknown-linux-gnueabi --prefix="/usr"
make; sudo make install
cd
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg/
./configure --enable-libfreetype --enable-gpl --enable-nonfree --enable-libx264 --enable-libass \
  --enable-libmp3lame --enable-omx --enable-omx-rpi --prefix="/usr" --bindir="/usr/local/bin"
make
sudo make install
ffmpeg -version
then this works to generate one mp4 video file each minute, with the time & date in the filename:

Code: Select all

ffmpeg -f v4l2 -framerate 9 -video_size 1280x960 -i /dev/video0 -c:v h264_omx -b:v 2000k   \
  -f segment -strftime 1 -segment_time 60 -segment_format mp4 out%Y-%m-%d_%H-%M-%S.mp4 \
  </dev/null >/dev/null 2>&1 &
Continually record 2-minute mp3 segments with time/date in filename, aligned to even real-time minutes

Code: Select all

  ffmpeg -f alsa -ac 2 -ar 8000 -i plughw:1 -map 0:0 -acodec libmp3lame \
  -b:a 96k -f segment -strftime 1 -segment_time 120 -segment_atclocktime 1 \
   ChA_%Y-%m-%d_%H-%M-%S.mp3

Return to “Graphics, sound and multimedia”

Who is online

Users browsing this forum: No registered users and 6 guests