Author Topic: Script to cut out part of a video  (Read 183 times)

antisol

  • Newbie
  • *
  • Posts: 4
Script to cut out part of a video
« on: September 02, 2018, 05:03:40 PM »
Here is a script I wrote to remove the selected section of a video file.

Open your video, set marker A and B so that your selection contains the part you want to remove, and run this script.

Note that there are linux-specific paths in this code since the tinypy built into avidemux doesn't seem to have an equivalent of os.mktmp() to generate a temporary file. If you're using windows you'll want to change these to point to the relevant place.

(dear devs: please consider including more of the standard python libraries, sys, os, and re in particular would be extremely helpful! I'd also love to have a way to seek to the next/previous keyframe from tinypy)

Code: [Select]

#PY  <- Needed to identify #

# This script removes the selection from the video:
# Open a video file, select the start and end of the
# section you want to cut out, and then run this.
# it does this by saving start -> marker A as a file,
# then saving marker B -> end as another file,
# then it loads file A and appends file B
# finally, it saves the result

adm = Avidemux()

ed = Editor()

gui = Gui()

#prompt for destination file to save result to
dest = gui.fileWriteSelect("Choose destination file")


start = adm.markerA
end = adm.markerB

vidlen = ed.getVideoDuration()

adm.videoCodec("Copy")
adm.audioCodec(0, "copy");

#save section before selection:
adm.markerA = 0
adm.markerB = start

#NOTE: this is linux-specific. ideally you'd generate
# temporary file names and use those, but
# unfortunately the tinypy included with avidemux
# doesn't appear to include anything like os.tmpnam()
adm.save("/tmp/avidemux_remove_selection-A")

#save section after selection:
adm.markerA = end
adm.markerB = vidlen
#NOTE: also linux-specific, see anove.
adm.save("/tmp/avidemux_remove_selection-B")

#now, join the two sections together. more linux-specific paths here
adm.loadVideo("/tmp/avidemux_remove_selection-A")
adm.appendVideo("/tmp/avidemux_remove_selection-B")

adm.save(dest)

gui.displayInfo("Done!","All done!\n\nIf you survive, please come again!")


eumagga0x2a

  • Hero Member
  • *****
  • Posts: 2136
Re: Script to cut out part of a video
« Reply #1 on: September 04, 2018, 10:25:37 AM »
dear devs: please consider including more of the standard python libraries, sys, os, and re in particular would be extremely helpful!

I can imagine that using an extremely limited python implementation is deliberate and rather a security feature.

Quote
I'd also love to have a way to seek to the next/previous keyframe from tinypy

I'm currently trying to find out whether the absence of navigation and editor functions is a design decision or simply not implemented due to time shortage or similar.

With regard to your script, I think using temporary files and append can be avoided. Two adm.addSegment calls should be enough.

antisol

  • Newbie
  • *
  • Posts: 4
Re: Script to cut out part of a video
« Reply #2 on: September 04, 2018, 10:48:39 AM »
I can imagine that using an extremely limited python implementation is deliberate and rather a security feature.

Yeah I figured the same, but more functions like os.tmpnam() could be extremely helpful.

Quote
I'm currently trying to find out whether the absence of navigation and editor functions is a design decision or simply not implemented due to time shortage or similar.

I'd be interested to know what you find out. I tried looking through the avidemux source to try to hack this functionality in, but I'm no C coder and it's beyond my skill to do it easily.

I'd really like to be able to find the next/previous keyframe as currently my script cuts at the nearest keyframe to your selection. I'd like to be able to cut to precise frames by doing something like:
1. copy video from 0 to keyframe before marker A into tempfile A
2. re-encode from keyframe before marker A to marker A into tempfile B, using same codec/settings as the video
3. re-encode from marker B to keyframe after marker B into tempfile C, using same codec/settings as the video
4. copy video from keyframe after marker B to end into tempfile D
5. append all temp files together, copy to new file.

There's other functionality I'd need to do that, e.g i can't see a way to get video/audio codec and settings that I can pass to adm.VideoCodec and adm.AudioCodec


Quote
With regard to your script, I think using temporary files and append can be avoided. Two adm.addSegment calls should be enough.

That's an excellent suggestion, I'll give that a try. Thanks.

eumagga0x2a

  • Hero Member
  • *****
  • Posts: 2136
Re: Script to cut out part of a video
« Reply #3 on: September 04, 2018, 02:03:50 PM »
2. re-encode from keyframe before marker A to marker A into tempfile B, using same codec/settings as the video

There is no infrastructure in Avidemux to achieve this. Even if you implement it, the success for a particular video stream is not guaranteed for H.264 and newer codecs.

antisol

  • Newbie
  • *
  • Posts: 4
Re: Script to cut out part of a video
« Reply #4 on: September 10, 2018, 01:01:11 PM »
2. re-encode from keyframe before marker A to marker A into tempfile B, using same codec/settings as the video

There is no infrastructure in Avidemux to achieve this. Even if you implement it, the success for a particular video stream is not guaranteed for H.264 and newer codecs.

I'm not sure what you mean by this. I can do it just fine via the interface. If I select a keyframe as marker A and then a non-keyframe as marker B I can re-encode that section just fine from x264 to x264. The script would probably look something like:

Code: [Select]

start=adm.markerA
adm.markerA = adm.previousKeyframe(start)
adm.markerB = start
adm.videoCodec(adm.currentVideoCodec())
adm.save('/tmp/fileA')


If you mean that I can't do it from python, I already know that, that's my point.

I'm especially not sure what you mean by "success for a particular video stream is not guaranteed for H.264 and newer codecs", I'd love some clarification.


eumagga0x2a

  • Hero Member
  • *****
  • Posts: 2136
Re: Script to cut out part of a video
« Reply #5 on: September 10, 2018, 01:05:18 PM »
I assumed you wanted the "smart copy" feature of legacy Avidemux versions for old codecs revived for H.264.

Smart copy = re-encoding only incomplete GOPs at the beginning and at the end of a selection, using the copy mode for everything in between.

It doesn't work reliably for H.264.
« Last Edit: September 10, 2018, 01:09:08 PM by eumagga0x2a »

eumagga0x2a

  • Hero Member
  • *****
  • Posts: 2136
Re: Script to cut out part of a video
« Reply #6 on: September 10, 2018, 01:17:17 PM »
In other words, you won't be able to play the merged video after Step 5 without decoder giving up at the boundaries.

antisol

  • Newbie
  • *
  • Posts: 4
Re: Script to cut out part of a video
« Reply #7 on: September 10, 2018, 01:20:27 PM »
Aah, I wasn't aware of smart copy. Probably because I mostly work with h264.

Yes, that's exactly what I was trying to do. Oh well. Thanks for the reply anyway :)