ProjectPhysX / FluidX3D

The fastest and most memory efficient lattice Boltzmann CFD software, running on all GPUs via OpenCL. Free for non-commercial use.
https://youtube.com/@ProjectPhysX
Other
3.81k stars 301 forks source link

Quick conversion of output images to video #33

Closed SLGY closed 1 year ago

SLGY commented 1 year ago

Hey I'm new to github so I don't know if these kind of snippets are helpful here or not? If there's a better forum or discord, etc then let me know and I can post there instead. 😊

If anyone wants a way to quickly create videos from the image sequences in between running simulations (so you don't have to cut and paste thousands of PNGs around into folders everywhere) I made this #.bin file and changed some of the FluidX3D code.

It's just a bin file that runs ffmpeg.exe and feeds it the images. Since people are running nvidia gear, the hardware encoding on the video is lightning fast.

The pipetovideo.bin file and ffmpeg.exe can sit anywhere but just needs to be edited to get the folder path correct. This is the arrangement I use:

Setup

The images in the folder have filenames like this (later in the post will show the code I changed to make that happen):

Files

And when pipetovideo.bin runs it creates .mpg videos in the same directory:

Setup with Video Output

My #.bin file needed to have the -pix_fmt yuv444p and high444 inputs there because I've set my png's to write with an alpha channel (rgba) so you may need to change some of the ffmpeg arguments to make it work. If people are interested I'll make it work for the shipped version of FluidX3D:

pipetovideo.bin: @echo off for /f "usebackq" %%x in (powershell "get-date -f yyyy-MM-dd-HH-mm-ss") do set timestamp=%%x if exist "ffmpeg.exe" ( ffmpeg.exe -framerate 60 -i images/%%6d.png -vcodec libx264 -pix_fmt yuv444p -preset slow -profile:v high444 -crf 20 %timestamp%.mp4 ) else ( @echo on echo ffmpeg.exe not found echo ------------------------------ echo Download a Windows build of FFmpeg from https://www.ffmpeg.org echo Place the ffmpeg.exe file in this folder and run this script again echo ------------------------------ pause ) cmd /k

Then, since Windows can't glob filenames and deal with the non-sequential numbering from FluidX3D, I also had to change the numbering for the output of PNGs in the code. My edits are very clunky because I know nothing much about programming, let alone C++

I added this into lbm at the start:

#include "lbm.hpp"
#include "info.hpp"
#include "graphics.hpp"
#include "units.hpp"

Units units; // for unit conversion
**int framecounter = 0; // initialise the frame counter for sequential output file naming**

And then changed default_filename around a bit:

string LBM::default_filename(const string& path, const string& name, const string& extension) {
    string time = "000000"+to_string(framecounter);
    time = substring(time, length(time) - 6u, 6u);
    framecounter++;
    return create_file_extension((path=="" ? get_exe_path()+"export/" : path)+time, extension);

I added another folder level in my setup.cpp to keep the important files separate from the flood of images: lbm.graphics.write_frame_png(get_exe_path() + "export/images/");

I think there was some other strings added to the filename like "images" and hyphens etc, and I scratched around to find and delete those, but can't remember where they were in the code.

Not posting this as a suggestion for something to add into the original program code, just putting this here as an idea for people who might need help to quickly make videos (or just preview) of their creations 🤷‍♂️

F-45 AoA Test

ProjectPhysX commented 1 year ago

Thanks for posting! I personally use ffmpeg in WSL, with ffmpeg -framerate 60 -pattern_type glob -i "export/image-*.png" -vcodec libx264 -pix_fmt yuv420p video.mp4