question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to cut (`trim`) video and audio with timestamps

See original GitHub issue

Great work!

I’m confused as to how to solve this problem, but am sure that it is solvable with a little guidance. I’m trying to trim a clip that should be 600/25(fps) = 24s long.

(
    input_video
    .trim   (start_frame=10000, end_frame=10600) # output len --> 600 frames long
    .output ('out.mp4' )
    .run()
) 

However, the output gives me a file that is 10600 frames long, where the first 10000 frames don’t play in some video players while they skip over in others. Nevertheless, what I wish for is a clip that is only 600 frames long.

Additionally, my output files do not have any audio. This isn’t an issue though, but a solution would be a nice bonus.

Thanks!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:8
  • Comments:9

github_iconTop GitHub Comments

38reactions
rsomani95commented, Jun 6, 2019

I found a solution:

(
    input_vid
    .trim   (start_frame = 10000, end_frame = 10600)
    .setpts ('PTS-STARTPTS')
    .output (f'output.mp4')
    .run()
) 

.setpts ('PTS-STARTPTS') basically resets the timestamp to 0 after a trim filter has been passed. This fixes the first issue I raised.

The above output, however, does not have any audio attached to the output. This is due to ffmpeg’s behavior, as has been discussed in detail in #26 , #208 and shown in this example. Mysteriously the input.audio or input.video didn’t work for me, but the following code can be used as a template to do the same:

vid = (
    input_vid
    .trim(start = 100, end = 160)
    .setpts('PTS-STARTPTS')
)
aud = (
    input_vid
    .filter_('atrim', start = 100, end = 160)
    .filter_('asetpts', 'PTS-STARTPTS')
)

joined = ffmpeg.concat(vid, aud, v=1, a=1)
output = ffmpeg.output(joined[0], joined[1], 'out.mp4')
output.run()
25reactions
j-mincommented, Jun 21, 2019

@rsomani95 's snippet gave me the error below.

TypeError: Expected string index (e.g. 'a'); got 0

In my case, adding .node after joined solved the problem.

def trim(input_path, output_path, start=30, end=60):
    input_stream = ffmpeg.input(input_path)

    vid = (
        input_stream.video
        .trim(start=start, end=end)
        .setpts('PTS-STARTPTS')
    )
    aud = (
        input_stream.audio
        .filter_('atrim', start=start, end=end)
        .filter_('asetpts', 'PTS-STARTPTS')
    )

    joined = ffmpeg.concat(vid, aud, v=1, a=1).node
    output = ffmpeg.output(joined[0], joined[1], output_path)
    output.run()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Cut Split Or Trim Videos In Vlc Media Player - YouTube
(2022 UPDATE) The Alternative to VLC Media Player to Cut /Split/ Trim :http://bit.ly/3U2VbFd Check out how to cut split or trim videos in...
Read more >
How to Cut and Trim a Video in Under 5 Minutes: Windows ...
1. Open the iMovie app. Click on “New Project” then “Movie,” and upload the video you want to trim. 2. Drag the video...
Read more >
How to trim a video using FFmpeg - Shotstack
Use FFmpeg to trim the start, end or middle of a video to create shorter segments.
Read more >
How to cut (`trim`) audio with timestamps Python
I've spent 3 days already and this feels like a millennium prize math problem. Ok so I have a .ttml of timestamps where...
Read more >
Trimming a video, audio, or image clip - Clipchamp Help Center
How to trim a video in Clipchamp · Step 1. Drag and drop the video into the timeline · Step 2. Drag the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found