Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video recording not working if killing process or closing window #4102

Closed
2 tasks done
mattia-lecci opened this issue Jun 22, 2023 · 7 comments
Closed
2 tasks done

Video recording not working if killing process or closing window #4102

mattia-lecci opened this issue Jun 22, 2023 · 7 comments

Comments

@mattia-lecci
Copy link

  • I have read the FAQ.
  • I have searched in existing issues.

Environment

  • OS: Windows 10
  • scrcpy version: 2.0, 2.1
  • installation method: windows release
  • device model: OnePlus 9
  • Android version: 11

Describe the bug

  • Running scrcpy -r test.mp4 --verbosity=debug (using v2.1)
C:\Users\User\Downloads>scrcpy -r test.mp4 --verbosity=debug
scrcpy 2.1 <https://github.com/Genymobile/scrcpy>
DEBUG: ADB device found:
DEBUG:     -->   (usb)              215af537            device  LE2113
DEBUG: Device serial: 215af537
DEBUG: Using server (portable): C:\Program Files\scrcpy\scrcpy-server
C:\Program Files\scrcpy\scrcpy-server: 1 file pushed, 0 skipped. 78.2 MB/s (56955 bytes in 0.001s)
[server] INFO: Device: [OnePlus] OnePlus LE2113 (Android 11)
DEBUG: Server connected
DEBUG: Starting controller thread
INFO: Recording started to mp4 file: test.mp4
DEBUG: Starting receiver thread
[server] DEBUG: Using audio encoder: 'c2.android.opus.encoder'
[server] DEBUG: Using video encoder: 'c2.qti.avc.encoder'
INFO: Renderer: direct3d
DEBUG: Trilinear filtering disabled (not an OpenGL renderer
DEBUG: Using icon (portable): C:\Program Files\scrcpy\icon.png
DEBUG: Demuxer 'video': starting thread
DEBUG: Demuxer 'audio': starting thread
INFO: Texture: 1080x2400
WARN: [FFmpeg] track 1: codec frame size is not set
  • If pressing Ctrl+C (Windows), scrcpy terminates correctly and the recorded video works as expected
DEBUG: User requested to quit
DEBUG: quit...
DEBUG: Server disconnected
DEBUG: Server terminated
DEBUG: Demuxer 'audio': end of frames
DEBUG: Receiver stopped
INFO: Recording complete to mp4 file: test.mp4
DEBUG: Recorder thread ended
DEBUG: Demuxer 'video': end of frames
  • If killing the process (e.g., taskkill /IM scrcpy.exe /F or closing the cmd window), the video does not work properly. DIfferent behaviors
    • v2.0: the output video is 0 KB. Opening it with VLC yields an error: Your input can't be opened
    • v2.1: the output videos is >0 KB. Opening it with VLC yields no error, but the video has no duration (--:--) and does not play.
[server] DEBUG: Controller stopped
[server] DEBUG: Device message sender stopped
[server] DEBUG: Audio encoder stopped
[server] DEBUG: Screen streaming stopped

Having a graceful stop would be great!
I have no experience in C programming, but maybe there's a way of capturing the sigkill signal and treat it as a user input ctrl+c?

@rom1v
Copy link
Collaborator

rom1v commented Jun 22, 2023

I have no experience in C programming, but maybe there's a way of capturing the sigkill signal and treat it as a user input ctrl+c?

You can't catch SIGKILL. If you kill, there is no way to stop gracefully (and write the file footer).
Only with other signals like SIGINT (signaled on Ctrl+c).

Do not kill scrcpy violently…

@mattia-lecci
Copy link
Author

That makes sense, thank you for the explanation!

I might have found a work-around for those who need this on a Windows system:

  • Get the scrcpy process PID
    • To get the PID manually, use tasklist and look for scrcpy.exe. You can also filter the results as follows: tasklist /FI "IMAGENAME eq scrcpy.exe"
    • To get the PID programmatically you can use the Powershell command Get-Process. You can also filter the results using Get-Process scrcpy and get directly the PID using (Get-Process scrcpy).id
  • Using windows-kill, send a SIGINT signal to the desired PID as follows:
    • windows-kill -SIGINT PID
    • Automatically from the Powershell: windows-kill.exe -SIGINT (Get-Process scrcpy).id

I think it could be even easier in linux, where the kill command natively supports which type of signal you want to send to the process, e.g., kill -SIGINT PID, although i have not tested this.

@cktgh
Copy link

cktgh commented Oct 13, 2023

Thanks for the solution with windows-kill, it worked well 👍

Is there anyway to do the same without using an external executable (windows-kill)?

@cktgh
Copy link

cktgh commented Oct 13, 2023

In my case I'm launching scrcpy with powershell's Start-Job, that way recording is done in the background without blocking the terminal.

I have yet to find a way of stopping the recording job without corrupting the recording file;
windows-kill actually worked, but it is an extra dependency which I would like to avoid.

I would really appreciate anything, any clues would help, much thanks!

@rom1v
Copy link
Collaborator

rom1v commented Oct 13, 2023

Note that if you want to record for a fixed amount of time and stop automatically, there is a --time-limit parameter. #4052 #3752

@cktgh
Copy link

cktgh commented Oct 13, 2023

Thanks for the swift response!
Timer is surely is a nice feature for some cases, but I am attempting to create some kind of "non-blocking handler" in powershell, like:

$handle = scrcpy --async-record    # example non-blocking command
adb shell input tap x y    # send some device control
$recordResult = $handle.stop() 
Write-Host $recordResult.filePath    # etc...

For now I am using wrapper classes to implement something similar to the above

@cktgh
Copy link

cktgh commented Oct 13, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants