Trim: Difference between revisions

From Avisynth wiki
Jump to navigationJump to search
Raffriff42 (talk | contribs)
add link to avs+ documentation
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<div {{BlueBox2|40|0|3px solid purple}} >
{{AvsPlusFullname}}<br>
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/trim.html https://avisynthplus.readthedocs.io]
</div>
== Trim ==
== Trim ==
<div style="max-width:62em" >
<div style="max-width:62em" >
Line 5: Line 11:
'''Trim''' cannot trim a clip if there is no video. Use '''AudioTrim''' (from AviSynth v2.60) for that case; for versions prior to v2.60, you must [[AudioDub]] your audio-only clip to a video clip and '''Trim''' ''that''.  
'''Trim''' cannot trim a clip if there is no video. Use '''AudioTrim''' (from AviSynth v2.60) for that case; for versions prior to v2.60, you must [[AudioDub]] your audio-only clip to a video clip and '''Trim''' ''that''.  


For any non-trivial editing, you will want to use [[ShowFrameNumber]] on the source clip, to keep track of your edits.
The resulting clip starts with frame number 0, and this must be taken into account when making additional edits using that clip. To view a clip's frame number at any point in your script, temporarily insert an [[Info]] or [[ShowFrameNumber]] statement.  




Line 19: Line 25:
:*If you set {{FuncArg|last_frame}} to 0, you will get a clip starting at {{FuncArg|first_frame}} and running to the end of the clip.
:*If you set {{FuncArg|last_frame}} to 0, you will get a clip starting at {{FuncArg|first_frame}} and running to the end of the clip.


:{{Par2|pad|bool|true}}
{{Par2h5|pad|bool|true}}
:True by default, {{FuncArg|pad}} causes the audio stream to be padded to align with the video stream. Otherwise the tail of a short audio stream is left so. You should use {{FuncArg|pad}}=false when the soundtracks being joined were originally contiguous &ndash; compare to [[UnalignedSplice]].
::True by default, {{FuncArg|pad}} causes the audio stream to be padded to align with the video stream. Otherwise the tail of a short audio stream is left so. You should use {{FuncArg|pad}}=false when the soundtracks being joined were originally contiguous &ndash; compare to [[UnalignedSplice]].




Line 59: Line 65:
'''AudioTrim''' cannot trim a clip if there is no audio. Use '''Trim''' for that case.  
'''AudioTrim''' cannot trim a clip if there is no audio. Use '''Trim''' for that case.  


For any non-trivial editing, you will want to use [[ShowTime]] on the source clip, to keep track of your edits.
The resulting clip starts with time = 0.0, and this must be taken into account when making additional edits to that clip. To view a clip's time at any point in your script, temporarily insert an [[Info]] or [[ShowTime]] statement.  




Line 99: Line 105:


== Examples ==
== Examples ==
<div {{BoxWidthIndent|56|0}} >
* Return only the first frame (frame 0)
  Trim(100,0)             # delete the first 100 frames, audio padded
<div {{BoxWidthIndent|26|2}} >
                        # or trimmed to match the video length.
  Trim(0, -1)
Trim(0, end=0)
Trim(0, length=1)
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Return only the last frame
  Trim(100,0,false)       # delete the first 100 frames of audio and video,
<div {{BoxWidthIndent|26|2}} >
                        # the resulting stream lengths remain independent.
  Trim(FrameCount, -1)
Trim(FrameCount, end=Framecount)
Trim(FrameCount, length=1)
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Return frames 100-199 (duration=100)
  Trim(100,-100)         # is the same as trim(100,199) ie, start=100, duration=100
<div {{BoxWidthIndent|26|2}} >
  Trim(100, 199)
Trim(100, -100)
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Delete the first 100 frames; audio padded or trimmed to match the video length.
  Trim(100,199,false)     # audio will be trimmed if longer but not
<div {{BoxWidthIndent|26|2}} >
                        # padded if shorter to frame 199
  Trim(100, 0) # (pad=true by default)
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Delete the first 100 frames of audio and video; the resulting stream lengths remain independent.{{Dubious|Talk:Trim}}
  Trim(0,-1)              # returns only the first frame
<div {{BoxWidthIndent|26|2}} >
Trim(0,end=0)          #
  Trim(100, 0, pad=false)
Trim(0,length=1)       #
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Trim audio if longer than video ({{FuncArg|pad}} does affect this action)
  AudioTrim(1,5.5)        # keeps the audio samples between 1 and 5.5 seconds
<div {{BoxWidthIndent|26|2}} >
AudioTrim(1,end=5.5)   #
  Trim(0, FrameCount-1)  
</div>
</div>


<div {{BoxWidthIndent|56|0}} >
* Audio will be trimmed if longer, but not padded if shorter to frame 199
  AudioTrim(1,-5.5)      # cuts the first second and keeps the following 5.5 seconds
<div {{BoxWidthIndent|26|2}} >
AudioTrim(1,length=5.5) #
  Trim(100, 199, pad=false)
</div>
</div>


* AudioTrim: trim video if longer than audio
<div {{BoxWidthIndent|26|2}} >
AudioTrim(0, AudioDuration)
</div>
* AudioTrim: keep the audio between 1.0 and 6.5 seconds
<div {{ListItemContinue}} >
(ie, delete the first second, keep the following 5.5 seconds)
</div>
<div {{BoxWidthIndent|26|2}} >
AudioTrim(1, 6.5)     
AudioTrim(1, -5.5)     
AudioTrim(1, length=5.5)
</div>


== Changes ==
== Changes ==

Latest revision as of 06:50, 18 September 2022

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


Trim

Trims a clip – removes frames from one or both ends.

Trim cannot trim a clip if there is no video. Use AudioTrim (from AviSynth v2.60) for that case; for versions prior to v2.60, you must AudioDub your audio-only clip to a video clip and Trim that.

The resulting clip starts with frame number 0, and this must be taken into account when making additional edits using that clip. To view a clip's frame number at any point in your script, temporarily insert an Info or ShowFrameNumber statement.


For convenience, Trim can be called in four slightly different ways:


Trim(clip, int first_frame, int last_frame [, bool pad] )

Returns a clip starting at first_frame and running up to and including last_frame.
  • For example, Trim(3, 5) returns source clip frames 3, 4 and 5.
  • Remember, AviSynth starts counting at frame 0.
  • If you set last_frame to 0, you will get a clip starting at first_frame and running to the end of the clip.
 pad
 bool  pad = true
True by default, pad causes the audio stream to be padded to align with the video stream. Otherwise the tail of a short audio stream is left so. You should use pad=false when the soundtracks being joined were originally contiguous – compare to UnalignedSplice.


Trim(clip, int first_frame, int -num_frames [, bool pad] )

With a negative value for the second argument, you get a clip starting at first_frame and running for num_frames frames.
  • For example, Trim(0, -4) returns source clip frames 0, 1, 2 and 3.


Trim(clip, int first_frame, [int end , bool pad] )

Returns a clip starting at first_frame and running up to and including frame end. From Avisynth v2.60.
  • For example, Trim(3, end=7) is equivalent to Trim(3, 7); both return frames 3, 4, 5, 6 and 7.
  • end default = 0; must be >= first_frame.
  • Unlike the last_frame syntax, the end syntax has no discontinuous boundary values: end=0 means end at frame 0. This feature is useful in avoiding unexpected boundary conditions in your user functions.


Trim(clip, int first_frame, [int length , bool pad] )

Returns a clip starting at first_frame and running for length frames. From Avisynth v2.60.
  • For example, Trim(3, length=5) is equivalent to Trim(3, -5); both return frames 3, 4, 5, 6 and 7.
  • length default = 0; must be >= 0.
  • Unlike the num_frames syntax, the length syntax has no discontinuous boundary values: length=0 means return a zero length clip. This feature is useful in avoiding unexpected boundary conditions in your user functions.


AudioTrim

AudioTrim (from Avisynth v2.60) trims a clip based on time, not on frames. This is most useful for audio-only clips, where "frames" have no meaning anyway, and you may want to edit with finer precision than whole frames (at 30fps, 1 frame=33.3ms).

All time arguments are in seconds, floating-point.

  • Trims on audio-only clips are accurate to the nearest audio sample.
  • Trims on clips with video are accurate to the nearest whole video frame.

AudioTrim cannot trim a clip if there is no audio. Use Trim for that case.

The resulting clip starts with time = 0.0, and this must be taken into account when making additional edits to that clip. To view a clip's time at any point in your script, temporarily insert an Info or ShowTime statement.


For convenience, AudioTrim can be called in four slightly different ways:


AudioTrim(clip, float start_time, float end_time)

Returns a clip starting at start_time and running up to and including time end_time.
  • For example, AudioTrim(3.0, 5.0) returns source clip from time 00:00:03.000 to 00:00:05.000.
  • If you set end_time to 0.0, you will get a clip starting at start_time seconds and running to the end of the clip.


AudioTrim(clip, float start_time, float -duration)

With a negative value for the second argument, you will get a clip starting at start_time and running for duration seconds.
  • For example, AudioTrim(0.0, -4.0) returns the source clip from time 00:00:00.000 to 00:00:04.000.


AudioTrim(clip, float start_time [, float end] )

Returns a clip starting at start_time and running up to and including time end.
  • For example, AudioTrim(3.0, end=7.0) is equivalent to AudioTrim(3.0, 7.0)
  • end default = 0.0; must be >= start_time.
  • Unlike the end_time syntax, the end syntax has no discontinuous boundary values: end=0.0 means return a zero length clip. This feature is useful in avoiding unexpected boundary conditions in your user functions.


AudioTrim(clip, float start_time [, float length] )

Returns a clip starting at start_time and running for length seconds.
  • For example, AudioTrim(3.0, length=4.0) is equivalent to AudioTrim(3.0, -4.0)
  • length default = 0.0; must be >= 0.
  • Unlike the duration syntax, the length syntax has no discontinuous boundary values: length=0.0 means return a zero length clip. This feature is useful in avoiding unexpected boundary conditions in your user functions.


Examples

  • Return only the first frame (frame 0)
Trim(0, -1)
Trim(0, end=0)
Trim(0, length=1)
  • Return only the last frame
Trim(FrameCount, -1)
Trim(FrameCount, end=Framecount)
Trim(FrameCount, length=1)
  • Return frames 100-199 (duration=100)
Trim(100, 199)
Trim(100, -100)
  • Delete the first 100 frames; audio padded or trimmed to match the video length.
Trim(100, 0) # (pad=true by default)
  • Delete the first 100 frames of audio and video; the resulting stream lengths remain independent.[dubious – discuss]
Trim(100, 0, pad=false)
  • Trim audio if longer than video (pad does affect this action)
Trim(0, FrameCount-1) 
  • Audio will be trimmed if longer, but not padded if shorter to frame 199
Trim(100, 199, pad=false)
  • AudioTrim: trim video if longer than audio
AudioTrim(0, AudioDuration)
  • AudioTrim: keep the audio between 1.0 and 6.5 seconds

(ie, delete the first second, keep the following 5.5 seconds)

AudioTrim(1, 6.5)       
AudioTrim(1, -5.5)       
AudioTrim(1, length=5.5)

Changes

v2.60 Added AudioTrim. Added length and end parameters.
v2.56 Added pad audio.