Recording games with FFmpeg

Here you can see youtube or twitch videos of Warzone 2100 games.
Post Reply
Forgon
Code contributor
Code contributor
Posts: 298
Joined: 07 Dec 2016, 22:23

Recording games with FFmpeg

Post by Forgon »

This tutorial is written for ArchLinux, but also useful for other operating systems.

0. To record Warzone2100's sound and listen to it at the same time, reconfigure Alsa.
Follow the tutorial at https://trac.ffmpeg.org/wiki/Capture/AL ... tputdevice and replace '<Your Output Device Name>' with the simple name of your card, e.g. 'Intel' or its number (typically '0') for ~/.asoundrc as suggested there. If unsure, check the output of `arecord -L`.

1. OpenAL does not default to Alsa. To change that, run `mkdir /etc/openal; echo drivers=alsa > alsoft.conf`.

2. When recording, mind the tradeoff between encoding quality and speed. Either framerate or bitrate should be given.

I chose 40fps with lossless encoding and a free container format:

`ffmpeg -video_size 1280x800 -framerate 40 -f x11grab -i $DISPLAY -f alsa -ac 2 -ar 44100 -i loopout -c:a flac -c:v libx264 -qp 0 -preset ultrafast game.mkv`

3. The process may be killed with 'SIGINT' or 'SIGTERM', e.g. when runinng `ffmpeg [...] & sleep 30; warzone2100 --debug=all`.

4. You can also limit the size of your output file, e.g. with `-fs 500M game.mkv`.

5. Trim and split the video as needed. To remove the first 30s and last 15s in a video of 25 minutes, run:

`ffmpeg -i game.mkv -ss 30 -to 24:45 -vcodec libx264 -qp 0 -acodec flac ingame.mkv`

Reencode! Do not copy input streams with `-c copy` or the equivalent `-acodec copy -vcodec copy` since this corrupts the output file.

6. Codec compression is useful for lossless codecs. I saved ~30% filesize with:

`ffmpeg -i game.mkv -vcodec libx264 -qp 0 -preset veryslow -acodec flac -compression_level 12 game_upload.mkv`

7. Consider process imaging to safeguard against power outage if FFmpeg takes a long time, e.g. with dmtcp or cryopid.

8. Check your files, e.g. with `ffprobe -hide_banner game.mkv`.

9. Extract frames and store them as images.

Take a screenshot in the middle of the 30th second 800px large:
`ffmpeg -i game.mkv -ss 30.5 -vframes 1 -vf scale=800:800 screenshot.png`

Create a list of thumbnails to chose from, e.g. for YouTube:
`ffmpeg -i game.mkv -vf 'select=gt(scene\,0.5)' -vsync vfr -vframes 3 thumbnail_%03d.png`

10. For further help read man pages, visit https://trac.ffmpeg.org or join Freenode's #ffmpeg channel.
User avatar
NoQ
Special
Special
Posts: 6226
Joined: 24 Dec 2009, 11:35
Location: /var/zone

Re: Recording games with FFmpeg

Post by NoQ »

Yay thanks.

Historical thread on the topic: viewtopic.php?f=3&t=6521&p=90944#p90944
Forgon
Code contributor
Code contributor
Posts: 298
Joined: 07 Dec 2016, 22:23

Re: Recording games with FFmpeg

Post by Forgon »

Forgon wrote: 1. OpenAL does not default to Alsa. To change that, run `mkdir /etc/openal; echo drivers=alsa > alsoft.conf`.
Correction: The command should obviously be `mkdir /etc/openal; cd /etc/openal; echo drivers=alsa > alsoft.conf`.
Forgon
Code contributor
Code contributor
Posts: 298
Joined: 07 Dec 2016, 22:23

Re: Recording games with FFmpeg

Post by Forgon »

Forgon wrote: 2. When recording, mind the tradeoff between encoding quality and speed. Either framerate or bitrate should be given.

I chose 40fps with lossless encoding and a free container format:

`ffmpeg -video_size 1280x800 -framerate 40 -f x11grab -i $DISPLAY -f alsa -ac 2 -ar 44100 -i loopout -c:a flac -c:v libx264 -qp 0 -preset ultrafast game.mkv`
Addition: To record Warzone2100 running in a window, determine the coordinates of its top left corner with a program like `xwininfo` and append them to $DISPLAY (example: '$DISPLAY+450,150').
Forgon wrote: 9. Extract frames and store them as images.

Take a screenshot in the middle of the 30th second 800px large:
`ffmpeg -i game.mkv -ss 30.5 -vframes 1 -vf scale=800:800 screenshot.png`

Create a list of thumbnails to chose from, e.g. for YouTube:
`ffmpeg -i game.mkv -vf 'select=gt(scene\,0.5)' -vsync vfr -vframes 3 thumbnail_%03d.png`
Correction:
9. Extract frames and store them as images.

Take a screenshot in the middle of the 30th second 800px large:
`ffmpeg -i game.mkv -ss 30.5 -frames:v 1 -vf scale=800:800 screenshot.png`

Create 10 thumbnails to chose from, e.g. for YouTube:
`ffmpeg -i game.mkv -vf select=thumbnail -vsync vfr -frames:v 10 thumbnail_%02d.png`
Post Reply