AllskyTeam / allsky

A Raspberry Pi operated Wireless Allsky Camera
MIT License
1.12k stars 174 forks source link

[ENHANCEMENT] Add music to generated videos #3547

Open flewid opened 3 months ago

flewid commented 3 months ago

I thought it'd be cool to have music over the generated videos to make them a bit more fun to watch, so I played around with this and came up with the following method.

I'm sure this isn't right, so I did not create a PR or anything, I think we'd in reality want something like this;

Anyway, here's what's working for me;

First, copy your mp3 to your allsky box as 'music.mp3' then, ssh to your allsky box and do the following

cd ~/allsky
mkdir music 
mv ~/music.mp3 ~/allsky/music/

then edit the timelapse script

nano ~/allsky/scripts/timelapse.sh

at line 249, which looks like -i "${SEQUENCE_DIR}/%04d.${EXTENSION}" \

add this following it on the next line -i "${ALLSKY_HOME}/music/music.mp3" -af "afade=in:st=0:d=5,afade=out:st=57:d=5" -t 63 \

now just wait until the next morning and check out your video, with sound!

What this does is;

EricClaeys commented 3 months ago

@flewid, you should be able to add that line to the KEOGRAM_EXTRA_PARAMETERS setting so you don't have to change the timelapse.sh file.

If I understand this correctly, the user needs to provide 3 pieces of info:

  1. Name of music file.
  2. Length of fade in time in seconds or 0 to have no fade in. This number must be less than the length of the video and ideally less than half the video length.
  3. Length of fade out. Same comments as above.

The 57 and 63 in your example could be automatically determined based on the length of the video. Do you know what happens if the audio is shorter than the video? Does it loop?

We probably won't add this to the WebUI but could add it to the documentation, possibly as a FAQ item, and include the code to determine the video length.

Do you know how to add audio AFTER the video is created? If that's possible then a module could be written to do that. We are eventually going to turn all the post capture work (e.g., stretching, cropping, timelapse, keogram, etc into modules.

flewid commented 3 months ago

@EricClaeys Thanks for checking it out!

I do not think adding to the keogram would work? I experimented a bunch and it seems the 'order matters' in terms of the ffmpeg command. The command must be BEFORE the video generation stuff on that line in timelapse.sh. But I could be wrong?

For your questions

1) yes this sounds right, however, I now have thought about this some more, and it might be cool to pick from a 'pool' of mp3's so each video gets a random song attached to it, if the directory only has one mp3, they all get the same, if it has 5 then it randomly chooses one to use.

2) yes, this sounds right to me. I think most people would want music the whole way through, but, you're right, some people might want to start it 'at night time only' or even have different music for day and night (but now i'm just getting crazy).

3) correct

4) if the audio is shorter than the video, there is a looping parameter for ffmpeg, but the way this is setup it will just stop. I have not tried the looping as the MP3's i'm using are minutes long.

Makes sense about a FAQ entry for sure!

Yes, you can add audio AFTER the video's been created, however, this would be processing twice so I figured this was kind of redundant, and also causes more strain on the box but I see where you're going with the module inclusion for sure.

I'm not much of a programmer, but I can help with whatever's needed on this for testing etc.

Thanks!

EricClaeys commented 3 months ago

@flewid, you are probably right that the music parameters need to go earlier on the command line than ${TIMELAPSE_EXTRA_PARAMETERS} which is at the end.

Thinking out loud... If there was a new setting for Music Parameters Script that pointed to a user-supplied script that output the parameters, that script could be run prior to ffmpeg, and its output put on the ffmpeg command line in the correct place. Some information would be passed to the script such as the length of the video in seconds and whether or not it was a mini or daily timelapse. The script could output the same parameters every time, or use different music files everytime, or anything the user wanted.

Do you think that would work?

flewid commented 3 months ago

Yep, I tried various spots in the command and definitely has to be either the first command, or the second position where I have it.

I like that idea for a checkbox + form field for 'user script' for this - that could be used for many other things as well too eh?