Adding Audio to MotionEye Recordings

Okay, so everyone - I’m a novice at best… I’ll do my best to fix issues that are found (PR always welcome!), but this is my first time trying to maintain anything… even if it is just a single script.

Here is the first (what I hope is) working draft of a script to add audio to MotionEye recordings. It has only been tested on continuous recording. It has not been tested on PI either… so feel free to give feedback. Basic instructions are in the repo.

I think this is something that has been highly requested for MotionEye, and although I know its a bit of a hack… if it works, it works :slight_smile: .

Cheers!
DeadEnd

1 Like

Great work! A request for the future might be to make it, so it works with a default docker version of motioneye. Don’t know if that would be possible.

1 Like

thank you for this. So i’m running motioneye on docker.
saved the script to the config file which is mapped to /etc/motioneye
added the options.
It’s saving on motion normally
downloaded the mp4, but no sound.
no error in the docker logs.
anything else to check?
thank you again

@frits1980 I am running motioneye in Docker and using this script. It should work.
I’m not sure if there is something else I am missing though…

@juan11perez check the folder where the MP4’s are at. You should see one WAV file of the current recording being generated. If you don’t see that, then the audio recording didn’t start.

Some things to check… the script expects there to be camera-#.conf in the config folder (default is /etc/motioneye). I think older systems might have used thread-#.conf instead… if you have thread files instead of camera files that would be a problem.

Otherwise, check your camera-#.conf file and make sure these variables are there:

netcam_url
netcam_userpass

and of course this all assumes your camera has audio on the stream :slight_smile: .
After all that, you might need to use ffprobe to check the stream and see what it contains…
That should give you a good start though.

DeadEnd

thank you very much for your response and recommendations.
camera labeling is correct and variables are in the conf files. all seems ok there
Looking at the script i thought the sound file was created in the tmp dir and then merged with the video file and subsequently deleted when the video recording stops.
I looked in the docker tmp file and I did find these files, which I thought were deleted after merging.
so I downloaded one of this files “motion-audio-ffmpeg-camera-4” and it seems to be a txt file.
I opened it and all it had was this:
1414

Yes, that is a temp file with PID of the audio recording.
The audio file should be in the same folder as the MP4 video files.
If you are using the default save locations I believe it is /var/lib/motioneye/Camera#/date/.....
And yes, it generates the sound file, and then when video stops, it merges the two and then deletes the audio file.

the storage location is correct. it is mapped in the docker to /var/lib/motioneye/Camera4/2020-05-23 in this example.
the sound file is not there as the recording already stopped.
are you running in docker? just thinking i may not have all the dependencies the script needs

Yes running in docker.
Since you saw the PID file, I would say the script is running.
Sounds like the audio stream is not being found possibly.
You might have to attached to the docker container and test some of the commands to see where it is failing.

ffmpeg -y -i "camera-stream-path" -c:a copy test.wav

See if you can run that and if a wav file is generated.

DeadEnd

sorry, what goes in camera-stream-path.
would it be something like this
ffmpeg -y -i /var/lib/motioneye/Camera4/2020-05-23 -c:a copy test.wa

the url of your camera stream.
This is camera specifc but is something like (this is my Dahua Camera):
rtsp://user:[email protected]:554/cam/realmonitor?channel=1&subtype=00

yes it works, i’ll test the other commands and see what’s the issue.
thank you again

What are the video lengths you are testing?
If they are very short (10-15 seconds) that hasn’t been tested…
60 second segments I did test - also are you doing continuous or on motion?

good day, so i found a problem, maybe specific to me:
had to modify

stream="$(grep ${netcam} ${motion_camera_conf} | sed -e 's/$'{netcam}'.//')"
with
stream="$(grep ${netcam} ${motion_camera_conf} | sed -e "s/"${netcam}".//")"

the netcam var needs to be in in double quotes; this now creates the sound file in the corresponding dir. and it sounds fine.

next problem is that the files mp4 and wav are not merging or at least there’s no sound in the final file.

looking at the stop part of the script I understand it must kill and remove the file in the tmp dir ?
but it’s not doing that. The tmp file stays until it’s superseded by the next one

so i presume the recording goes on and cant merge?

Okay, looking into the single vs double quotes… need to read up on shell script to understand if its different than I thought…

For the second issue - I am thinking possible the second part of the script is not being called… Reviewing the instructions, I had a mistake…
Make sure your “run a command” has the correct text - I accidentally added on_movie_end which is actually what the box is for… so it was a duplicate and probably caused it to fail to be called. Repo instructions have been updated - here is the correct line:

/etc/motioneye/motioneye-audio.sh stop %t %f

If you have:

on_movie_end /etc/motioneye/motioneye-audio.sh stop %t %f

Then it probably isn’t working - update to the first and that should fix it… I hope :slight_smile: .

Cheers!
DeadEnd

That was it. It’s working well now. Recording on motion and creating the video/audio file.

Again thank you for sharing this and for taking the time to help me get it running.

Great to hear!
I am reading up on the sed command… I think the way it is written in the script it not ideal. I’ll do some testing (I think there is an extra set of quotes that can/should be removed). I’ll do testing this week and get it straightened out.

Cheers!
DeadEnd

Thanks for this script! Althought I could’t make it work till yet.

I’m running ubuntu 20.04 and MotionEye is running under docker:

Hereby my docker-compose.yaml

version: "3.6"
services:

##### MotionEye  ####

  motioneye:
    container_name: motioneye
    image: ccrisan/motioneye:master-amd64
    restart: unless-stopped
    volumes:
      - /etc/localtime:/etc/localtime
     # - /etc/motioneye:/etc/motioneye
     # - /var/lib/motioneye:/var/lib/motioneye
      - /home/luiscarlos/dockerdisco2/volumes/MotionEye/etc:/etc/motioneye
      - /home/luiscarlos/dockerdisco2/volumes/MotionEye/lib:/var/lib/motioneye
    ports:
      - 8765:8765
      - 18081:8081

Under the following path

/home/luiscarlos/dockerdisco2/volumes/MotionEye/etc

I have the .conf files and the script with the properly +x permission:

[email protected]:~/dockerdisco2/volumes/MotionEye/etc$ ls
camera-1.conf  motion.conf  motioneye-audio.sh  motioneye.conf  tasks.pickle

Hereby my camera-1.conf:

threshold_maximum 0
stream_quality 100
threshold 107827
noise_level 31
smart_mask_speed 5
pre_capture 1
movie_codec mp4:h264_omx
noise_tune on
stream_maxrate 15
netcam_url rtsp://3.0.0.30:554/user=admin_password=tlJwpbo6_channel=1_stream=0.sdp?real_stream
stream_localhost off
text_changes on
movie_filename %Y-%m-%d/%H-%M-%S
movie_max_time 180
lightswitch_percent 25
movie_passthrough off
event_gap 30
auto_brightness off
stream_port 8181
rotate 0
stream_auth_method 0
threshold_tune on
framerate 10
emulate_motion off
movie_output on
picture_quality 85
snapshot_filename %Y-%m-%d/%H-%M-%S
despeckle_filter
snapshot_interval 0
minimum_motion_frames 20
stream_motion off
target_dir /var/lib/motioneye/Camera1
movie_output_motion off
post_capture 1
stream_authentication user:
on_picture_save /usr/local/lib/python2.7/dist-packages/motioneye/scripts/relayevent.sh "/etc/motioneye/motioneye.conf" picture_save %t %f; /home/luiscarlos/dockerdisco2/volumes/MotionEye/etc/motioneye-audio.sh stop %t %f
on_movie_end /usr/local/lib/python2.7/dist-packages/motioneye/scripts/relayevent.sh "/etc/motioneye/motioneye.conf" movie_end %t %f; /home/luiscarlos/dockerdisco2/volumes/MotionEye/etc/motioneye-audio.sh stop %t %f
text_left Pasillo planta
picture_output_motion off
picture_filename %Y-%m-%d/%H-%M-%S
text_scale 3
locate_motion_style redbox
locate_motion_mode on

I copied the “motioneye-audio.sh” as it is.

Under the path where recordings must be stored I see no wav audio file.

[email protected]:~/dockerdisco2/volumes/MotionEye/lib/Camera1/2020-06-04$ ls
10-23-05.mp4  10-23-05.mp4.thumb  10-23-09.jpg  10-26-17.mp4  10-26-17.mp4.thumb  10-26-35.jpg  12-16-44.mp4  12-16-44.mp4.thumb  12-16-59.jpg  15-05-56.jpg  15-05-56.mp4  15-05-56.mp4.thumb

What am I doing wrong?

Thanks a lot in advance.

Hmm… so just for clarity the audio file will only exist during the video recording… once the video recording is complete, the two will be merged, and the audio file will be removed.

Yesterday I also made some updates to the script. Depending on when you pulled it it will either be a WAV (before update) or an AAC (after update). This change was to prevent codec compatibility issues.

Lastly I added some clarification to the ReadMe file on directories as it can get confusion. This is what your problem is. Let me see if I can clearly explain:

This is the HOST directory accessible from your OS (ubuntu):
/home/luiscarlos/dockerdisco2/volumes/MotionEye/etc

This is the CONTAINER directory only accessible from within the MotionEye container:
/etc/motioneye

These in reality are the same directory, but accessed from two different systems (this is what the volume map allows). Your host OS (ubuntu) can only see /home/luiscarlos/... it cannot see /etc/motioneye and vice versa, MotionEye can see /etc/motioneye but cannot see /home/luiscarlos...

So you need to fix your start and stop calls to use /etc/motioneye since this is happening inside the container (Steps 3 and 4 in repo). For whatever reason I don’t see the on_movie_start in your camera-1.conf either (step 3 from instructions). Make sure you have this entered.

Cheers!
DeadEnd

1 Like

Thank you for responding so quickly.
Now it really works, but it’s not recording like I expected.

I walk past the camera and count to 10. Then I turn around and count again from 10 to 20. So the length of the video is about 20 seconds.

Everything goes perfectly until I count to 4, from then on, the video stops and I can only hear myself counting to 20 without any image movement.

What can happen?

Thanks in advance.

With out the script going, do you have full video? Or does this still happen even “normally”.

Also what hardware are you on PC or PI?