Does somebody got a clean solution for that without try except.

No one has a fundamentally better solution because this is how the backwards propagation of a pipe closure is designed to work in Unix.

  • Forward propagation happens by a program reading from the closed input pipe, seeing EOF, wrapping up, and closing its output pipe (if any).

  • Backwards propagation happens by a program writing to the closed pipe and (by default) receiving a SIGPIPE that kills it, causing any open input pipes to be closed. Programs can choose to ignore SIGPIPE and instead handle the EPIPE exit code itself which Python uses to raise an Error in its place.

All APIs layered on top, like subprocess.communicate, simply work with this fact under the hood. The best practice is to stop fighting Python and Unix, and just go with the flow using a try-catch (optionally tidied away in a helper function).

However, if you really want a cosmetically cleaner version without try-catch, there are several bad practices you can invoke, such as disabling Python's default signal handler:

import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL);

This will cause the Python process to immediately and silently be killed instead, which is how most programs in pipelines are stopped, such as find in find / | head -n 1

Answer from that other guy on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 41529023 › python-ffmpeg-like-media-seeking-with-requests
video - Python - ffmpeg-like media seeking with requests - Stack Overflow
January 8, 2017 - Using ffmpeg, times in HTTP media resources, such as videos, can be seeked to very quickly with a command like ffmpeg -ss 00:01:00 -i https://example.com/video.mp4, presumably by downloading the video headers to look for the right byte offset.
🌐
GitHub
github.com › kkroening › ffmpeg-python
GitHub - kkroening/ffmpeg-python: Python bindings for FFmpeg - with complex filtering support · GitHub
Python bindings for FFmpeg - with complex filtering support - kkroening/ffmpeg-python
Starred by 11K users
Forked by 940 users
Languages   Python
Discussions

Reading specific frames from a video
So, I was wondering how to read a video, frame-by-frame and select only specific frames. For example - Say the video has 240 frames in total, and I want to read frames at index [0, 10, 20, 30, and ... More on github.com
🌐 github.com
3
August 23, 2018
python - ffmpeg stdin Pipe seeking - Stack Overflow
There are some fundamental problems ... with ffmpeg specifically, but other programs may fork off worker processes of their own). 2022-01-28T19:28:17.513Z+00:00 ... @thatotherguy - Just read your answer, and it makes sense. So, the try-except really is the best (dear I say pythonic?) option. Thanks for the insight. 2022-01-28T20:00:13.157Z+00:00 ... If video is guaranteed to be a pipe or FIFO, then video.seek(0) will never ... More on stackoverflow.com
🌐 stackoverflow.com
January 28, 2022
Formula to get frame display number after seek in FFmpeg - Stack Overflow
For a given AV file, I need to be able to seek to a particular video frame number in the file. Because of how decoding works, I am seeking to the nearest I-Frame before the required frame (which m... More on stackoverflow.com
🌐 stackoverflow.com
September 26, 2021
I've DOUBLED the FFmpeg Speed!
I've also managed to shoe-horn the 'ss:' and 'to:' input properties into ffmpeg-python's call constructor. This means being able to 'seek' near-instantaneously to any point in the video requested. More on github.com
🌐 github.com
14
January 2, 2022
🌐
FFmpeg Python
kkroening.github.io › ffmpeg-python
ffmpeg-python: Python bindings for FFmpeg — ffmpeg-python documentation
filter_ is normally used by higher-level filter functions such as hflip, but if a filter implementation is missing from ffmpeg-python, you can call filter_ directly to have ffmpeg-python pass the filter name and arguments to ffmpeg verbatim.
🌐
Bannerbear
bannerbear.com › blog › how-to-use-ffmpeg-in-python-with-examples
How to Use FFMpeg in Python (with Examples) - Bannerbear
Then, make an HTTP request in your Python code to call the Bannerbear API and trigger the video generation process. You will get the URL of the resulting video in the response and here’s a screenshot of the video: ... You can refer to this tutorial and the API Reference to learn how to do it in detail. The ffmpeg-library enables you to use FFmpeg in Python to manipulate various media files for different purposes like building comprehensive multimedia applications, preprocessing media files for machine learning projects, etc.
🌐
GitHub
github.com › kkroening › ffmpeg-python › issues › 114
Reading specific frames from a video · Issue #114 · kkroening/ffmpeg-python
August 23, 2018 - So, I was wondering how to read a video, frame-by-frame and select only specific frames. For example - Say the video has 240 frames in total, and I want to read frames at index [0, 10, 20, 30, and ...
Author   MrinalJain17
🌐
PyPI
pypi.org › project › ffmpeg-python › 0.1.1
ffmpeg-python · PyPI
If you're like me and find Python to be powerful and readable, it's easy with `ffmpeg-python`: ``` import ffmpeg in_file = ffmpeg.file_input(TEST_INPUT_FILE) overlay_file = ffmpeg.file_input(TEST_OVERLAY_FILE) ffmpeg \ .concat( in_file.trim(10, 20), in_file.trim(30, 40), ) \ .overlay(overlay_file.hflip()) \ .drawbox(50, 50, 120, 120, color='red', thickness=5) \ .file_output(TEST_OUTPUT_FILE) \ .run() ``` `ffmpeg-python` takes care of running `ffmpeg` with the command-line arguments that correspond to the above filter diagram, and it's easy to what's going on and make changes as needed.
      » pip install ffmpeg-python
    
Published   May 14, 2017
Version   0.1.1
Top answer
1 of 3
3

Does somebody got a clean solution for that without try except.

No one has a fundamentally better solution because this is how the backwards propagation of a pipe closure is designed to work in Unix.

  • Forward propagation happens by a program reading from the closed input pipe, seeing EOF, wrapping up, and closing its output pipe (if any).

  • Backwards propagation happens by a program writing to the closed pipe and (by default) receiving a SIGPIPE that kills it, causing any open input pipes to be closed. Programs can choose to ignore SIGPIPE and instead handle the EPIPE exit code itself which Python uses to raise an Error in its place.

All APIs layered on top, like subprocess.communicate, simply work with this fact under the hood. The best practice is to stop fighting Python and Unix, and just go with the flow using a try-catch (optionally tidied away in a helper function).

However, if you really want a cosmetically cleaner version without try-catch, there are several bad practices you can invoke, such as disabling Python's default signal handler:

import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL);

This will cause the Python process to immediately and silently be killed instead, which is how most programs in pipelines are stopped, such as find in find / | head -n 1

2 of 3
1

Can't you just do this?

def pipewriter(video, process):
    video.seek(0)
    for chunk in iter(partial(video.read,1024),b''):
        if process.poll() is not None:
            break
        process.stdin.write(chunk)
    if process.poll() is None:
        process.stdin.flush()
    process.stdin.close()

Based on the OP's addendum in the comment below:

Now I want to create n videos with variable length x and variable start point k and variable endpoint p

Maybe this one does the job:

def trim(ss, t ,outfile):
    sp.run(f'ffmpeg -i pipe: -ss {ss} -t {t} -c:v libx264 -strict -2 {outfile}'), 
        stdin=sp.PIPE, input=in_use.getbuffer())

for mp4file, ss, t in [('out1.mp4',ss0,t0),('out2.mp4',ss1,t1),...]:
    trim(ss,t,mp4file)
Find elsewhere
🌐
GitHub
github.com › kkroening › ffmpeg-python › blob › master › README.md
ffmpeg-python/README.md at master · kkroening/ffmpeg-python
Python bindings for FFmpeg - with complex filtering support - ffmpeg-python/README.md at master · kkroening/ffmpeg-python
Author   kkroening
🌐
FFmpeg
ffmpeg.org › ffmpeg.html
ffmpeg Documentation
3 weeks ago - When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between ...
🌐
Stack Overflow
stackoverflow.com › questions › 69339291 › formula-to-get-frame-display-number-after-seek-in-ffmpeg
Formula to get frame display number after seek in FFmpeg - Stack Overflow
September 26, 2021 - For a given AV file, I need to be able to seek to a particular video frame number in the file. Because of how decoding works, I am seeking to the nearest I-Frame before the required frame (which m...
🌐
GitHub
github.com › bml1g12 › benchmarking_video_reading_python › issues › 1
I've DOUBLED the FFmpeg Speed! · Issue #1 · bml1g12/benchmarking_video_reading_python
January 2, 2022 - I've also managed to shoe-horn the 'ss:' and 'to:' input properties into ffmpeg-python's call constructor. This means being able to 'seek' near-instantaneously to any point in the video requested.
Author   roninpawn
🌐
Stack Overflow
stackoverflow.com › questions › tagged › ffmpeg-python
Highest scored 'ffmpeg-python' questions - Stack Overflow
November 14, 2022 - I am trying to learn to convert ffmpeg command line background blur filter to ffmpeg-python format.
🌐
GitHub
github.com › Zulko › moviepy › issues › 2115
Seeking by frame is inexact and inconsistent · Issue #2115 · Zulko/moviepy
February 14, 2024 - moviepy largely treats "get the frame at time x" as shorthand for "get the frame displayed at time x", so "get the frame at time 0.00001" should mean "get frame 0". But ffmpeg treats "get the frame at time x" as "skip all frames until reaching time x", so sometimes clip.make_frame(0.00001) will return frame 1, and sometimes it will return frame 0, depending on what's been cached in last_read.
Author   bzczb
🌐
GitHub
github.com › kkroening › ffmpeg-python › issues › 441
images/frame extraction · Issue #441 · kkroening/ffmpeg-python
November 5, 2020 - Hi, I am trying to extract images from a video both for a whole video and for specific duration of the video. I tried to implement the commands of fffmpeg (from its documnetation) but unable to run them using ffmpeg-python package. I hav...
Author   hlmhlr
🌐
Matham
matham.github.io › ffpyplayer › player.html
Player — FFPyPlayer documentation
Although log are also filtered globally according to the level of set_loglevel, this is applied first to quickly filter logs generated by this instance (it’s not applied to internal ffmpeg logs). ... The threading library to use internally. Can be one of ‘SDL’ or ‘python’.
🌐
Reddit
reddit.com › r/ffmpeg › seeking by frame?
r/ffmpeg on Reddit: Seeking by frame?
July 14, 2020 -

So, I'm trying to split an almost three hour long three video file into shorter files. I was originally doing so with a video editor, but this would require to re encode the video and take quite a very long time. However, I do have the specific length of the splits in frames, and I managed to create a single file like this:

ffmpeg -ss 00:00:00 -i "file.avi" -c:v copy -c:a copy -frames:v 88705 "file split 01.avi"

What I would like to do next is to set the seek function to an specific frame as the beginning to cut the video with precision. How can I do this?

🌐
FFmpeg
ffmpeg.org
FFmpeg
- aecho filter - perspective filter ported from libmpcodecs - ffprobe -show_programs option - compand filter - RTMP seek support - when transcoding with ffmpeg (i.e. not streamcopying), -ss is now accurate even when used as an input option. Previous behavior can be restored with the -noaccurate_seek option.