0

Using ffmpeg, there are suggestions on how to trim or how to fade in, but how can you do both in one command?

The video should start black at 00:10:07 with a fade in for 1 second, so at 00:10:08 it should be fully visible.

  1. Is it possible to trim and fade in at the same time?
  2. Can I test run it first (for big video files, this takes hours). ffplay works somewhat differrent than ffmpeg.
  3. And is it even possible to do something similar at the end (start fade at 01:28:57 with the duration 1 second and then ignore the rest)?

Thanks for any help!

Gary

1
  • 1. Yes - you combine the cut and fade. 2. Test run using a smaller length. 3. Yes, but you need to do some calculations Commented Nov 10, 2024 at 23:55

1 Answer 1

1

This is relatively straight-forward with ffmpeg:

ffmpeg -i input -ss 00:10:07 -to 01:28:57 -vf "fade=in:start_time=00:10:07:duration=1,fade=out:start_time=01:28:57:duration=1" -c:a copy output

Add the video encoder options to your linking.

Please note that this will re-compress the entire video. If processing power is the bottle-neck, you can (in theory) apply the fades separately and then concatenate around the main part.


Assuming your input is x264 in mp4 with 30 fps with I-frames every 10 seconds and libx264 produces a byte-stream that is reasonably compatible to what the input is made out of, you can try this:

# fist, find the I-frames (this does not do anything, you need to look at it yourself)
ffprobe -v error -skip_frame nokey -show_entries frame=pts_time -select_streams v -of csv=p=0 input.mp4 

# for the fade-in, using the to specifier I move the end time of the segment right in front of the next I-frame (I know the I-frame's presentation time from the prior command)
ffmpeg -i input.mp4 -ss 00:10:07 -to 00:10:9.96666666 -vf "fade=in:start_time=$((10*60+7)):duration=1" -c:v libx264 -crf 18 -c:a copy fade_in_part.mp4

# for the fade-out, I use an accurate seek with the ss after the input. the to is where the fade shall end. the fade's start_time is absolute.
ffmpeg -i input.mp4 -ss 01:28:50 -to 01:28:58 -vf "fade=out:start_time=$((1*3600+28*60+57)):duration=1" -c:v libx264 -crf 18 -c:a copy fade_out_part.mp4

# for the main part, I use I-frame seeks (with ss and to before the input)
ffmpeg -ss 00:10:10 -to 01:28:49.96666666 -i input.mp4 -c copy main_part.mp4

# now put the three parts together
ffmpeg -f concat -safe 0 -i file_list.txt -c copy output.mp4

Where file_list.txt contains:

file 'fade_in_part.mp4'
file 'main_part.mp4'
file 'fade_out_part.mp4'

This way, you will write the main part twice, making the I/O the bottleneck.

I regard this method as finicky and hard to get right.

6
  • I think the fade in/out times need to be relative to the "cut" video, not the original ... at least, I've always done it that way Commented Nov 11, 2024 at 2:24
  • Thanks for the quick answer. I will try this out and then get back. I guess this was my problem; trying to trim and fade with -c copy at the same time. I believe you could put the second solution into a script that is called with a easier command. Lets see how this goes. -- Or it is really better to just use two commands. One to trim and copy and the other one to fade. Commented Nov 11, 2024 at 6:34
  • The first example produces the following error: [fade @ 0x7f9fe9706780] Value 10.000000 for parameter 'type' out of range [0 - 1] Last message repeated 1 times [fade @ 0x7f9fe9706780] Error setting option type to value 10. [Parsed_fade_0 @ 0x7f9fe9706d00] Error applying options to the filter. [AVFilterGraph @ 0x7f9feb200c40] Error initializing filter 'fade' with args 'in:start_time=00:10:07:duration=1' Error reinitializing filters! Failed to inject frame into filter network: Result too large Error while processing the decoded data for stream #0:0 Conversion failed! Trying to fix. Commented Nov 11, 2024 at 19:52
  • @GaryU.U.Unixuser Oh, you are very right. The documentation of the fade filter indeed says start_time wants seconds, not a time-stamp. I tested this with a short video that was not even a minute long so that was not an issue for me. I updated the answer with an inline bash-calculation that turns hours, minutes and seconds into seconds. Commented Nov 11, 2024 at 22:56
  • @JaromandaX I tried this and it works this way. I think the fade in/out times are relative to the selected video segment when -ss is used before the -i. Commented Nov 11, 2024 at 22:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.