tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Fri Jul 11, 2014 6:22 am

I made this little app for viewing a video stream from the pi's camera using 'Motion':

https://itunes.apple.com/us/app/picam/id894461908?mt=8

raspiruedi
Posts: 9
Joined: Fri Jul 11, 2014 2:19 pm

Re: MMAL Motion - tuning & optimisations

Fri Jul 11, 2014 4:44 pm

tgwaste wrote:etc/rc.local never worked for me for auto startup.

i wrote this script. edit paths:
then just put
@hourly root /path/to/motion.sh start >/dev/null
in crontab
i restart motion every hour because it gets locked up so I do:
@hourly root /path/to/motion.sh restart >/dev/null

#!/bin/bash
pid=`ps ax | grep motion | grep conf | grep -v grep | awk '{print $1}'`
cmd="/usr/src/motion/motion -c /usr/src/motion/motion.conf"

if [ "$1" = "start" ]; then
if [ "$pid" != "" ] && [ "$pid" -gt "0" ]; then
# already running
echo "motion: already running"
exit
...
...

echo "usage: motion.sh start|stop|restart"
@tgwaste
I am a newbie concerning Linux and Raspberry PI as well.
My hardware:
Raspberry Pi Ver. 2.0 Model B 512MB
Raspi Camera Module Noir
SD-Card Transcend 90MB/s 600x
I have running mmal-motion with secondary_buffer. Now i would like to start the engine by crontab like You have suggested in Your post.
Is it right that I have to put BOTH '@hourly...' lines in the crontab - like:

@hourly root /path/to/motion.sh start >/dev/null
@hourly root /path/to/motion.sh restart >/dev/null

Second question is:
Is it right to put the 'motion.sh' into the 'bin' folder?

Would be nice if You could push me into the right direction.
Thanks in advance.

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Sat Jul 12, 2014 6:04 pm

raspiruedi wrote:
tgwaste wrote:etc/rc.local never worked for me for auto startup.

i wrote this script. edit paths:
then just put
@hourly root /path/to/motion.sh start >/dev/null
in crontab
i restart motion every hour because it gets locked up so I do:
@hourly root /path/to/motion.sh restart >/dev/null

#!/bin/bash
pid=`ps ax | grep motion | grep conf | grep -v grep | awk '{print $1}'`
cmd="/usr/src/motion/motion -c /usr/src/motion/motion.conf"

if [ "$1" = "start" ]; then
if [ "$pid" != "" ] && [ "$pid" -gt "0" ]; then
# already running
echo "motion: already running"
exit
...
...

echo "usage: motion.sh start|stop|restart"
@tgwaste
I am a newbie concerning Linux and Raspberry PI as well.
My hardware:
Raspberry Pi Ver. 2.0 Model B 512MB
Raspi Camera Module Noir
SD-Card Transcend 90MB/s 600x
I have running mmal-motion with secondary_buffer. Now i would like to start the engine by crontab like You have suggested in Your post.
Is it right that I have to put BOTH '@hourly...' lines in the crontab - like:

@hourly root /path/to/motion.sh start >/dev/null
@hourly root /path/to/motion.sh restart >/dev/null

Second question is:
Is it right to put the 'motion.sh' into the 'bin' folder?

Would be nice if You could push me into the right direction.
Thanks in advance.
Hello,
So I have never been able to get motion to start on boot up. There is some issue there I have no figured out yet (when trying to put it in rc.local) and also Moiton will often lock up on me and stop producing video.

For this reason I use:
@hourly root /path/to/motion.sh restart >/dev/null

in my crontab. which will restart motion every hour.

if you just want it to start once (the first hour after a boot up). then just use 'start':

@hourly root /path/to/motion.sh start >/dev/null


make sure to edit '/path/to/motion.sh' to your path and edit
/usr/src/motion/motion -c /usr/src/motion/motion.conf
to your paths as well..

I keep it in /usr/src (don't ask me why so my files look like:

raspi/root# ls -1 /usr/src/motion/
images
mailer.sh
motion
motion.conf
motion.log
motion.sh
nohup.out

raspiruedi
Posts: 9
Joined: Fri Jul 11, 2014 2:19 pm

Re: MMAL Motion - tuning & optimisations

Sun Jul 13, 2014 11:55 am

@tgwaste
thanks for your quick reply.
I will try your tips on Monday.

raspiruedi
Posts: 9
Joined: Fri Jul 11, 2014 2:19 pm

Re: MMAL Motion - tuning & optimisations

Mon Jul 14, 2014 10:02 am

@tgwaste
I can't get motion restarted.
My situation is as following:

Path of motion program: /usr/local/bin/

/home/pi/motion = path of other files:
videos
motion.conf
motion.log
motionstart.sh (I renamed motion.sh)

Do I need 'nohup.out' ? I found the command 'nohup' in the start sections of Your script.

Within the script I changed the cmd path to:
/usr/local/bin/motion -c /home/pi/motion/motion.conf
In the last line of the script (echo ...) I changed:
'motion.sh' to 'motionstart.sh'.

Initially I start motion 'by hand' in the console:
motion -c /home/pi/motion/motion.conf
or
sudo ....
Both alternatives work saving the movie files and closing motion by the script. But motion is not restarted (as well as httpd).

The motion.log reads:
11:37:21] MMAL Camera cleanup
11:37:22] main: Threads finished
11:37:23] httpd_run: motion-httpd - Finis
11:37:23] httpd_run: motion-httpd Closing
11:37:23] motion_web_control: motion-httpd thread exit
11:37:23] main: Motion terminating
11:37:23] motion_remove_pid: Closing logfile (/home/pi/motion/motion.log).

Thanks for Your help in advance.

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Mon Jul 14, 2014 7:06 pm

I made a script for you based on what you said.

get via:

wget https://dl.dropboxusercontent.com/u/61083309/motion.sh

chris-mb-ca
Posts: 2
Joined: Mon Jul 14, 2014 6:40 pm

Re: MMAL Motion - tuning & optimisations

Mon Jul 14, 2014 11:25 pm

Hi folks i have set up mmal-motion / rpi cam to email me a photo and a video to my gmail account when motion is detected however my iphone 4 wont play the msmpeg4 avi file... Any recomendations?
Codec /pallette? Etc? Framerate bitrate?
I Have set the size to 640x480
Thanks :-)

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Tue Jul 15, 2014 7:02 am

dozencrows wrote:Likely next steps are to make a simpler test case to reproduce this so they can debug it, and also give them the option to look at the motion app itself if that's required.
Are there any news on that and will the necessary steps be posted here? If it is "only" a firmware issue than I suspect we have to update the firmware only, not motion (I am affected as well...)
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Tue Jul 15, 2014 12:49 pm

For the current state of play, please read from this post onwards: http://www.raspberrypi.org/forums/viewt ... 46#p576146

raspiruedi
Posts: 9
Joined: Fri Jul 11, 2014 2:19 pm

Re: MMAL Motion - tuning & optimisations

Tue Jul 15, 2014 1:44 pm

@tgwaste
wow ... great service!
I appreciate Your script expecially made for my needs!

Stopping motion works but restarting doesn't work.
motion.log reads at the end:

Code: Select all

[Jul 15 15:07:18] motion_loop: Thread exiting
[Jul 15 15:07:18] stream_stop: Closing motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] stream_stop: Closed motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] MMAL Camera cleanup
[Jul 15 15:07:19] main: Threads finished
[Jul 15 15:07:19] httpd_run: motion-httpd - Finishing
[Jul 15 15:07:19] httpd_run: motion-httpd Closing
[Jul 15 15:07:19] motion_web_control: motion-httpd thread exit
[Jul 15 15:07:20] main: Motion terminating
[Jul 15 15:07:20] motion_remove_pid: Closing logfile (/home/pi/motion/motion.log).
Another idea for solving that problem ?
Thanks in advance.

chris-mb-ca
Posts: 2
Joined: Mon Jul 14, 2014 6:40 pm

Re: MMAL Motion - tuning & optimisations

Tue Jul 15, 2014 2:41 pm

I have found this:

The iPhone 4 supports three video formats:

H.264 video up to 720p, 30 frames per second, Main Profile level 3.1
Audio: AAC-LC up to 160 Kbps, 48kHz, stereo
File types: .m4v, .mp4, and .mov

MPEG-4 video, up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile
Audio: AAC-LC up to 160 Kbps per channel, 48kHz, stereo
File types: .m4v, .mp4, and .mov

Motion JPEG (M-JPEG) up to 35 Mbps, 1280 by 720 pixels, 30 frames per second
Audio: ulaw, PCM stereo
File type: .avi

What settings in the motion.cOnf would Meet these specifications?

Thanks again
Im running ios 5.1.1 on my iphone 4 (jb)

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Tue Jul 15, 2014 3:47 pm

raspiruedi wrote:@tgwaste
wow ... great service!
I appreciate Your script expecially made for my needs!

Stopping motion works but restarting doesn't work.
motion.log reads at the end:

Code: Select all

[Jul 15 15:07:18] motion_loop: Thread exiting
[Jul 15 15:07:18] stream_stop: Closing motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] stream_stop: Closed motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] MMAL Camera cleanup
[Jul 15 15:07:19] main: Threads finished
[Jul 15 15:07:19] httpd_run: motion-httpd - Finishing
[Jul 15 15:07:19] httpd_run: motion-httpd Closing
[Jul 15 15:07:19] motion_web_control: motion-httpd thread exit
[Jul 15 15:07:20] main: Motion terminating
[Jul 15 15:07:20] motion_remove_pid: Closing logfile (/home/pi/motion/motion.log).
Another idea for solving that problem ?
Thanks in advance.
are you running as root?

raspiruedi
Posts: 9
Joined: Fri Jul 11, 2014 2:19 pm

Re: MMAL Motion - tuning & optimisations

Wed Jul 16, 2014 10:48 am

tgwaste wrote:
raspiruedi wrote:@tgwaste
wow ... great service!
I appreciate Your script expecially made for my needs!

Stopping motion works but restarting doesn't work.
motion.log reads at the end:

Code: Select all

[Jul 15 15:07:18] motion_loop: Thread exiting
[Jul 15 15:07:18] stream_stop: Closing motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] stream_stop: Closed motion-stream listen socket & active motion-stream sockets
[Jul 15 15:07:18] MMAL Camera cleanup
[Jul 15 15:07:19] main: Threads finished
[Jul 15 15:07:19] httpd_run: motion-httpd - Finishing
[Jul 15 15:07:19] httpd_run: motion-httpd Closing
[Jul 15 15:07:19] motion_web_control: motion-httpd thread exit
[Jul 15 15:07:20] main: Motion terminating
[Jul 15 15:07:20] motion_remove_pid: Closing logfile (/home/pi/motion/motion.log).
Another idea for solving that problem ?
Thanks in advance.
are you running as root?
Yes, I start with 'sudo motion -c /home/pi/motion/motion.conf'

If starting without sudo the motion.log reads:
Problem creating directory /var/run/motion

My entry in crontab is:
28 12 * * * root /home/pi/motion/motionstart.sh restart > /dev/null
('28 12' for actual testing purpose; renamed motion.sh to motionstart.sh)

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Mon Jul 21, 2014 11:31 am

andies wrote:I was not able to get this second buffer thing working (my motion simply crashed).
Problem solved: http://www.raspberrypi.org/forums/viewt ... 00#p571300. Just sudo rpi-update.
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

King_of_Throne
Posts: 3
Joined: Sat Aug 09, 2014 6:41 am

Re: MMAL Motion - tuning & optimisations

Sun Aug 10, 2014 6:14 am

Interesting post. I installed it but strangely enough my secondary output is not saved to a file (no .avi file is created).
I double checked the conf file and ffmpeg_output_secondary_movies is on, also extpipe_secondary is on.

Then I tested other settings turning them on or off separately:
If I turn stream_secondary on then I can see the high resolution output in a browser.
If I turn ffmpeg_output_movies on then there is no problem (low resolution .avi files are being created).
If I turn output_pictures on then there also is no problem (low resolution .jpg files are being created).
If I turn output_secondary_pictures on then there is a problem (no .jpg files are being created at all).

So the primary output saves to files but the secondary doesn't for some reason (although it's there because I see it in the browser). If I look into the logfile there is a "event_newfile: File of type 8 saved to: /home/pi/01-20140809124539.avi" when I try the primary output but there isn't when I try the secondary.
Any ideas how to solve this?
Thanks!

Kaspars
Posts: 3
Joined: Sun Aug 10, 2014 6:28 pm

Re: MMAL Motion - tuning & optimisations

Sun Aug 10, 2014 6:33 pm

Hi,
i have a trouble of finding the correcrt motion mmal compiled files.

I found this link in git repo :
"Downloading: a temporary prebuilt binary for Raspberry Pi is available at this link:
https://www.dropbox.com/s/xdfcxm5hu71s9 ... mal.tar.gz"

But I don't see a secondary buffer option in the conf file..

Could someone point me to the right location?

thanks

Kaspars
Posts: 3
Joined: Sun Aug 10, 2014 6:28 pm

Re: MMAL Motion - tuning & optimisations

Sun Aug 10, 2014 8:49 pm

Hi,
same here - if "mmalcam_secondary_buffer_upscale " is not "0" it does not work at all

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Mon Aug 11, 2014 8:21 am

Did you "sudo apt-get update"?
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

Kaspars
Posts: 3
Joined: Sun Aug 10, 2014 6:28 pm

Re: MMAL Motion - tuning & optimisations

Mon Aug 11, 2014 8:17 pm

andies wrote:Did you "sudo apt-get update"?
Yes I did, is there a way to get the older ("working") version of raspberian or downgrade?

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Mon Aug 11, 2014 9:01 pm

@Kaspars - re your original query - the up-to-date Dropbox location is https://www.dropbox.com/s/jw5r1wss32tdi ... opt.tar.gz

Regarding your secondary buffer problem - that's an issue with the Pi firmware, for which there is a fix, but I'm not sure if it is in the latest update of Raspbian. If you go to page 3 of this thread and read through, you'll see what people have tried to work around the issue (including starting from an earlier version of Raspbian and only updating to the point of the problem).

@King_of_Throne - please would you post your motion config file and log somewhere public (or as an attachment) so I can take a look in more detail?

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Tue Aug 12, 2014 7:00 am

I made a mistake: not an update but an upgrade is necessary. In particular for me "sudo rpi-update" did the job. But now I learned that with other RPi's the problem was only created after installation of the new firmware (see http://www.raspberrypi.org/forums/viewt ... 36#p596436).
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

King_of_Throne
Posts: 3
Joined: Sat Aug 09, 2014 6:41 am

Re: MMAL Motion - tuning & optimisations

Wed Aug 13, 2014 6:12 am

dozencrows wrote:@King_of_Throne - please would you post your motion config file and log somewhere public (or as an attachment) so I can take a look in more detail?
@dozencrows - I attached both files (in a rar).

The first part of the log is with ffmpeg_output_movies on.
The second part of the log is with ffmpeg_output_secondary_movies on instead.
The third part of the log is with stream_secondary also on.

Thanks for your help.
Attachments
motion_log_conf.rar
(10.62 KiB) Downloaded 278 times

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Wed Aug 13, 2014 9:43 pm

Many thanks, @King_of_Throne!

A quick reply (haven't had time to test this yet)...

I can see from the config file that you have mmalcam_secondary_buffer_jpeg set to 50. This means that the secondary buffer contains a JPEG encoded frame from the Pi GPU which can't be passed to ffmpeg. What should happen in this case is that the primary (lower resolution) buffer is used for the AVI output instead.

Writing the JPEG secondary buffer to an image file via output_pictures/output_secondary_pictures should work alongside this; however the config file you've supplied has secondary picture output turned off.

Note that you need to turn on the main output settings (output_pictures, ffmpeg_output_movies) first - the corresponding "secondary" output options are then used to select whether the primary buffer or secondary buffer should be used.

I.e.. to get ffmpeg movies output from secondary buffer, you need ffmpeg_output_movies AND ffmpeg_output_secondary_movies to both be on.

King_of_Throne
Posts: 3
Joined: Sat Aug 09, 2014 6:41 am

Re: MMAL Motion - tuning & optimisations

Thu Aug 14, 2014 10:31 am

@dozencrows

If I turn both ffmpeg_output_movies AND ffmpeg_output_secondary_movies on it records the primary low resolution avi, not the secondary high resolution avi.

Output_secondary_pictures is turned off because I turned it on/off for problem-solving-purposes only. I would like to record high resolution avi, not jpg.

Does the mmalcam_secondary_buffer_jpeg influence the avi? How
much should I best put it at? This is what I get when I change it:

to 1: no apparent change is noticeable (low resolution avi is recorded, not high resolution avi).

to 0: I notice a change but not a good one. The images in the browser get very slow, the recorded avi has the same slow images although with a higher resolution. But I think it’s still the primary buffer (because there is text in the corner of the images).

to 100: not yet tested

Other question: Is there maybe something wrong with my extpipe setting (I’m
not sure what it does)?

Thanks a lot for your efforts!

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Thu Aug 14, 2014 11:38 am

What you describe is what I would expect. I hope my explanation below makes sense!

ffmpeg_output_movies on, ffmpeg_output_secondary_movies on, mmalcam_secondary_buffer_jpeg greater than zero - AVI encoding will use primary low resolution buffer, as secondary buffer contains image in JPEG format (ie already encoded for still image). No text overlay appears on this image, as it has already been encoded to JPEG by the Pi's GPU before it gets to the motion application.

ffmpeg_output_movies on, ffmpeg_output_secondary_movies on, mmalcam_secondary_buffer_jpeg zero - AVI encoding will use secondary high resolution buffer, as secondary buffer now contains a raw image that ffmpeg can encode for video. This raw image can also accept text overlays generated by motion, which is why you see a high-res video with text on it.

The second option will be slow, as motion uses the CPU to encode the video - the most processor intensive part of motion's operation.

You don't need to set up extpipe output - this is another output from motion, intended to pipe image data to an external application for further processing.

At the moment, this version of motion performs best when using secondary buffers for picture output only, with the mmalcam_secondary_buffer_jpeg option set to a non-zero value to make the GPU do the JPEG encoding - with ffmpeg output turned off completely.

As soon as you turn on ffmpeg output, motion starts making much heavier use of the CPU which really reduces the framerate on the Pi (as it's not a very powerful device). If you use the secondary buffer for ffmpeg output, the slowdown will be worse because the image is much higher resolution.

When you turn on the web stream output, as soon as you connect a browser, the framerate will also drop. The MJPEG format used by motion seems to swamp the network connection with data, and this is worse with the higher resolution images.

Return to “Camera board”