Skip to content

Instantly share code, notes, and snippets.

@samelie
Created February 18, 2015 09:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save samelie/32ecbdd99e07b9d8806f to your computer and use it in GitHub Desktop.
Save samelie/32ecbdd99e07b9d8806f to your computer and use it in GitHub Desktop.
ffmpeg last frame of video
#!/bin/bash
fn="$1"
of=`echo $1 | sed s/mp4/jpg/`
lf=`ffprobe -show_streams "$fn" 2> /dev/null | grep nb_frames | head -1 | cut -d \= -f 2`
rm -f "$of"
let "lf = $lf - 1"
ffmpeg -i $fn -vf select=\'eq\(n,$lf\) -vframes 1 $of
@PacoH
Copy link

PacoH commented Apr 13, 2017

Thanks for being the only one on the net to provide the best way to do this, which is to find the number of the last frame. Everyone else, including the genii at stack*, did this by using timestamps.

@noinfo
Copy link

noinfo commented Sep 25, 2017

Handy. Saved me a lot of work. Thanks.

@cxkoda
Copy link

cxkoda commented Nov 2, 2018

Thanks for sharing!

@mhwinkler
Copy link

Thanks for sharing! You could save future users some headache by adding a safety check before you run rm -f ...

Something like:

if [ "$fn" == "$of" ]; then echo "Only accepts MP4"; exit 1; fi

Otherwise, any input file that isn't an mp4 will be permanently deleted.

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