I am trying to read the frames of a video using this command but from a python script using the subprocess
module. The output of the command is in string
but since the frame count of a video is an integer I would like to have it as an int
object in python. However, int()
as shown here is not working as expected for I assumed I would get 321
as an int.
Terminal Output:
ValueError: invalid literal for int() with base 10: ''
I have tried solutions from here like float()
but they are not working for me.
Code:
import subprocess
def main():
shell_ffprobe_frame_count = ["ffprobe", "-v", "error", "-select_streams", "v:0", "-count_packets", "-show_entries", "stream=nb_read_packets", "-of", "csv=p=0", "test.mkv"]
output = subprocess.Popen(shell_ffprobe_frame_count, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
print(output.stdout.read()) # 321
print(type(output.stdout.read())) # <class 'str'>
print(int(output.stdout.read())) # should be '321'
print(type(int(output.stdout.read()))) # should be 'int'
if __name__ == "__main__":
main()
I got <class 'str'>
as return type from the subprocess which I have never seen ever before. type('321')
produces just str
in my terminal. I do not understand the difference and I wager this is the reason for the error. Any help is appreciated.