Opened 7 years ago

Closed 7 years ago

Last modified 7 months ago

#6336 closed defect (invalid)

No Way to Cleanly Terminate ffmpeg.exe while capturing screen

Reported by: dbareis Owned by:
Priority: normal Component: ffmpeg
Version: unspecified Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

This is on Windows 7/10 64 bit (but would at least be the same on all Windows computers), using this command line:

ffmpeg -y -f gdigrab -i desktop -r 10 -vcodec libx264 -pix_fmt yuv420p 10fps.mp4

You can manually terminate the program with CTRL+BREAK which is better than nothing but if you use the command (in a batch/automated process):

TaskKill /im ffmpeg.exe /F

then the result is an unusable and corrupted video.

The program already handles CTRL+BREAK, it should probably be simple to enhance this for the sigterm signal. But it would be nice to be able to cleanly do this with something like:

ffmpeg.exe /STOP 

or perhaps:

ffmpeg.exe /STOP [AliasDefinedOnExistingCommandLineToAllowSpecificTaskTermination]

Change History (21)

comment:1 by Carl Eugen Hoyos, 7 years ago

Component: undeterminedffmpeg
Keywords: ffmpeg terminate close automate batch removed
Type: enhancementdefect

What's wrong with pressing "q"?
Please provide the complete, uncut console output for the case when you get an "unusable and corrupted video" to make this a valid ticket.

comment:2 by dbareis, 7 years ago

The main point is I'm trying to AUTOMATE the task (no user interaction) with the "ffmpeg.exe" task or its window.

The console output follows:

ffmpeg\bin> ffmpeg -y -f gdigrab -i desktop -r 10 -vcodec libx264 -pix_fmt yuv420p  -metadata "title=AIR Recording on CN-DENNIS-MBOX by CN-DENNIS-MBOX\Dennis @ Fri 21/04/2017 17:25:45.89"  -metadata "genre=AIR TRIAGE CAPTURE" -metadata "composer=AIR TRIAGE PROGRAM v99.111 by DENNIS BAREIS" -metadata "album=AIR TRIAGE PROGRAM v99.111 by DENNIS BAREIS" -metadata "author=CN-DENNIS-MBOX\Dennis on CN-DENNIS-MBOX" -metadata "album_artist=CN-DENNIS-MBOX\Dennis on CN-DENNIS-MBOX" -metadata "comment=User comment that was entered into a dialog"  10fps.mp4
ffmpeg version N-85469-gf1d80bc Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 6.3.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
  libavutil      55. 61.100 / 55. 61.100
  libavcodec     57. 92.100 / 57. 92.100
  libavformat    57. 72.100 / 57. 72.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 84.101 /  6. 84.101
  libswscale      4.  7.100 /  4.  7.100
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
[gdigrab @ 037c0f80] Capturing whole desktop as 3840x1080x32 at (-1920,0)
[gdigrab @ 037c0f80] Stream #0: not enough frames to estimate rate; consider increasing probesize
Input #0, gdigrab, from 'desktop':
  Duration: N/A, start: 1492759547.171207, bitrate: 3977347 kb/s
    Stream #0:0: Video: bmp, bgra, 3840x1080, 3977347 kb/s, 29.97 fps, 1000k tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (bmp (native) -> h264 (libx264))
Press [q] to stop, [?] for help
[libx264 @ 03671220] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 03671220] profile High, level 5.0
[libx264 @ 03671220] 264 - core 148 r2762 90a61ec - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=10 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '10fps.mp4':
  Metadata:
    title           : AIR Recording on CN-DENNIS-MBOX by CN-DENNIS-MBOX\Dennis @ Fri 21/04/2017 17:25:45.89
    genre           : yyy
    composer        : xxx
    album           : zzz
    author          : aaa
    album_artist    : bbb
    comment         : ccc
    encoder         : Lavf57.72.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 3840x1080, q=-1--1, 10 fps, 10240 tbn, 10 tbc
    Metadata:
      encoder         : Lavc57.92.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
frame=    8 fps=0.0 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A speed=   frame=   14 fps= 13 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   19 fps= 12 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   25 fps= 11 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   30 fps= 11 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   35 fps= 11 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   41 fps= 11 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 droframe=   46 fps= 11 q=0.0 size=       0kB time=00:00:00.00 bitrate=N/A dup=0 drop=18 speed=   0x
ffmpeg\bin>

As you'd expect nothing to see...

comment:3 by Hendrik, 7 years ago

Sending a "q" character to your running ffmpeg through some script is definitely not impossible, so you could investigate that solution.

Otherwise, encoding into a streaming container like mpegts would also allow the process to be hard-interrupted without leaving a broken file. mp4 does not work like that, as you found out the hard way.

comment:4 by Carl Eugen Hoyos, 7 years ago

I wonder why TaskKill doesn't work: Does it possibly never work for programs running in a cmd window?

comment:5 by Hendrik, 7 years ago

TaskKill works, but he forced a forcefull shutdown (like a SIGKILL), so it doesn't "finish" the file and write the MP4 header (which is at the end of the file for MP4, hence using somehting like mpegts would likely yield a usable file).

Last edited 7 years ago by Hendrik (previous) (diff)

comment:6 by Carl Eugen Hoyos, 7 years ago

Why doesn't TaskKill /im ffmpeg.exe work?

comment:7 by Cigaes, 7 years ago

Why do you not send the correct signal to ffmpeg? The point of having different signals is to cause different kinds of termination. If all signals are handled the same way, it serves no purpose.

comment:8 by dbareis, 7 years ago

TaskKill returned a message saying I needed "/F" and after waiting a while, the processed wasn't terminating. I know sending "Q" to the processes is not impossible, just not easy or reliable and a major hack to workaround a deficiency in the app which should be corrected.

comment:9 by dbareis, 7 years ago

ffmpeg\bin> taskkill /im ffmpeg.exe
ERROR: The process "ffmpeg.exe" with PID 10808 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

If my command line can be altered to not corrupt the file, I can live with that, what is the new command line? I tried adding "-f mpegts" from a google but that made no difference and neither did replacing "gdigrab".

comment:10 by Cigaes, 7 years ago

On Unix systems, that would be kill -INT, but kill would work just as well. This is an OS question rather than a ffmpeg question.

comment:11 by Hendrik, 7 years ago

The non-forced method of TaskKill doesn't work for console applications, it only works for UI applications which receive window messages.

The only way to cleanly close a running ffmpeg instance on Windows is to actually interact with it, send it "q", Ctrl+C or Ctrl+Break

comment:12 by Carl Eugen Hoyos, 7 years ago

Resolution: invalid
Status: newclosed

Thank you for the explanation!

comment:13 by dbareis, 7 years ago

I'm disappointed but not that surprised that the app won't be enhanced, this is definitely not an OS question as it's not possible to terminate the application cleanly on Windows and the fact that you can do so (relatively) cleanly on Unix is irrelevant.

As part of the bug report I was required to supply a command line, its a pity that the same requirement isn't required for responders. Eventually I may work out the "mpegts/mp4" comment meant...

comment:14 by Elon Musk, 7 years ago

What way would you prefer to close ffmpeg?

comment:15 by dbareis, 7 years ago

I'd like it to work as per my suggestion in the description of this enhancement request.

comment:16 by kt250, 6 years ago

Why was this marked as invalid? I would also like to have a way to cleanly terminate the ffmpeg process from Windows Command line. I'm sure this would benefit many ffmpeg users.

comment:17 by Stéphane Gourichon, 6 years ago

# A working solution

Using taskkill without the /F option, works for mewith FFMpeg 4.0.1 64bit on Windows 10 when ffmpeg creates mp4 files:

taskkill /im ffmpeg.exe

Resulting MP4 files can be read with usual tools.

# Opinion

Also, I understand that FFMpeg developers consider this outside of the scope of FFMpeg but is rather a matter of using OS provided interprocess communication tools.

comment:18 by dbareis, 6 years ago

Killing solutions exist, but don't expect to see all your "captured" data or even in some cases any depending on your luck and options chosen.

comment:19 by patachou, 6 years ago

In the case of Q key is pressed, could you set %errorlevel% <> 0 ?
Easier to catch that %errorlevel% in a batch file (WinOS).

comment:20 by Rattley Cooper, 4 years ago

taskkill doesn't work with all versions of ffmpeg so I created a wrapper program that creates a window handle, can launch ffmpeg, catch the WM_CLOSE event sent by the taskkill command, and then send "q" to the ffmpeg processe's stdin to shut it down gracefully. So it is now possible to handle a taskkill gracefully on windows with any version. It never shows the window handle but it can still handle the WM_CLOSE event.

Here is a link to the repository. https://github.com/Wykleph/ffmpeg_wrapper - I am not sure if this qualifies as "fixed".

Last edited 4 years ago by Rattley Cooper (previous) (diff)

comment:21 by Anh Trinh, 7 months ago

I encountered the same issue as well.

My usecase is: ffmpeg process receive packets sent from browser MediaRecorder via websocket, recording as HLS .ts 6s segments.

However, when I stop MediaRecorder, and then send and SIGTERM to ffmpeg, it just hang there. Then I wait about couple of seconds and send SIGKILL, but then the last segments failed to be written.

Would be nice if this can be fixed.

Note: See TracTickets for help on using tickets.