Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
113.29k stars 10.81k forks source link

[HELP] When scrcpy stops screen recording, it takes too long to release resources #5533

Open codematrixer opened 4 days ago

codematrixer commented 4 days ago

Environment

Describe the bug

This problem has been bothering me for a long time, please help me, thank you!!

Record using the python3 script,when stops screen recording, it takes too long to release resources Sometimes it works normally, but the probability of it failing is quite high.

Log as follows:

(venv) wai57@waideMacBook-Pro % python3 test.py
Recording started.
scrcpy 3.0 <https://github.com/Genymobile/scrcpy>
2024-11-25 20:10:35.026 scrcpy[97109:5003209] INFO: No video mirroring, SDK mouse disabled
INFO: ADB device found:
INFO:     -->   (usb)  2LQ0224126000898                device  ALN_AL00
/opt/homebrew/Cellar/scrcpy/3.0/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 63.4 MB/s (90224 bytes in 0.001s)
[server] INFO: Device: [HUAWEI] HUAWEI ALN-AL00 (Android 12)
INFO: Recording started to mp4 file: screen_recording.mp4
INFO: Renderer: metal
WARN: [FFmpeg] track 1: codec frame size is not set
Stopping screen recording...
Traceback (most recent call last):
  File "/Users/wai57/tmp/krunner-timer/test.py", line 38, in <module>
    main()
  File "/Users/wai57/tmp/krunner-timer/test.py", line 33, in main
    stop_recording(recording_process)
  File "/Users/wai57/tmp/krunner-timer/test.py", line 21, in stop_recording
    process.wait(timeout=60)
  File "/Users/wai57/.pyenv/versions/3.9.20/lib/python3.9/subprocess.py", line 1189, in wait
    return self._wait(timeout=timeout)
  File "/Users/wai57/.pyenv/versions/3.9.20/lib/python3.9/subprocess.py", line 1925, in _wait
    raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command '['scrcpy', '--no-playback', '--record', 'screen_recording.mp4']' timed out after 60 seconds

python3 script

import subprocess
import time
import signal

def record_screen(output_file):
    scrcpy_command = [
        'scrcpy',
        '--no-playback',
        '--record', output_file
    ]

    process = subprocess.Popen(scrcpy_command)
    return process

def stop_recording(process):
    t1 = time.time()
    print("Stopping screen recording...")
    process.send_signal(signal.SIGINT)
    process.wait(timeout=60)
    print(f"Screen recording stopped. cost time: ({time.time() - t1})")

def main():
    output_file = "screen_recording.mp4"

    recording_process = record_screen(output_file)
    print("Recording started.")

    time.sleep(30)

    stop_recording(recording_process)
    print(f"Screen recording saved to {output_file}")

if __name__ == "__main__":
    main()
rom1v commented 3 days ago

If you run directly your command, and interrupt with Ctrl+c, how long does it take to terminate?

More than 1 second is unexpected.

codematrixer commented 3 days ago

Executing commands on the terminal is normal

rom1v commented 3 days ago

Does it work better with SIGTERM?

Or with os.kill(process.pid, signal.SIGINT)?

codematrixer commented 3 days ago

I've tried all this before, but it doesn't work.

I lowered the version to 2.3.1 and restored it

rom1v commented 3 days ago

I cannot test right now, but FYI there is a --time-limit option in scrcpy (but maybe you're doing something more complex).

codematrixer commented 3 days ago

This ’time-limit‘ does not meet my requirements. Could you test it when you have time? Thanks!

rom1v commented 3 days ago

I justed tested your script on Linux with scrcpy 3.0 (just replacing time.sleep(30) by time.sleep(5), it works perfectly.