How to merge/mix/overlay multiple videos, with time-shifts? [duplicate] - audio

This question already has answers here:
FFmpeg - Overlay one video onto another video?
(1 answer)
How to overlay 2 videos at different time over another video in single ffmpeg command?
(1 answer)
FFmpeg - Multiple videos with 4 areas and different play times
(1 answer)
Closed 2 years ago.
How to merge/mix/overlay multiple videos, with time-shifts?
Example: I'd like to take:
4 seconds of 1.mp4 and it should start at 0'10" in the output video
8 seconds of 2.mp4 and it should start at 0'11" in the output video
16 seconds of 3.mp4 and it should start at 0'12" in the output video
They will be overlaying/overlapping themselves; example: if 1.mp4 is 400x400, 2.mp4 is 200x200, and 3.mp4 is 100x100 pixels, then at 0'12" we will see the 3 videos at the same time, overlapping each other.
ffmpeg -t 4 -i 1.mp4
-t 8 -i 2.mp4
-t 16 -i 3.mp4
-filter_complex [0][1][2]overlay[out]
-map [out]
out.mp4 -y
I see there is a overlay t parameter, but how to specify a different starting-timestamp for each video?
It seems we cannot use -itsoffset for multiple sources at the same time: https://trac.ffmpeg.org/ticket/1349

Related

Cut a video in between key frames without re-encoding the full video using ffpmeg?

I would like to cut a video at the beginning at any particular timestamp, and it need to be precise, so the nearest key frame is not good enough.
Also, these videos are rather long - an hour or longer - so I would like to avoid re-encoding this altogether if possible, or otherwise only re-encode a minimal fraction of the total duration. Thus, would like to maximise the use of -vcodec copy.
How can I accomplish this using ffmpeg?
NOTE: See scenario, and my own rough idea for a possible solution below.
Scenario:
Original video
Length of 1:00:00
Has a key frame every 10s
Desired cut:
From 0:01:35 through till the end
Attempt #1:
Using -ss 0:01:35 -i blah.mp4 -vcodec copy, what results is a file where:
audio starts at 0:01:30
video also starts at 0:01:30
this starts both the audio and the video too early
using -i blah.mp4 -ss 0:01:35 -vcodec copy, what results is a file where:
audio starts at 0:01:35,
but the video is blank/ black for the first 5 seconds,
until 0:01:40, when the video starts
this starts the audio on time,
but the video starts too late
Rough idea
(1) cut 0:01:30 to 0:01:40
re-encode this to have new key frames,
including one at the target time of 0:01:35
then cut this to get the 5 seconds from 0:01:35 through 0:01:40
(2) cut 0:01:40 through till the end
without re-encoding, using -vcodec copy
(3) ffmpeg concat the first short clip (the 5 second one)
with the second long clip
I know/ can work out the commands for (2) and (3), but am unsure about what commands are needed for (1).
List timestamps of key frames:
ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries frame=pkt_pts_time -of csv=p=0 input.mp4
It will output something like:
0.000000
2.502000
3.795000
6.131000
10.344000
12.554000
16.266000
...
Let's say you want to delete timestamps 0 to 5, and then stream copy the remainder. The closest following key frame is 6.131.
Re-encode 5 to 6.131. Ensure the input and output match attributes and formats. For MP4 default settings should do most of the work, assuming H.264/AAC, but you may have to manually match the profile.
ffmpeg -i input.mp4 -ss 5 -to 6.131 trimmed.mp4
Make input.txt for the concat demuxer:
file 'trimmed.mp4'
file 'input.mp4'
inpoint 6.131
Concatenate:
ffmpeg -f concat -i input.mp4 -c copy output.mp4
try
ffmpeg -i src.mp4 -vcodec copy -reset_timestamps 1 -map 0 out.mp4
or
ffmpeg -i src.mp4 -vcodec copy -reset_timestamps 1 -map 0 src_.m3u8
which generates hls playlists

Using ffmpeg to split MP3 file to multiple equally sound length files

How to use the command line tool ffmpeg on Windows to split a sound file to multiple sound files without changing the sound properties same everything each one is fixed 30 seconds length. I got this manual example from here:
ffmpeg -i long.mp3 -acodec copy -ss 00:00:00 -t 00:00:30 half1.mp3
ffmpeg -i long.mp3 -acodec copy -ss 00:00:30 -t 00:00:30 half2.mp3
But is there a way to tell it to split the input file to equally sound files each one is 30 seconds and the last one is the remaining what ever length.
You can use the segment muxer.
ffmpeg -i long.mp3 -acodec copy -vn -f segment -segment_time 30 half%d.mp3
Add -segment_start_number 1 to start segment numbering from 1.

ffmpeg concat drops audio frames

I have an mp4 file and I want to take two sequential sections of the video out and render them as individual files, later recombining them back into the original video. For instance, with my video video.mp4, I can run
ffmpeg -i video.mp4 -ss 56 -t 4 out1.mp4
ffmpeg -i video.mp4 -ss 60 -t 4 out2.mp4
creating out1.mp4 which contains 00:00:56 to 00:01:00 of video.mp4, and out2.mp4 which contains 00:01:00 to 00:01:04. However, later I want to be able to recombine them again quickly (i.e., without reencoding), so I use the concat demuxer,
ffmpeg -f concat -safe 0 -i files.txt -c copy concat.mp4
where files.txt contains
file out1.mp4
file out2.mp4
which theoretically should give me back 00:00:56 to 00:01:04 of video.mp4, however there are always dropped audio frames where the concatenation occurs, creating a very unpleasant sound artifact, an audio blip, if you will.
I have tried using async and -af apad on initially creating the two sections of the video but I am still faced with the same problem, and have not found the solution elsewhere. I have experienced this issue in multiple different use cases, so hopefully this simple example will shed some light on the real problem.
I suggest you export segments to MOV with PCM audio, then concat those but with re-encoding audio.
ffmpeg -i video.mp4 -c:a pcm_s16le -ss 56 -t 4 out1.mov
...
and then
ffmpeg -f concat -safe 0 -i files.txt -c:v copy concat.mp4

ffmpeg leaves audio gap when concatening videos

I am trying to cut a video in 2 parts then reassembling with ffmpeg but the final output has a small audio glitch right where the segments meet. I am using the following command to split the video 1.mp4 in 2 parts:
ffmpeg -i 1.mp4 -ss 00:00:00 -t 00:00:02 -async 1 1-1.mp4
and
ffmpeg -i 1.mp4 -ss 00:00:02 -t 00:00:02 -async 1 1-2.mp4
Once I have the 2 parts I am concatening them back together with:
ffmpeg -f concat -i files.txt -c copy output.mp4
files.txt is correctly listing both files. Can anyone point me to where the problem might be?
Thanks
The glitch is likely due to the audio priming sample showing up in between.
Since you're re-encoding the segments, you can do this in one command:
ffmpeg -i 1.mp4 -filter_complex
"[0]trim=duration=2[v1];[0]trim=2:4,setpts=PTS-STARTPTS[v2];
[0]atrim=duration=2[a1];[0]atrim=2:4,asetpts=PTS-STARTPTS[a2];
[v1][a1][v2][a2]concat=n=2:v=1:a=1[v][a]"
-map "[v]" -map "[a]" output.mp4
I had the same problem for about 3 weeks.
just merge the mp3 files using sox
sox in1.mp3 in2.mp3 in3.mp3 out.mp3
When I used concat with FFMPEG it made 12.5ms (I saw them on using Audacity) audio gaps. (I don't know why)
Maybe for your case it'll be better to extract the audio and video to two separate files using ffmpeg, merge them (video using FFMPEG and audio using sox) then put the files together into one container (mp4) file

ffmpeg clip audio interval with starting and end time

I am trying to clip an MP3 between two starting points, like starting at 10 seconds and ending at 16 seconds (time interval of 6 seconds).
I am using this command:
ffmpeg -ss 10 -i input.mp3 -t 6 output.mp3
The resulting output.mp3 contains the 6 seconds that I specified followed by 8 or 9 seconds of empty audio. Is there something wrong with my command?
Edit:
ffmpeg -ss 10 -t 6 -i input.mp3 output.mp3 says -t is not an input option, keeping it for the next output; consider fixing your command line. and gives me a file that's got 8 seconds of audio starting from 10s and then some 9 or 10 seconds of silence.
ffmpeg -ss 10 -to 16 -i input.mp3 output.mp3 produces a file that is twice the length of the original - basically the same audio file repeated again.\
Testing the output:
I used Quicktime and it has silent audio at the end. The description of the output file in finder says like 14 seconds. When I use VLC, it plays for the correct 6 seconds and stops, even though its duration in the file browser in VLC says 14. My MPlayer doesn't work properly. I also did the preview audio in Finder, and it plays the 6 seconds properly and then stops. But the round seeker bar of the MP3 didn't reach the end. And it also says 14 seconds instead of 6.
My goal is to stream this 6 second file through a REST API to the front end. I want the user to be able to download this file properly. Ideally it won't have inconsistent metadata (14 seconds instead of 6).
For me both
ffmpeg -ss 10 -t 6 -i input.mp3 output.mp3
or
ffmpeg -ss 10 -i input.mp3 -t 6 output.mp3
work OK, just 6 seconds of audio. Here's the mplayer output (last line):
A: 5.8 (05.7) of 6.0 (06.0) 0.5%
Also
ffmpeg -ss 10 -to 16 -i input.mp3 output.mp3
work the same way. I use ffmpeg version 1.2.4. I guess your ffmpeg is somehow "broken" or the input file is somehow (report a bug in either case).
You may try the other answer with mp3cut from portforwardpodcast or
sox input.mp3 output.mp3 trim 10 6
ffmpeg - Trim audio file without re-encoding
Use ffmpeg to trim an audio file without re-encoding it.
Trim starting from 10 seconds and end at 16 seconds (total time 6 seconds)
ffmpeg -i input.mp3 -ss 10 -t 6 -acodec copy output.mp3
Trim from 00:02:54.583 to the end of the file
ffmpeg -i input.mp3 -ss 00:02:54.583 -acodec copy output.mp3
Trim from 00:02:54.583 for 5 minutes (300 seconds)
ffmpeg -i input.mp3 -ss 00:02:54.583 -t 300 -acodec copy output.mp3
I've had great success with both CBR and VBR mp3 files using mp3cut.
mp3cut -o output.mp3 -t 00:10-00:16 input.mp3
http://manpages.ubuntu.com/manpages/lucid/man1/mp3cut.1.html

Resources