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

Add time-limit argument #3752

Closed
1 task done
mattia-lecci opened this issue Feb 24, 2023 · 8 comments
Closed
1 task done

Add time-limit argument #3752

mattia-lecci opened this issue Feb 24, 2023 · 8 comments

Comments

@mattia-lecci
Copy link

Related but not solving my issue:

Is your feature request related to a problem? Please describe.
I am trying to automate the acquisition of videos from an android phone which can last for hours.
I know how long the recording should be, so something like adb screenrecord --time-limit would be great (see here)

Describe the solution you'd like
Something similar to adb screenrecord --time-limit=S <file> would be great (see here), where S is the amount of time in seconds of the desired recording duration.

Describe alternatives you've considered
My automation system is only working on windows, unfortunately.
I tried interrupting the scrcpy process after a fixed amount of time in multiple ways but it is either not stopping or produces a corrupted video file.

Tests:

  • start /B scrcpy -Nr test.mp4 && timeout /t 5 /nobreak >nul && taskkill /im scrcpy.exe /f: corrupted file
  • Python script
import subprocess, signal, time

proc = subprocess.Popen(["scrcpy", "-Nr", "test.mp4"],
                        creationflags=subprocess.CREATE_NEW_PROCESS_GROUP)
print("sleeping")
time.sleep(5)
print("terminating")
proc.terminate() # also tried proc.send_signal(signal.CTRL_C_EVENT) and proc.send_signal(signal.CTRL_BREAK_EVENT)
@rom1v
Copy link
Collaborator

rom1v commented Feb 24, 2023

As a workaround, did you test #818 (comment)?

@rom1v
Copy link
Collaborator

rom1v commented Feb 24, 2023

Ah yes, it's your last test. Weird, it should work.

@rom1v
Copy link
Collaborator

rom1v commented Feb 24, 2023

Just to be sure, if you run scrcpy -Nr file.mp4 manually and press Ctrl+c in the terminal, is the recorded file corrupted?

@mattia-lecci
Copy link
Author

Yes, pressing pressing Ctrl+c from windows terminal does work as expected. Sorry for not mentioning that!

@nnnpa31
Copy link

nnnpa31 commented Mar 2, 2023

You can refer to this:
https://stackoverflow.com/questions/7085604/sending-c-to-python-subprocess-objects-on-windows

In python's subprocess, when sending CTRL+C command for stdin of a specific process ID, it will wait for the process to finish before sending it. Therefore, you should change the pid to 0 and send CTRL_C_EVENT to the parent process in order to achieve the purpose of interrupting the process.
which mean:

os.kill(0, signal.CTRL_C_EVENT)
#       ^Here
@mattia-lecci
Copy link
Author

Thank you @nnnpa31 !
I had to slightly modify the code suggested in this answer.

The following setup is working for me.

# demo.py
# this script is launched by wrapper.py

import signal, sys, time, subprocess

def signal_handler(signal, frame):
  print('Ctrl+C received in demo.py')
  time.sleep(1)
  sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

print('demo.py started')
proc = subprocess.Popen(["scrcpy", "-Nr", "test.mp4"]) # simple scrcpy command
# wrapper.py

import subprocess, time, signal, sys, os

def signal_handler(signal, frame):
  print('Ctrl+C received in wrapper.py')
  time.sleep(1)

signal.signal(signal.SIGINT, signal_handler)
print("wrapper.py started")
subprocess.Popen("python demo.py")
time.sleep(5) # Run for 5 seconds
os.kill(0, signal.CTRL_C_EVENT)

Launching this with python wrapper.py record a 5s video as expected.

This is a nice workaround and it works well enough for me, so I'll close the issue.
Having it natively into scrcpy, though, would be even better!

@nnnpa31
Copy link

nnnpa31 commented Mar 2, 2023

Having it natively into scrcpy, though, would be even better!

I agree.

rom1v added a commit that referenced this issue Jun 1, 2023
Add an option to stop scrcpy automatically after a given delay.

Fixes #3752 <#3752>
@rom1v rom1v mentioned this issue Jun 1, 2023
@rom1v
Copy link
Collaborator

rom1v commented Jun 1, 2023

See #4052

rom1v added a commit that referenced this issue Jun 10, 2023
Add an option to stop scrcpy automatically after a given delay.

PR #4052 <#4052>
Fixes #3752 <#3752>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants