I'am trying to write some code that is reading from stdin and then trims a video with seeking. Thats what I got so far:
def trim():
in_use=io.BytesIO()
process = sp.Popen(shlex.split('ffmpeg -i pipe: -ss 00:00:01.0 -t 00:00:01.4 -c:v libx264 -strict -2 output.mp4'), stdin=sp.PIPE, bufsize=10**8)
# Pipewriter function
pipewriter(in_use,process)
process.wait()
The pipewriter function does look like this:
def pipewriter():
video.seek(0)
for chunk in iter(partial(video.read,1024),b''):
process.stdin.write(chunk)
process.stdin.flush()
process.stdin.close()
The file inside the in_use io.BytesIO object is a valid video and thats not the problem. The output file also does get generatet and trimmed correctly so the function does work. My problem is that because of seeking and trimming the pipewriter function does write the whole video into the pipe. But the ffmpeg process stops after -t 00:00:01.4 seconds so the rest of the video written in stdin leads to an pipe Error
Does somebody got a clean solution for that without try except. I also do have to trim the video as accurate as possible. The current solution does work good for me.
Error:
process.stdin.flush()
BrokenPipeError: [Errno 32] Broken pipe