jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

New raspivid test release - please try.

Fri Nov 15, 2013 3:05 pm

https://github.com/JamesH65/userland/tree/segmentation

This allows splitting up the files into seamless segments using --segment and --wrap (if you want to overwrite earlier files once you reach a certain number). You'll need to put a %d in the filename somewhere to ensure you get multiple filenames.

Please test and report back - if OK, I'll push it to the Raspi github.

TIA.

James
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

gordon77
Posts: 5034
Joined: Sun Aug 05, 2012 3:12 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 4:12 pm

Jamesh,

I haven't really tried raspivid but is there an option, or possibility of an option where it could save stills as well as video. I imagine something where it could be recording video and you trigger it in some way to record a still , or maybe set it to store a still at set periods or after so many frames.

Thanks

Gordon77

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 4:47 pm

That's on my list of stuff to do. The raspi should be capable of capturing a still during video (you will drop frames in the video though, in most cases). I've never got round to adding it.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

ethanol100
Posts: 640
Joined: Wed Oct 02, 2013 12:28 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 5:11 pm

Thank you for the nice new feature. Have compiled raspivid successful. But how should I invoke it?

I have tried "raspivid --segment 1000 -o vid%d.h264". This creates only one file vid1.h264.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 5:14 pm

can you try with a longer segment (-sg 5000) and a longer time e.g. -t 30000

What's happening is that the file can only change on an I-frame in the stream. These are by default every second (maybe two?), and I think what's happening is it can never find an appropriate time to switch - the timescale is too short.

Or there is a bug!

EDIT. Or I've got the wrap parameter handling wrong - can you try with -wr 100 or something. I've about to get in the car so cannot check it myself.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 5:16 pm

I should add you will need a fairly recent firmware (last month or two), as this need the inline headers option enabled in the GPU.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

ethanol100
Posts: 640
Joined: Wed Oct 02, 2013 12:28 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 5:26 pm

Yes, with -wr 100 it splits the files in segments. I will do some more tests later, seems to works fine. I can play the segments. And "raspivid -sg 2000 -wr 10 -o vid%d.h264" will create 2s segments, which are the smallest segments with the default I-frame setting, exactly like you have said.

Code: Select all

pData->pstate->segmentNumber++;
if (pData->pstate->segmentNumber > pData->pstate->segmentWrap)
   pData->pstate->segmentNumber = 1;
will be always true, if segmentWrap is zero. Adding "&& pData->pstate->segmentWrap!=0" as a quick fix.
Last edited by ethanol100 on Fri Nov 15, 2013 6:08 pm, edited 1 time in total.

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

Re: New raspivid test release - please try.

Fri Nov 15, 2013 6:04 pm

I'm still stumbling over git syntax, eventually I just downloaded RaspiVid.c directly:

Code: Select all

cd /home/pi/userland/host_applications/linux/apps/raspicam/
wget https://raw.github.com/JamesH65/userland/4221b97b726945e38038f67c03b06a8a62766c68/host_applications/linux/apps/raspicam/RaspiVid.c
cd ../../../..
./buildme
Sure enough I have raspicam v1.3.7 so then I tried

Code: Select all

raspivid -sg 5000 -t 30000 -o test%03d.h264
which ran about 30 seconds as expected, but I got only a single file test001.h264. So maybe my firmware of 2 Oct 2013 is not recent enough. Attempting to update now but the server seems... very.... slow... at the moment (D/L speed of 32 kB/sec)

UPDATE: I guess I had the wrong syntax? Seems like it does not split files unless the -wr option is used. I do get three playable files from

Code: Select all

$ raspivid -sg 2000 -wr 10 -o vid%d.h264
$ ll
total 9924
-rw-r--r-- 1 pi pi 3937943 Nov 15 10:08 vid1.h264
-rw-r--r-- 1 pi pi 4546112 Nov 15 10:08 vid2.h264
-rw-r--r-- 1 pi pi 1671791 Nov 15 10:08 vid3.h264

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

Re: New raspivid test release - please try.

Fri Nov 15, 2013 6:25 pm

Ok, this is looking promising. Tried this 20 second sequence which got split into 9 files (not 10?) with some motion in the 4th, 5th and 6th set of files. That's very clear looking at the file sizes.

Code: Select all

pi@raspberrypi:~/pics/test1$ raspivid -w 640 -h 480 -sg 2000 -wr 20 -t 20000 -qp 40 -o test%03d.h264
pi@raspberrypi:~/pics/test1$ ll
total 1260
-rw-r--r-- 1 pi pi  93081 Nov 15 10:23 test001.h264
-rw-r--r-- 1 pi pi  95609 Nov 15 10:23 test002.h264
-rw-r--r-- 1 pi pi  94503 Nov 15 10:23 test003.h264
-rw-r--r-- 1 pi pi 136129 Nov 15 10:23 test004.h264
-rw-r--r-- 1 pi pi 474724 Nov 15 10:23 test005.h264
-rw-r--r-- 1 pi pi 110121 Nov 15 10:23 test006.h264
-rw-r--r-- 1 pi pi  90962 Nov 15 10:23 test007.h264
-rw-r--r-- 1 pi pi  89366 Nov 15 10:23 test008.h264
-rw-r--r-- 1 pi pi  86544 Nov 15 10:23 test009.h264
EDIT: Ok, this is odd. The reason 'test005.h264' is so large, is that it is 4 seconds long and contains 120 frames instead of 60 frames like the rest. I used avconv -i test005.h264 Still%04d.jpg to extract each still frame to confirm. That's why there were only 9 files, instead of 10 files for the 20 second test. Now it is true that 'test005.h264' was the segment with nearly all the motion, so I'm guessing the h264 encoder decided to change its placement of I-frame insertion and that fouled up the timing of the splitting process. So for this idea to work as expected we may also need the ability to force the placement of regular I-frames instead of adaptive. Or accept variable-size file splits, maybe it even helps as in this particular case it is highlighting the motion.

EDIT: Trying to confirm if autoexposure is maintained across image segments. Looking at the extracted still images from avconv, it appears it is not. But maybe this is an artifact of that still extraction? Actually joining the video segments with

Code: Select all

avconv -i concat:test001.h264\|test002.h264\|test003.h264\|test004.h264 -c copy sum4.h264
omxplayer sum4.h264
actually gives me a smooth output without visible seams. Hmmm....

tvjon
Posts: 778
Joined: Mon Jan 07, 2013 9:11 am

Re: New raspivid test release - please try.

Fri Nov 15, 2013 7:33 pm

jamesh wrote:....
Please test and report back - if OK, I'll push it to the Raspi github.

TIA.

James
Thank you James.
I've built it on a RPi, & tried low fps thus:

pi@r2 ~ $ raspivid -t 50000 -ex night -fps 2 -sg 2000 -wr 20 -o vid%d.h264

I built a fork of omxplayer a few weeks ago which allowed me to pause h264's. My SD corrupted though, so I've rebuilt the omxplayer fork, but the pause has vanished, so I need to see if I can find why it's gone. The official recent omxplayer builds won't play raw h264 at all. Hello_video plays them of course but no pause. Is that a difficult feature to add?

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

Re: New raspivid test release - please try.

Fri Nov 15, 2013 7:42 pm

I had a close look at a very slowed-down version of the segmented and re-joined files. Actually it looks very good to me, I don't see any sign that the files were ever split.

Code: Select all

raspivid -w 640 -h 480 -sg 2000 -wr 20 -t 8000 -qp 40 -o test%03d.h264
avconv -i concat:test001.h264\|test002.h264\|test003.h264\|test004.h264 -c copy sum4.h264
sudo apt-get install gpac
MP4Box -add sum4.h264 -fps 10 slow.mp4
omxplayer slow.mp4

ethanol100
Posts: 640
Joined: Wed Oct 02, 2013 12:28 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 7:47 pm

jbeale wrote:...Now it is true that 'test005.h264' was the segment with nearly all the motion, so I'm guessing the h264 encoder decided to change its placement of I-frame insertion and that fouled up the timing of the splitting process. So for this idea to work as expected we may also need the ability to force the placement of regular I-frames instead of adaptive. Or accept variable-size file splits, maybe it even helps as in this particular case it is highlighting the motion.
You can use the -g option, i.e. if you choose fps=30 you can try -g 30 to take an i-frame every second.

I think we can also use MP4Box to add all files to a mp4 file, which we should be able to pause.

Code: Select all

MP4Box -cat vid1.h264 -cat vid2.h264 -cat vid3.h264 vid.mp4

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

Re: New raspivid test release - please try.

Fri Nov 15, 2013 8:00 pm

turns out that you can simply concatenate the separate .h264 files directly and get a playable file:

Code: Select all

raspivid -g 30 -fps 30 -w 640 -h 480 -sg 1100 -wr 20 -t 30000 -qp 30 -o test%03d.h264
cat test*.h264 > sum.h264
omxplayer sum.h264
works for me.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: New raspivid test release - please try.

Fri Nov 15, 2013 9:26 pm

Code: Select all

omxplayer <(cat *.h264)
Bash process substitution.

This segmentation is sweet. If only we could get a container format with timestamps now to make auto-gain with variable frame rates feasible.






( ... And full sensor video ...)

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

Re: New raspivid test release - please try.

Fri Nov 15, 2013 11:39 pm

Looking for a simple H.264 analyzer that can (for example) read a .h264 file and dump out a list like IPPPPPPIPPPP to show you where your I and P frames are in the video stream... I know about CodecVisa but it's way more than I want (and also it doesn't run on the Pi).

Looks like the MPEG4IP / "h264_parse" code (2005 Bill May / Cisco Systems) does what I want. http://www.w6rz.net/h264_parse.zip
When I use the raspivid '-g 30' option I do get consistent groups of 30 frames, being one I frame followed by twenty-nine P frames. Testing with this command

Code: Select all

raspivid -g 30 -fps 30 -w 640 -h 480 -sg 10000 -wr 50 -t 30000 -qp 30 -o test%03d.h264
and using h264_parse on the resulting .h264 files I see each P frame has quite a size range, from 928 on static out-of-focus scene, to 32181 on a detailed frame with motion. So that is a ratio of 32:1 which is a pretty big "motion/no motion" signal.

I wouldn't complain if someone could make "exceeding a P-frame size threshold" a trigger for saving a file from within raspivid, but even just looking at the total filesize, you can see the difference between lots of motion and no motion. In this case each GOP is 1 I + 9 P frames and a file is 5 GOPs for a total of 50 frames, covering 10 seconds at 5 fps. I set the segment at 9 sec to achieve 10 seconds (if I'd put 10, it would have just missed the next I-frame and gone to 6 GOPs or 12 seconds). Here the filenames had wrapped around after test020 and test001.h264 is incomplete; still being written.

Code: Select all

$ raspivid -g 10 -fps 5 -w 640 -h 480 -sg 9000 -wr 20 -t 0 -qp 30 -o test%03d.h264
$ ll
-rw-r--r-- 1 pi pi   28672 Nov 15 16:31 test001.h264
-rw-r--r-- 1 pi pi  763353 Nov 15 16:28 test002.h264
-rw-r--r-- 1 pi pi 1357693 Nov 15 16:28 test003.h264
-rw-r--r-- 1 pi pi 1664864 Nov 15 16:28 test004.h264   <= high detail, in motion
-rw-r--r-- 1 pi pi  977903 Nov 15 16:28 test005.h264
-rw-r--r-- 1 pi pi  757771 Nov 15 16:28 test006.h264
-rw-r--r-- 1 pi pi  699633 Nov 15 16:29 test007.h264
-rw-r--r-- 1 pi pi  672355 Nov 15 16:29 test008.h264
-rw-r--r-- 1 pi pi  532064 Nov 15 16:29 test009.h264
-rw-r--r-- 1 pi pi   97760 Nov 15 16:29 test010.h264
-rw-r--r-- 1 pi pi   87114 Nov 15 16:29 test011.h264
-rw-r--r-- 1 pi pi   86690 Nov 15 16:29 test012.h264
-rw-r--r-- 1 pi pi  128390 Nov 15 16:30 test013.h264
-rw-r--r-- 1 pi pi  533110 Nov 15 16:30 test014.h264
-rw-r--r-- 1 pi pi  576913 Nov 15 16:30 test015.h264
-rw-r--r-- 1 pi pi  439669 Nov 15 16:30 test016.h264
-rw-r--r-- 1 pi pi  122173 Nov 15 16:30 test017.h264
-rw-r--r-- 1 pi pi   91557 Nov 15 16:30 test018.h264   <= no motion, and out of focus
-rw-r--r-- 1 pi pi   90267 Nov 15 16:31 test019.h264
-rw-r--r-- 1 pi pi   91134 Nov 15 16:31 test020.h264
To sketch out an idea for motion capture in pseudocode:

Code: Select all

set $average_filesize = SOME_VALUE
# ping-pong between two files in the ramdisk, each one is exactly two seconds long (10 frames at 5 fps)
sudo raspivid -g 10 -fps 5 -w 1280 -h 720 -sg 1000 -wr 2 -t 0 -qp 30 -o /run/shm/test%d.h264 &
do:
  wait until there are two *.h264 files in /run/shm   # (first file is done, second file is in progress)
  set $filename = oldest file in ( /run/shm/*.h264 )
  IF ( filesize($filename) > ($average_filesize)*1.1 ) THEN
       copy ($filename)  (/home/pi/video/$time$date.h264)
  set $average_filesize = ($average_filesize * 0.9) + (filesize($filename) * 0.1)
  delete $filename
done
To JamesH: Looking good! this version of raspivid is working for me, thanks for this very useful update.
Last edited by jbeale on Sat Nov 16, 2013 3:39 pm, edited 1 time in total.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Sat Nov 16, 2013 11:15 am

ethanol100 wrote:Yes, with -wr 100 it splits the files in segments. I will do some more tests later, seems to works fine. I can play the segments. And "raspivid -sg 2000 -wr 10 -o vid%d.h264" will create 2s segments, which are the smallest segments with the default I-frame setting, exactly like you have said.

Code: Select all

pData->pstate->segmentNumber++;
if (pData->pstate->segmentNumber > pData->pstate->segmentWrap)
   pData->pstate->segmentNumber = 1;
will be always true, if segmentWrap is zero. Adding "&& pData->pstate->segmentWrap!=0" as a quick fix.
Yup, bug there. I had this working correctly, then made some changes to do with generating the filename, and must have broken it. I'll try and fix it today.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Sat Nov 16, 2013 11:20 am

Just some detail of what it does. It really just splits the output from the encoder on the fly to multiple files - it makes not additions/changes to the stream at all. To ensure that the file is playable, the inline header option is enabled - this puts width/height information in a packet just before each I-frame, so it doesn't matter if you split the stream up, as long as you do it at one of those headers, the file will be playable. Note all this is done down stream of the camera and encoder, so it should have no impact on the camera settings.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Sat Nov 16, 2013 11:29 am

OK, just pushed a fix for the wrap issue, so please do a git pull --rebase on the segmentation branch to get the latest code.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

ethanol100
Posts: 640
Joined: Wed Oct 02, 2013 12:28 pm

Re: New raspivid test release - please try.

Sat Nov 16, 2013 12:42 pm

Yes, now it works without setting -wr.

caerandir
Posts: 131
Joined: Tue Dec 18, 2012 11:26 am
Location: Bonn, Germany

Re: New raspivid test release - please try.

Sat Nov 16, 2013 5:13 pm

Hi jamesh,

thanks for the additions to raspivid! I was trying to use this with my motion detection script only to realize that the splitting does not work with option "-b 0" set, which I need for VBR recording. It just starts recording into file #1 and never switches to #2+. Without -b 0, everything works perfect!!!

Caerandir

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26652
Joined: Sat Jul 30, 2011 7:41 pm

Re: New raspivid test release - please try.

Sat Nov 16, 2013 6:08 pm

You have set the quantisation level as well?

Although I cannot see how setting the bitrate to that would have any effect whatsoever on the splitting, unless for some reason it also disables inline headers. I'll nee to check the streams, which I cannot do until Monday.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

caerandir
Posts: 131
Joined: Tue Dec 18, 2012 11:26 am
Location: Bonn, Germany

Re: New raspivid test release - please try.

Sat Nov 16, 2013 6:18 pm

Yes, the following works:

Code: Select all

raspivid -o /dev/shm/test%02d.h264 -fps 30 -g 10 -sg 1000 -wr 10 -t 0 -qp 25 -sa -100 -e
This one does not (only difference -b 0):

Code: Select all

raspivid -o /dev/shm/test%02d.h264 -fps 30 -g 10 -sg 1000 -wr 10 -t 0 -b 0 -qp 25 -sa -100 -e
Leaving out -wr does not alter anything. Including -ih also no effect.

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

Re: New raspivid test release - please try.

Sat Nov 16, 2013 7:16 pm

caerandir wrote:thanks for the additions to raspivid! I was trying to use this with my motion detection script only to realize that the splitting does not work with option "-b 0" set, which I need for VBR recording. It just starts recording into file #1 and never switches to #2+. Without -b 0, everything works perfect!!!
r
with the -qp parameter (without -b 0) you get quite highly variable bitrate, as I posted above, if that helps.

caerandir
Posts: 131
Joined: Tue Dec 18, 2012 11:26 am
Location: Bonn, Germany

Re: New raspivid test release - please try.

Sat Nov 16, 2013 11:11 pm

Thanks for the hint - I should have read your post more closely, would have saved me quite some time... I found out the same during my tests. You'll find my now somewhat more sophisticated script here: http://www.raspberrypi.org/phpBB3/viewt ... 55#p454855

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

example script for motion detection

Sun Nov 17, 2013 12:48 am

Ok, this code below been tested and works... as a "poor man's" motion detector. I commented out the raspivid call for now, I did that separately outside the script to avoid accidentally invoking it twice during debug/testing. I determined the filesize threshold of 730000 experimentally in my case. Looking at the resulting video snippets with omxplayer <(cat /home/pi/video/*.h264) about half of them were "legitimate" motion of cars and people, the rest were blowing leaves and moving shadows of trees in the wind. A bunch of leaves blowing across a pathway causes a lot of moving detail so it's a big bitrate signal. After some hours and with the sun lower in the sky, the average filesize dropped to around 500k and I did not detect any more motion, so as expected this needs to track deviations from the running average.

Code: Select all

#!/bin/bash

DIR=/run/shm
DEST=$HOME/video
#THRESH=730000
THRESH=723000
CLIM=2

# raspivid -g 10 -fps 5 -w 1280 -h 720 -sg 1000 -wr 3 -t 0 -qp 30 -o $DIR/t%02d.h264 &

while [ 1 ]; do
 sleep 1
 FCOUNT=$(find $DIR -iname "*.h264" | wc -l)
 while [ $FCOUNT -lt $CLIM ]; do
  sleep 1
  # count files in current directory
  FCOUNT=$(find $DIR -iname "*.h264" | wc -l)
#  echo $FCOUNT
 done

 NOW=$(date +"%Y-%m-%d_%H-%M-%S")
# echo 'Now have '$FCOUNT' files at '$NOW
# ls -tr1 $DIR
 OLDEST="$(ls -tr1 $DIR/*.h264 | head -1)"

 SIZE=$(stat -c "%s" $OLDEST)
# echo 'Oldest file '$OLDEST' is '$SIZE
 if [ $SIZE -gt $THRESH ]
  then
   cp $OLDEST $DEST/$NOW.h264
   echo 'Saved: file size '$SIZE
  fi
# echo Now removing $OLDEST
 rm $OLDEST
done

Return to “Camera board”