spacetelescope / astronify

Astronomical data sonification.
https://astronify.readthedocs.io
71 stars 15 forks source link

Animation Function #47

Open Jenneh opened 3 years ago

Jenneh commented 3 years ago

Write Python function that generates mp4 animation by combining the .gif graph images and .wav sonification sound.

@ceb8 has notebook with code to get started with this

MoralCode commented 3 years ago

https://github.com/kkroening/ffmpeg-python may be helpful with this.

You mentioned that this was currently being done in a jupyter notebook so i'm not 100% sure if this would work off the top of my head, however i'd like to learn more about how this solution would be used within your workflow to see if there is a better, more user-friendly way this could be done that still fits your requirements (perhaps a web page where you drag-and drop the two files and it spits out the .mp4? or maybe an option built into astronify that automatically converts the output?).

running the outputs through ffmpeg may also be able to help solve the clipping issues in #10 (i.e. by using https://github.com/slhck/ffmpeg-normalize or something similar)

ceb8 commented 3 years ago

@MoralCode I suspect that ffmpeg is the solution, I haven't had time to explore it though. The goal with this is to be able to have a function within Astronify (so 100% python) that allows the user to make a vidio of a sonification with animated graph all at once. The Jupyter notebook I have, uses matplotlib to create a gif animation, and astronify to make a wav file of the sonification, so it's just that last step that needs to be worked out, to combine the two in python before it can go into atronify.

MoralCode commented 3 years ago

Here's a gist with what should be a working piece of sample code using this library to combine a gif and audio. it includes a pipenv python environment for the one dependency but thats not strictly necessary if you have the ffmpeg-python package installed. I have also included a random gif (found on giphy) and some matching Creative commons music that can be used to test this out.

In my testing ive noticed that if the audio is longer than the gif, the gif will just freeze at the last frame, im sure there are options to make the gif loop to fill the time or other things if some other behavior is desired

I can also rewrite this as a function that accepts the three filenames (two inputs and an output path) or if there's another way you prefer to have the value returned (i.e. through return values) i can poke around some more and see if that is supported by ffmpeg-python

ceb8 commented 3 years ago

@MoralCode Awesome! The last freeze frame is the behavior we want anyway, since the reason the audio is longer than the gif is that the animation ends at the onset of the last note, but the note has a duration, so it lasts a little longer.

Here is my notebook with an example of creating a sonification and gif that need to be coupled: https://nbviewer.jupyter.org/gist/ceb8/b02612675f2c5d01f3bb093e8d61e852