Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Autumn in Year 5 - @118.22
Two Stars Random | Recent Posts | Guild Recents
News: :seal: Thank you for today! :seal: Guild Events: Prepare for Halloween Reminder!

+  MelonLand Forum
|-+  Projects & Art
| |-+  ⛽︎ ∙ Technology & Archiving
| | |-+  cool ffmpeg tricks


« previous next »
Pages: [1] Print Embed
Author Topic: cool ffmpeg tricks  (Read 821 times)
purpleraindrip
Newbie ⚓︎
*
View Profile WWW


meow meow meow
⛺︎ My Room

Guild Memberships:
Artifacts:
Joined 2026!
« on: Summer in Year 5 @079 » Embed

I've recently found myself just converting files in command line using ffmpeg than having to open up adobe media encoder. It's just easier than opening up a whole ass app.

you can do it like this:
ffmpeg -i [input].[fileformat] [output].[fileformat]

of course it looks a lot different without the brackets. You also need to be in the right path

c:\users\person\downloads ffmpeg -i video.mov video2.mp4

like that. I also think it's really fun to compress video and audio cause the crunch is beautiful. so far I know how to mess with resolution, bitrate, and audio sample rate. it's like this

RESOLUTION
ffmpeg -i video.mp4 -vf scale=[width]:[height] video2.mp4

BITRATE
ffmpeg -i video.mp4 -b [desired data rate] video2.mp4
(you can also specify if you want to change the bitrate of the video or audio using -b:v or -b:a)
(also data rate is in thousands, example: 800k you gotta write it like that)

SAMPLE RATE
ffmpeg -i video.mp4 -ar [desired sample rate] video2.mp4
(while the bitrate will make the audio more compressed. for that real nice audio crunch you gotta bring down the sample rate. lowest is 8000 i think)

but yeah. those are all the ones that I know. But if anyone else knows any fun things to mess with using ffmpeg please reply! It's really fun making videos rly compressed and shitty and also it makes it easier to send shit to people on discord or apps with a file size limit. enjoy :D

Logged

:D :] ;P :L :I doot doot lalala~~~
Melooon
Hero Member ⚓︎
*****
View Profile WWWArt


So many stars!
⛺︎ My Room
SpaceHey: Friend Me!
StatusCafe: melon
iMood: Melonking
Itch.io: My Games
RSS: RSS

Guild Memberships:
Artifacts:
Wavy Shoulder-Length Hair with BangsTop HatBug!You're a Star!Flinstone VitaminI got robbed by Dan Q on Melonland!
« Reply #1 on: Summer in Year 5 @101 » Embed

Here's a script I've not messed with in years but I made this video with it:

The idea was to have a folder with 100s of videos in it, the script them splits all those videos into individual frames, then recombine them into a single tiled montage of all the videos playing at once. I also got carried away and started using ImageMagick to rotate each frame a little each time, so you get the tiles spinning while the montage plays!

So you put your videos in the VIDEOS folder names 1.mp4 2.mp4 etc, it'll dump the frames into FRAMES, then process them!  :ozwomp:

Code
#!/bin/bash

# Warning videos must have number names like 1.mp4 or it wont work
VIDEOS=./videos/*
FRAMES=./frames
SORTEDFRAMES=./sortedframes
MONTAGES=./montages
FRAMECOUNT=2000				#how many frames to process must be less than limit
FRAMERATE=24					#force framerate on videos
PLAYBACKSPEED=10

mkdir -p $FRAMES
mkdir -p $SORTEDFRAMES
mkdir -p $MONTAGES

for path in $VIDEOS
do
  file=$(basename ${path})
  ffmpeg -i "$path" -r $FRAMERATE -vframes $FRAMECOUNT $FRAMES/$file-%04d.png
done

for (( f=0 ; f<=$FRAMECOUNT ; f++ ))
do
	padded_count=$(printf "%04d" $f)
	for file in $FRAMES/*-$padded_count.png
	do
		mkdir -p $SORTEDFRAMES/$padded_count
		mv "$file" $SORTEDFRAMES/$padded_count/$(basename ${$file})
	done
done

ROTATEDEG=0

for (( i=1 ; i<=$FRAMECOUNT ; i++ ))
do
	padded_count=$(printf "%04d" $i)
  	echo $padded_count
	  # 20x10 96px fits EXACTLY 200 on a 1080p screen at around 3cm at 27" - 10x20 is vertical
  	magick montage "$SORTEDFRAMES/$padded_count/*" -geometry 96x96\!+0+0 -tile 10x20 -rotate $ROTATEDEG -background black -bordercolor black -mattecolor black $MONTAGES/$i.jpg
	#magick montage "$SORTEDFRAMES/$padded_count/*" -geometry 96x96\!+0+0 -tile 10x20 -background black -bordercolor black -mattecolor black $MONTAGES/$i.jpg

	# image rotation
	ROTATEDEG=$(($ROTATEDEG+1))
	if [ $ROTATEDEG -ge $((361)) ]
	then
		ROTATEDEG=0
	fi
done

convert -delay $((100/$PLAYBACKSPEED)) $MONTAGES/*.png -quality 100 final.mp4

Logged


everything lost will be recovered, when you drift into the arms of the undiscovered

Artifact Swap: alien yappin kittayHave a chillpillMove it, football head!black n orange yappin kittayRed Tulip100 coinsLurby<3eyntivemds pet fishGreen partyhatPlankMaya's Magatama
Seechain
Jr. Member ⚓︎
**
View Profile WWW


⛺︎ My Room
Itch.io: My Games

Artifacts:
Joined 2026!
« Reply #2 on: Summer in Year 5 @162 » Embed

ffmpeg is awesome. A while ago, I started converting reels to GIFs using this command.  :ok:

Code
ffmpeg -i reel.mp4 -vf "fps=12,scale=200:-1:flags=lanczos" reel.gif

https://i.imgur.com/eEFnAkH.gif

Logged

mrparker
Jr. Member ⚓︎
**
View Profile WWW

⛺︎ My Room

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!lighthouse keeper artemisUserJoined 2026!
« Reply #3 on: Summer in Year 5 @264 » Embed

ffmpeg is awesome. A while ago, I started converting reels to GIFs using this command.  :ok:

Code
ffmpeg -i reel.mp4 -vf "fps=12,scale=200:-1:flags=lanczos" reel.gif

https://i.imgur.com/eEFnAkH.gif

I just want to say - the arguments on this command specifically can lower the FPS, scale the result down to a smaller size than the original (or, unintentionally upscale the output), and then apply lanczos resampling - depending on the source, this can all result in a larger size than the original in some instances! Use no options by default - or use scaling only, and add more as you see fit if performance (size, encoding/format) is your goal.

To prevent upscaling entirely, you can modify the above to be similar to this:

Code
ffmpeg -i reel.mp4 -vf "scale='min(200,iw)':-1" reel.gif

The parameters in the scale flag are specifically width:height. Read more in the official documentation here: https://trac.ffmpeg.org/wiki/Scaling

This specifically keeps the aspect ratio when resizing the width to 200px - which is why for the height parameter in this example we set it to -1 - which tells ffmpeg to keep the original value for the height from the original file.

The iw/ih variables in the min() function specifically applies to the initial height/width of the source - read it as
Code
[i]nitial [w]idth / [i]nitial [h]eight

This basically tells ffmpeg "Hey - only if the original is wider/taller than 200px, shrink it to 200px".

Like for width, the same can be done for the height parameter, too:

Code
ffmpeg -i reel.mp4 -vf "scale=-1:'min(200,ih)'" reel.gif

Instead of iw for width, for height we use ih instead.

Keep in mind as well, mp4 in a lot of cases is smaller for web applications, which is why a lot of the time "gifs" are actually mp4 files - you can loop mp4 videos in raw HTML, and even save more space by removing audio tracks - so that way the file is smaller and downloads faster (also add the attribute to mute the video just in case the user agent relies on that for autoplay).

If you still want to use an image format - apng, webp, avif all allow additional frames - sometimes compresses better and looks better than gif - resembling more of the original file! Try a mix and match of all - and keep whichever is smaller!

Of course, nothing wrong with doing what you want - just letting people know other options exist, just in case it matters at all. The gif format provides an aesthetic that it's known for - but the other file formats with the right parameters can replicate it, too - so even then you don't need to limit yourself to gif.

« Last Edit: Summer in Year 5 @266 by mrparker » Logged
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« Reply #4 on: Summer in Year 5 @325 » Embed

If anybody's looking for a better media encoder but isn't yet willing to teach themselves ffmpeg, can I swoop in and recommend Handbrake. It's free, open source, multiplatform, and basically backs onto ffmpeg under the hood.

I find it useful because I've got a handful of common output scenarios I use so I can have them saved as Handbrake presets and just drag files in. I use ffmpeg as well, but sometimes you just want a lazy GUI approach, y'know?

Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif

Artifact Swap: PolyamorousI met Dan Q on Melonland!Joined 2025!
Seechain
Jr. Member ⚓︎
**
View Profile WWW


⛺︎ My Room
Itch.io: My Games

Artifacts:
Joined 2026!
« Reply #5 on: Summer in Year 5 @571 » Embed

I just want to say - the arguments on this command specifically can lower the FPS, scale the result down to a smaller size than the original (or, unintentionally upscale the output), and then apply lanczos resampling - depending on the source, this can all result in a larger size than the original in some instances! Use no options by default - or use scaling only, and add more as you see fit if performance (size, encoding/format) is your goal.

To prevent upscaling entirely, you can modify the above to be similar to this:

Code
ffmpeg -i reel.mp4 -vf "scale='min(200,iw)':-1" reel.gif



I love this, it's like sorcery, and every spell has room for improvement. :wizard:




Logged

mrparker
Jr. Member ⚓︎
**
View Profile WWW

⛺︎ My Room

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!lighthouse keeper artemisUserJoined 2026!
« Reply #6 on: Summer in Year 5 @841 » Embed

If anybody's looking for a better media encoder but isn't yet willing to teach themselves ffmpeg, can I swoop in and recommend Handbrake. It's free, open source, multiplatform, and basically backs onto ffmpeg under the hood.

I find it useful because I've got a handful of common output scenarios I use so I can have them saved as Handbrake presets and just drag files in. I use ffmpeg as well, but sometimes you just want a lazy GUI approach, y'know?


Oh yeah, this is also great if you have movies you want to re-encode for things like jellyfin (or plex - if you have money).

Logged
purpleraindrip
Newbie ⚓︎
*
View Profile WWW


meow meow meow
⛺︎ My Room

Guild Memberships:
Artifacts:
Joined 2026!
« Reply #7 on: Summer in Year 5 @027 » Embed

Here's a script I've not messed with in years but I made this video with it:


this is on ffmpeg???? that's fucking crazy!!!!!!! i clearly do not know the full extent of what it can do. that is bananas

Logged

:D :] ;P :L :I doot doot lalala~~~
mrparker
Jr. Member ⚓︎
**
View Profile WWW

⛺︎ My Room

Guild Memberships:
Artifacts:
Visited on Melon's 10th Anniversary!lighthouse keeper artemisUserJoined 2026!
« Reply #8 on: Summer in Year 5 @430 » Embed

this is on ffmpeg???? that's fucking crazy!!!!!!! i clearly do not know the full extent of what it can do. that is bananas

A lot of video editors actually just use FFmpeg underneath to encode (typically called "render") videos. KDEnlive uses it, and I believe the Vegas suite and Adobe Premiere too.

Logged
bartender
Newbie
*
View Profile WWW

⛺︎ My Room

Artifacts:
Joined 2026!
« Reply #9 on: Summer in Year 5 @273 » Embed

There's old codecs you can use as well. Such as Cinepak used for the Sega Saturn.

Some example command I looked up:
ffmpeg -i input_file -s 320x240 -ar 48000 -c:a pcm_s16le -c:v cinepak output_file.avi

There's others included in ffmpeg from the 90s as well. I wanted to find out how to get video to look like the intro FMVs for this game 'Winning Post'. Couldn't quite recreate it but it still gave me some nice results.

(1:05 - 3:00)

Logged
Rubbereon
Sr. Member ⚓︎
****
View Profile WWWArt


⛺︎ My Room
SpaceHey: Friend Me!
Matrix: Chat!
XMPP: Chat!

Guild Memberships:
Artifacts:
Caught by Rubbereon!Caught by Rubbereon!Caught by Rubbereon!Caught by Rubbereon!Caught by Rubbereon!Caught by Rubbereon!
« Reply #10 on: Summer in Year 5 @634 » Embed

I haven't really tested it much, but this script can apparently let you stream to Twitch, thing that is convenient on a weaker machine since obs requires some bit of processing power and ram.
Code
ffmpeg -f x11grab -video_size 1920x1080 -i :0.0 -f alsa -i default -c:v libx264 -preset ultrafast -c:a aac -f flv rtmp://your-streaming-url/your-stream-key
https://salivity.github.io/ffmpeg/article/how-to-use-ffmpeg-re-for-live-streaming
https://trac.ffmpeg.org/wiki/Capture/Desktop?source=post_page---------------------------

This one can repair corrupted video files, it does so by trimming out unreadable chunks out of the file (they call it muxing, I'm specialized in art preservation so I might have the wrong idea, but this sounds like removing whatever is corrupted and rebuilding the file with what's left). I used it once and was able to "recover" a mp4 video i.e. I could play it without any issues.
Code
ffmpeg -i "video-broken.mp4" -c copy "video-fixed.mp4"
https://www.junian.dev/tech/ffmpeg-fix-corrupted-video/

Logged

https://bettysgraphics.neocities.org/images/animals/cat%20695.gifFuzzy fwiendhttps://bettysgraphics.neocities.org/images/animals/cat%20696.gifhttps://bettysgraphics.neocities.org/images/animals/cat%20697.gif
https://rubbereon.nekoweb.org/img/penguin.gifLinux userhttps://rubbereon.nekoweb.org/img/penguin.gif
↓ All my web profiles are available on this website ↓
https://fursona.directory/@rubbereon :eyes:

Artifact Swap: <html>pearl yappin kittayGreen SpiffoJust CheeseTomato SliceCreampuff
xwindows
Full Member
***
View Profile


⛺︎ My Room

Artifacts:
Great Posts PacmanJoined 2024!
« Reply #11 on: Autumn in Year 5 @583 » Embed

My tips are not "cool" stuff, but rather mundane ones that --admit it-- you want to do them sometimes. Note that everything I wrote in ALL-CAPS in the command lines are supposed to be substituted with what you actually use...

This extracts audio (which is usually MPEG-4 AAC) from MPEG-4 video, without re-encoding it; lightning-fast with no quality loss:
Code
ffmpeg -i VIDEO.MP4 -vn -acodec copy AUDIO.AAC
  • `.M4A` file extension also works for this kind of audio.

This extracts audio from any arbitrary video, and re-encode it to 192 kbps MP3; very suitable when you downloaded YouTube still-video music (or still-video "podcast") abominations and would like to listen to them on MP3 player or MP3 CD-HiFi:
Code
ffmpeg -i VIDEO.MP4 -acodec libmp3lame -ab 192000 AUDIO.MP3

This creates a silent "test card" WebM VP8 video from a single still image for the duration of specified:
Code
ffmpeg -loop 1 -r 12 -i IMAGE.PNG -vcodec libvpx -t HH:MM:SS VIDEO.WEBM
  • If video quality was not satisfactory, add " -b VIDEO_BITS_PER_SEC " right after "libvpx" to specify video bit rate. (1)
  • If FFmpeg version was very old, you would have to use JPEG source image.

This creates a YouTube-style still-video music abomination in WebM VP8-Vorbis, from a still cover image and an audio file:
Code
ffmpeg -loop 1 -f image2 -r 12 -i IMAGE.PNG -i AUDIO.MP3 -vcodec libvpx -acodec libvorbis -shortest VIDEO.WEBM
  • I do not recommend doing this in general, unless you're trying to shoehorn music upload into a video-sharing site.
  • If you are trying to shoehorn music into video-sharing site, you might want to change "-acodec libvorbis" to "-acodec copy" to pass-through the audio losslessly. Because video sharing sites normally re-encode audio anyway, avoiding extra re-encoding would retain better audio quality.
  • If you must use proprietary MPEG-4 H.264-AAC, change "-vcodec libvpx" to "-vcodec libx264", and "-acodec libvorbis" to "-acodec aac", and change output file extension from ".WEBM" to ".MP4". I've heard that FFmpeg's AAC audio encoder sucks though, unless you have a very-very-latest version.
  • If video quality turned out unsatisfactory, add " -b VIDEO_BITS_PER_SEC " right before "-acodec" to specify video bit rate. (1)
  • If audio quality turned out unsatisfactory, add " -ab AUDIO_BITS_PER_SEC " right before the output filename to specify audio bit rate. (2) Note that this doesn't apply if you used `-acodec copy`.
  • If FFmpeg version was very old, you would have to use JPEG cover image.

This replaces sound in MPEG4 video with your own audio track, but with no re-encoding whatsoever; lightning-fast way to encode (actually, remux) your gag dub or music remix track unto original video, ready for upload:
Code
ffmpeg -i ORIGINAL.MP4 -i DUB.MP3 -map 0.0 -map 1.0 -vcodec copy -acodec copy DUBBED.MP4
  • If FFmpeg complained about incorrect (audio) codec parameter; omit the "-acodec copy" part. It will be a bit slower though, since this would now re-encode the audio part.



Also, it should be noted that while FFmpeg has capabilities to encode audio inside video file, that ability is not limited to video file output; you can use FFmpeg to encode audio files as well. It wouldn't allow you to adjust anything beyond bitrate, nor allow you to enter metadata, but it should be serviceable in many uses...

This transforms FFmpeg into a 192 kbps LAME MP3 encoder:
Code
ffmpeg -i ORIGINAL.WAV -ar 192000 ENCODED.MP3
This transforms FFmpeg into a 192 kbps Xiph.org Ogg Vorbis encoder:
Code
ffmpeg -i ORIGINAL.WAV -acodec libvorbis -ar 192000 ENCODED.OGG
  • DO NOT omit `-acodec libvorbis`. If you did, FFmpeg would sneakily encode in Opus codec instead, which many Hi-Fi as well as older players/browsers wouldn't play.

This transforms FFmpeg into a lossless Xiph.org FLAC encoder:
Code
ffmpeg -i ORIGINAL.WAV ENCODED.FLAC
Note: FFmpeg actually doesn't have code to do these audio encodings on its own; it enlisted help from software library of each corresponding brand of audio encoder I just noted.



(1) The number you specify in place of `VIDEO_BITS_PER_SEC` varies by pixel size of the video. For still-image 16:9 videos, I think ballpark bitrate value for 360p, 480p, and 720p resolution would be around 160000, 320000, and 640000 respectively. For 4:3 videos, multiply these values with 0.75.

(2) If in doubt, use value 128000 in place of `AUDIO_BITS_PER_SEC`.

« Last Edit: Autumn in Year 5 @678 by xwindows » Logged
purpleraindrip
Newbie ⚓︎
*
View Profile WWW


meow meow meow
⛺︎ My Room

Guild Memberships:
Artifacts:
Joined 2026!
« Reply #12 on: Autumn in Year 5 @825 » Embed

xwindows that is so crazy town banana pants ur blowing my mind rn. I see I have barely scratched the surface with what ffmpeg can do. Does anyone have any resources for like basic commands or r we sorta just doing that right here on the forum?

Logged

:D :] ;P :L :I doot doot lalala~~~
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF 2.0.19 | SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Forum Guide | Rules | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 Melon
Land
Editor Recent Edits Tenement Arcade Random Link Land → Forum Art Hub Chat Webring Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Newsletters Image Stream
Passport Sites Asatte Add your site?
Zap!
Minecraft: Online
Join: craft.melonking.net