imageio / imageio-ffmpeg

FFMPEG wrapper for Python
BSD 2-Clause "Simplified" License
233 stars 52 forks source link

can't trim video #101

Closed sugizo closed 7 months ago

sugizo commented 7 months ago

objective can trim video duration

code

input_file = 'audio_video.mp4'
output_file = 'input_ss_t.mp4'
audio_file = 'audio_video.mp4' # audio or video file

reader = read_frames(input_file)
meta = reader.__next__()

writer = write_frames(output_file, audio_path = audio_file,
                      size = meta['size'], fps = meta['fps'], 
                      input_params = ['-y', '-ss', '2', '-to', '3', ],
                      output_params = ['-r', '60', ] )
writer.send(None)

for frame in reader:
    writer.send(frame)

writer.close()

result

BrokenPipeError                           Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/imageio_ffmpeg/_io.py](https://localhost:8080/#) in write_frames(path, size, pix_fmt_in, pix_fmt_out, fps, quality, bitrate, codec, macro_block_size, ffmpeg_log_level, ffmpeg_timeout, input_params, output_params, audio_path, audio_codec)
    626             try:
--> 627                 p.stdin.write(bb)
    628             except Exception as err:

BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
1 frames
[/usr/local/lib/python3.10/dist-packages/imageio_ffmpeg/_io.py](https://localhost:8080/#) in write_frames(path, size, pix_fmt_in, pix_fmt_out, fps, quality, bitrate, codec, macro_block_size, ffmpeg_log_level, ffmpeg_timeout, input_params, output_params, audio_path, audio_codec)
    632                     "OUTPUT:\n".format(err, cmd_str)
    633                 )
--> 634                 raise IOError(msg)
    635 
    636             nframes += 1

OSError: [Errno 32] Broken pipe

FFMPEG COMMAND:
/usr/local/lib/python3.10/dist-packages/imageio_ffmpeg/binaries/ffmpeg-linux64-v4.2.2 -y -f rawvideo -vcodec rawvideo -s 640x368 -pix_fmt rgb24 -r 60.00 -y -ss 5 -to 8 -i - -i audio_video.mp4 -vcodec libx264 -pix_fmt yuv420p -crf 25 -v warning -r 60 -map 0:v:0 -map 1:a:0 input_ss_t.mp4

FFMPEG STDERR OUTPUT:

another trials trying to use -ss 2 and -t 3 parameter put in output_params trying to use -ss 2 and -to 3 parameter put in input_params and output_params but got the same result

question how to face this problem ?

best regards

almarklein commented 7 months ago

I think -ss and -t can only be used when an input is provided with ffmpeg. For this kind of trimming imageio-ffmpeg is not a good fit. You can better use ffmpeg directly and specify both -i and -o.

The way this could work with imageio-ffmpeg is to read the frames, trim these in Python (i.e. make a selection), and then write the result. A bit different approach.