12

I record a few hours of video footage every day, which I set in queue for encoding with ffmpeg. I use the veryslow preset with x265, so a single hour of video may take up to 20 hours or more to encode.

Since my computer is working day and night, I'm wondering if ffmpeg has a way to pause and resume encoding? I'm using Terminal for OS X.

If so, can I also continue encoding even after shutting down Terminal and rebooting the computer? I think the Terminal session is restored when you log in again in OS X, at least the Terminal history is.

13

Suspend

A simple method is to suspend it with ctrl+z. Or you could get the PID with pgrep ffmpeg then use kill -s SIGSTOP <PID> to suspend.

Then resume with fg command or kill -s SIGCONT <PID>.

Unfortunately this will not survive a reboot.

VM

If you use a virtual machine, with something like VirtualBox, you could perform your encoding in a guest VM. It will allow you to "save the machine state" at any time which can survive a reboot. It can also allow you to assign max CPU resources for the guest so your host will always have available resources.

Possibly overkill, but if you're familiar with VMs or using one already it is a possible solution.

  • I love that VM solution, however, unless you're already using one it's a pretty big hassle. – DigiVision Media Feb 1 '17 at 17:56
  • The VirtualBox way didn't work for me. Tried just to save the state in mid-encoding, and tried to suspend by [Ctrl]+[z] and then save the state. After restoring the state and trying to resume, ffmpeg stops with an error (decode_band_types: Input buffer exhausted before END element found or Invalid NAL unit size and Error writing trailer / Protocol error). Maybe it's related to the jump in time, I don't know. – Jānis Elmeris Apr 1 at 6:39
  • @JānisElmeris I can't seem to duplicate that issue. I tried it on Ubuntu 18.04 VM and it worked. – llogan Apr 1 at 17:07
  • OK, I'm probably not lucky enough. :) Tried with a fresh Linux Mint 19.1 (based on Ubuntu 18.04) VirtualBox VM. – Jānis Elmeris Apr 2 at 11:34
  • my ffmpeg has many threds and as many PID. Do I have to SIGSTOP and SIGCONT each of them? is there a timely constraint? – wuppi 2 days ago
9

It didn't seem possible as of Sep 30 2015. I would suggest segmenting the source file, encoding the segments and then stitching the resultant files.

This isn't a true pause/resume facility but the piecemeal division will allow you to have a break from encoding.

A rough overview of the commands to issue:

Break the fullfile into parts

ffmpeg -i recording.mp4 -c copy -flags +global_header -segment_time n -f segment file%03d.mp4

This should create file000.mp4, file001.mp4 and so on. Because of how the segmenter works - it only cuts at I-frames - these files won't be exactly n seconds long and the video part may not play properly, but the data's all there and you can convert each of the parts e.g.

ffmpeg -i file001.mp4 -{filter/encoding parameters} -fflags +genpts file001-new.mp4

Rejoin the encoded parts

Create a text file and put in the name of each rendered file like this

file 'file000-new.mp4'
file 'file001-new.mp4'
.... 
file 'filelast-new.mp4'

And run

ffmpeg -f concat -i textfile -c copy -fflags +genpts recording-encoded.mp4
  • Having only 6GB memory, and an estimated time of 12h to enconde from x264 to x265 and having no no-break (desktop), I cant use the VM trick, but this sounds great! I just expect the rejoining step is fast, but as there will have no encoding on it, I think it will be. Have to try it, thx vm! – Aquarius Power Dec 22 '18 at 6:04
  • it creates a warning when joining: "Codec for stream 0 does not use global headers but container format requires global headers" but the final result is a usable video, I saw no problems at all – Aquarius Power Dec 24 '18 at 0:16
9

On Windows, pressing the "Pause/Break" key (the top-right most key) will pause it. Enter will resume.

If it doesn't work, click on the command prompt window to give it focus.

  • This is the easiest and best answer for Windows (verified on Windows 10). – lidsinker Aug 6 '18 at 13:14
  • 1
    This frees up the cpu completely but the ram is still bottlenecked. In my case I have 8gb ram and it's using almost all of it still while the operation is paused. Any fix for that? Can I transfer this to the hdd or something? I don't mind a bit of a windup for things to get going when i unpause. – kite Jan 4 at 18:45
4

Suspending the Thread works on Windows too, not with crtl + z, but in the resource manager, you can also resume it there.

4

I sugest instead of pausing, you give the encoding process the very least system priority (aka "renice"), so you can work daily comfortably and the encoding will take place in background with the unused system ressources. So your encoding will take place seamlessy 24/7 without interruptions, without conflicting with day work

as AFAIK OSX lacks the pidof command, you need to know the ffmpeg process number (PID)

then you can renice the process to idle background with

sudo renice 20 yourPID

if needed you can also renice the process to more agressive foreground priority

sudo renice -10 yourPID
  • Hi! Good suggestion. Will have to try it. – Winterflags Jan 29 '17 at 12:01
1

(This for Linux)

As you all probably know, pressing 'q' ends the recording file.

ffplay has the 'p'-possibility. Why doesn't ffmpeg have the same thing? Maybe because it can lead to desync between video and audio?...

I just ffmpeg-encode to x264-aac-mkv with crf (23) and always with the same video conditions, 'quit' (q) the recordings and join all the segments in a specific dir, where this script resides, to 1 file in streamcopy mode with this bash-script:

#!/bin/bash

# FFMPEG MERGE

# File path must be absolutely absolute...

ffmpeg -f concat -safe 0 -i <(printf "file '%s'\n" /home/me/Videos/FFmpeg/FFmpeg_Merge/*.mkv) -c copy merged.mkv

exit 

After that I check the resulting merged video and paste it to another dir. The segments are removed from the merge dir.

But, a hasslefree 'Pause & Resume' button would be welcome indeed;)

0

This is just an idea, but if you have some budget, it'd be a good idea to upgrade either the processor (to 8th gen Intel) or even better, the video card (to NVIDIA GTX 1050 or above) so you can encode h.265/HEVC with hardware acceleration instead of CPU-intensive x265. Your video encoding will be finished in much less than 20 hours, saving you electricity costs too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.