Skip to content

Instantly share code, notes, and snippets.

@logiclrd
Last active October 11, 2024 16:44
Show Gist options
  • Save logiclrd/287140934c12bed1fd4be75e8624c118 to your computer and use it in GitHub Desktop.
Save logiclrd/287140934c12bed1fd4be75e8624c118 to your computer and use it in GitHub Desktop.
FFmpeg: Ultimate film grain
ffmpeg -i "HD Splice 1080p No Grain.mkv" -i "HD Splice 1080p No Grain.mkv" -filter_complex "
color=black:d=3006.57:s=3840x2160:r=24000/1001,
geq=lum_expr=random(1)*256:cb=128:cr=128,
deflate=threshold0=15,
dilation=threshold0=10,
eq=contrast=3,
scale=1920x1080 [n];
[0] eq=saturation=0,geq=lum='0.15*(182-abs(75-lum(X,Y)))':cb=128:cr=128 [o];
[n][o] blend=c0_mode=multiply,negate [a];
color=c=black:d=3006.57:s=1920x1080:r=24000/1001 [b];
[1][a] alphamerge [c];
[b][c] overlay,ass=Subs.ass"
-c:a copy -c:v libx264 -tune grain -preset veryslow -crf 12 -y Output-1080p-Grain.mkv
@kocoten1992
Copy link

Thanks for the detailed answer, really appreciate that!

I find an alternative art: add noise at run time

For example:
using VLC player, we could Tools->Effects and Filters->Video Effects->Film Grain
For browsers: programmatically add noise via canvas api

(I'm working at a streaming firm - increasing filesize a lot is very out of question 😃).

One drawback about this approach is the band effect when encode video in 8-bit in low bitrate:
The feeling of banding effect (low quality) + film noise (high quality) feel really weird (like someone haven't take a shower in week put on a fragrant) - it could be mitigate if using 10-bit video encode - but currently not any browser support that, I really hope they do in the future.

banding

(sharing this if anyone looking for the same thing as me)

@logiclrd
Copy link
Author

logiclrd commented Sep 7, 2020

I suspect the only way to really achieve what you're looking for will be to add film grain only if the bitrate is high enough to eliminate macroblocking. But, perhaps judiciously applying a denoise filter before encoding could allow a lower bitrate to do a good job conveying smooth frames, and then that would be a suitable thing to add fake film grain to at playback time.

@HannesJo0139
Copy link

I am trying to beautify a tv series with this. But in every episode after 50 mins and 6 secs (movie length) ffmpeg throws "EOF timestamp not reliable" and from now on the grain is not changing anymore between the frames (like not temporal). Any idea what could cause this?

@RollingStar
Copy link

OP, what is your goal?

kocoten1992, this is similar to what AV1 is trying to do. I am unsure how well it works in real-world tests.

https://norkin.org/research/film_grain/index.html

@logiclrd
Copy link
Author

My goal is simulated film grain that looks more like the real thing than just per-pixel noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment