0

I'm trying to cut out a fragment from video with the given start and end frames indexes or theirs timestamps (preferably frame indexes). So far I've found this Q but it takes floats and convert them to integers hence the output video has inaccurate frames

this is a sample of what I've tried which gives me inaccurate output video

def extract_fragment(input_path, output_path):
    start_time = 3.453453
    end_time = 6.8768678

    ffmpeg_extract_subclip(
        filename=input_path,
        t1=start_time,
        t2=end_time,
        targetname=output_path,
    )

And I also tried to use ffmpeg directly

with duration

    subprocess.call(
        [
            "ffmpeg",
            "-start_at_zero",
            "-copyts",
            "-ss",
            start_time,
            "-t",
            end_time - start_time,
            "-i",
            f"{path_to_video}",
            "-c",
            "copy",
            f"{output_video_path}",
        ]
    )
    subprocess.call(
        [
            "ffmpeg",
            "-y",
            "-ss",
            str(start_time),
            "-i",
            path_to_video,
            "-t",
            str(end_time),
            "-map",
            "0",
            "-vcodec",
            "copy",
            "-acodec",
            "copy",
            output_video_path,
        ]
    )
cxnt
  • 111
  • 3
  • Have you tried `clip = VideoFileClip("video1.mp4").cutout(start_time, end_time)` and `clip.write_videofile("test.mp4")` ? – Rotem Dec 12 '22 at 20:42
  • this one is cutting out a fragment from video, but I need to extract this fragment as a separate file @Rotem – cxnt Dec 13 '22 at 10:09
  • What do you mean? `test.mp4` is extracted as a separate file. – Rotem Dec 13 '22 at 10:13
  • What I want is similar to this [Question](https://stackoverflow.com/questions/37317140/cutting-out-a-portion-of-video-python?noredirect=1&lq=1), but I want to set frame indexes instead of frame timestamps, or set frame timestamps in float format that wont be converted to integers ones passed to ffmpeg @Rotem – cxnt Dec 13 '22 at 10:32
  • `ffmpeg_extract_subclip` applies cutting without re-encoding. You can't get accurate cut of without re-encoding because the first frame must be a **key frame** (without re-encoding). According to the source code of MoviePy, the time is rounded to 2 decimal digits (not integers). You may try using FFmpeg directly (without MoviePy). Try using FFmpeg in command line first. I am sure there are good examples for cutting video part using FFmpeg. For executing the FFmpeg command from Python you may use subprocess module. – Rotem Dec 13 '22 at 10:53
  • I've tried ffmpeg directly too, still can't get accurate frames, I'll update questions with snippets @Rotem – cxnt Dec 13 '22 at 11:07
  • `"-vcodec", "copy", "-acodec", "copy"` is without re-encoding, so it's not going to work. – Rotem Dec 13 '22 at 11:20
  • I am sure there are good examples for cutting video part using FFmpeg. I recommend you to use Google, and find a solution. I also recommend you to do some research about video codecs. – Rotem Dec 13 '22 at 11:30

0 Answers0