ffmpeg is installed on your service. With one command it takes screenshots from a video, cuts out a short sample, puts a film into another file format or saves only the sound. This is handy before you upload a release to a tracker, or when a player does not open a file.
The examples use a film holiday-film.mkv in the folder downloads. Go there first with cd ~/downloads and use the name of your own file. Put a name with spaces between quotes: "My Film.mkv".
The commands in steps 1 to 4 only read and copy; they finish in seconds and put no load on the server. Converting a film to another codec or size (step 5) is different: it uses the processor at full power for a long time and slows down your apps and the other services on a shared server. Convert only when you must, one file at a time, and limit it as shown in step 5.
What You Need
- An SSH connection to your service: How to Connect to Your App Hosting Service via SSH.
1. Take Screenshots
One screenshot at a chosen moment. The time after -ss is hours:minutes:seconds:
ffmpeg -ss 00:10:00 -i holiday-film.mkv -frames:v 1 shot.png
A screenshot every 5 minutes of the whole film. The files are numbered shot-01.png, shot-02.png and so on; 300 is the number of seconds between two pictures:
ffmpeg -i holiday-film.mkv -vf fps=1/300 shot-%02d.png
Smaller files: use the ending .jpg instead of .png and add -q:v 2 for good quality:
ffmpeg -ss 00:10:00 -i holiday-film.mkv -frames:v 1 -q:v 2 shot.jpg
One picture with several small screenshots, here four of them in two rows, one every 5 minutes:
ffmpeg -i holiday-film.mkv -vf "fps=1/300,scale=480:-2,tile=2x2" -frames:v 1 sheet.jpg
ffmpeg prints many lines about the file while it works. Add -hide_banner -loglevel error directly after ffmpeg to see only errors. If a file with the same name exists, ffmpeg asks File 'shot.png' already exists. Overwrite? [y/N]; answer y or choose another name.
2. Cut Out a Sample
This copies 30 seconds, starting one hour into the film, to a new file. The picture and the sound are copied as they are, so it takes a second:
ffmpeg -ss 01:00:00 -i holiday-film.mkv -t 30 -c copy sample.mkv
A copy can only start at certain points of a video, so the sample begins a few seconds before the time you gave and is a little longer.
3. Put a Film Into Another File Format
Some players and TVs do not open .mkv files. This puts the same picture and sound into an .mp4 file without converting them:
ffmpeg -i holiday-film.mkv -c copy holiday-film.mp4
The new file has the picture and the sound of the film. Subtitles stored inside the .mkv file are not copied, so keep the original if you need them.
4. Save Only the Sound
As an MP3 file:
ffmpeg -i holiday-film.mkv -vn -b:a 192k audio.mp3
-vn leaves the picture out; 192k is the quality of the MP3.
5. Convert to a Smaller Picture, Only When You Must
This makes a copy with a height of 480 pixels, for a phone for example. The sound is copied:
nice -n 19 ffmpeg -i holiday-film.mkv -vf scale=-2:480 -c:v libx264 -crf 23 -threads 2 -c:a copy small.mkv
nice -n 19gives every other program priority over this one.-threads 2limits the conversion to two processor cores.-crf 23sets the quality: a lower number means better quality and a larger file.
A whole film takes a long time this way. Start it in tmux so that it continues when you close SSH: tmux: Keep a Command Running After You Close SSH.
Good to Know
- What is inside a video file (codecs, resolution, languages): See What Is Inside a Video File via SSH: mediainfo.
- Get the pictures to your computer: Download Your Finished Files in the Browser.
- Create the torrent for your upload: Create a Torrent via SSH With mktorrent.
- The new files count towards your disk space. Delete samples and screenshots you no longer need, for example with
rm shot-*.png sample.mkv.
