Stop the recording after some period of time

Use the -t option. From man ffmpeg:

-t duration (input/output)

When used as an input option (before -i), limit the duration of data read from the input file.

When used as an output option (before an output filename), stop writing the output after its duration reaches duration.

duration must be a time duration specification, see the Time duration section in the ffmpeg-utils(1) manual.

-to and -t are mutually exclusive and -t has priority.

Example that records for one hour:

ffmpeg -f v4l2 -i /dev/video0 -t 01:00:00 output

Or in seconds:

ffmpeg -f v4l2 -i /dev/video0 -t 3600 output

If you're running ffmpeg or avconv from the command line, you could use the timeout command:

timeout 3600 avconv -f video4linux2 -r 25 -i /dev/video0  -vcodec mpeg4 -y out.mp4