Use FFMPEG to combine different MP4s with srt into one file - ffmpeg

So...probably a very basic question for those of you familiar with FFMPEG (I'm really not). I know that you can combine multiple videos into one using FFMPEG, but what about if each video has its own srt file, saved separately in a 'subs' folder and NOT included in the video itself?
Is it possible for FFMPEG to also combine the srt files into a single one (and recalculate the timestamps), and then merge this into the final, combined video? If so, what would the command be?
For example, I have video1.mp4 and video2.mp4. They have corresponding sub1.srt and sub2.srt. When video1.mp4 and video2.mp4 are merged, the timestamps for sub2.srt will, of course, be out of sync now and need to be corrected by adding the duration of video1.mp4 to the individual timestamps (i.e., if video1 is 30 seconds long, and the first subtitle in sub2.srt appears at the 2-second mark, then after the combination, it should now appear at the (30+2)=32-second mark, and so on.
If it helps, all the files are mp4, and have the same dimensions (720p).

While there might be a (complicated) way to concatenate the srt files first, the easiest way is to combine pairs of video and text first, and then concatenate the resulting container files.
Copy everything from video1.mp4 and add subtitles from sub1.srt
# Assuming English for subtitle language
ffmpeg -i video1.mp4 -i sub1.srt -c copy -c:s mov_text -metadata:s:s:0 language=en -metadata:s:s:0 title=English 1.mp4
-c copy will copy everything that might be in video1.mp4, and -c:s mov_text will format the text stream from sub1.srt into subtitles for mp4 (mov_text). The result will be written to 1.mp4.
Repeat the same command for all the other video-subtitles pairs.
Create a text file (f.e. chapters.txt) with the resulting file names
file 1.mp4
file 2.mp4
file 3.mp4
…
Concatenate the resulting container files listed in the text file
ffmpeg -f concat -safe 0 -i chapters.txt -c copy everything.mp4
See ffmpeg's concatenate demuxer
There are other ffmpeg commands that can also deal with different dimensions, mentioned in the docs.

For whatever reason I had to explicitly copy the video, audio, and subtitle streams individually on step 4, otherwise I ended up with silent videos. So my step 4 looked like this:
ffmpeg -f concat -safe 0 -i chapter.txt -c:v copy -c:a copy -c:s copy everything.mp4

Related

Concatenating/Splicing overlapping video clips with ffmpeg

I'm trying to concatenate multiple short .mp4 video clips from a security camera. The camera records short clips, with a few seconds on either end of a timespan when motion is detected. For example, two minutes of video will often be broken up into four ~35 second clips, with the first/last few seconds of each clip being duplicative of the last/first few seconds of the previous/next clip.
I simply concatenate the clips together using the ffmpeg concat demuxer, as described here: How to concatenate two MP4 files using FFmpeg?, with
(echo file 'first file.mp4' & echo file 'second file.mp4' )>list.txt
ffmpeg -safe 0 -f concat -i list.txt -c copy output.mp4
Or else I transcode them into intermediate MPEG-2 transport streams, which I can then concatenate with the file-level concat protocol, as described here: https://trac.ffmpeg.org/wiki/Concatenate#protocol, with
ffmpeg -i "first file.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i "second file.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
But either way, the resulting video (output.mp4) jumps backward in time a few seconds every half-minute or so because of the duplicated frames.
I want to throw out the duplicate frames, and tie the clips together based on timestamps to achieve smooth playback of the concatenated full-length video. I'd strongly prefer to do this on Windows with ffmpeg if possible. Surely this has been done before, right? Are there timestamps in the .mp4 files that I can use to determine how much overlap there is, and then splice at the proper point-in-time? And if so, how do I read them, how do I splice at an exact point in time, and how do I get around the KeyFrames issue if I can splice at the exact point in time?

How do I use FFMPEG with filter_complex to combine/join a full folder of 40+ audio files into one long mp3?

I have a full directory of 40-50 audio files that I am trying to "concat" into one long mp3.
I was able to do this just testing around with 2 files using the command shown below, but I need an easy way to do this with a script that I can make if I have a folder of many files with complicated file names. This is something I'm going to be doing frequently so if I had a script or something I could use quickly that would be most helpful.
ffmpeg -i a.webm -i b.webm -filter_complex "[0:a] [1:a] concat=n=2:v=0:a=1 [a]" -map [a] -c:a mp3 testfull.mp3
The simplest solution is to use the concat demuxer, but all inputs must be the same format and have the same attributes (sample rate, sample format, channel layout).
printf "file '%s'\n" *.webm > input.txt
ffmpeg -f concat -safe 0 -i input.txt -map 0:a output.mp3
-safe 0 is only needed if your input file names contain special characters.

concat multiple mp4 files with an effect

I have a folder that contains n video files of mp4 format: 00001.mp4, 00002.mp4, etc...
They are all the same resolution, fps, and dimensions.
What I need is a way to concat them together into one large mp4 file, but to have a specific effect between every two subsequent videos.
For example a flash fade.
Here are some examples: https://biteable.com/blog/video-transitions-effects-examples/
I have access to ffmpeg and looking for some sample commands.
Thanks,
This tutorial here will provide you with a starting point: link
What you will need to do is use:
ffmpeg -i slide.mp4 -y -vf fade=in:0:30 slide_fade_in.mp4
and:
ffmpeg -i slide_fade_in.mp4 -y -vf fade=out:120:30 slide_fade_in_out.mp4
Then combine the files with the effects. In the tutorial, they have setup the script for the combinations.

Concat multiple video and audio files with ffmpeg

I have an array of audio and video clips, where each audio clip has a 1:1 correlation with it's video clip. The encoding of each video and each audio clip are the same. How can I concat all of the audio clips, and all the video clips, then merge them together to output a video. As of now I only figured out how to merge 1 audio clip with 1 video clip:
$ ffmpeg -i video_1.webm -i audio_1.wav -acodec copy -vcodec copy output.mkv
Update
I just came across mkvmerge would this possibly be a better option?
If all the files are encoded with the same codecs then it's easy to do. First merge the audio and video files as you have already done so each pair of files is contained in one mkv. Then you can concatenate them with the concat demuxer like this:
ffmpeg -f concat -i <(printf "file '%s'\n" ./file1.mkv ./file2.mkv ./file3.mkv) -c copy merged.mkv
or:
ffmpeg -f concat -i <(printf "file '%s'\n" ./*.mkv) -c copy merged.mkv
You could also list one file per line in a text file called mergelist.txt (or whatever you want to call it), i.e.:
file './file1.mkv'
file './file2.mkv'
file './file3.mkv'
Then use that as the input, a la:
ffmpeg -f concat -i mergelist.txt -c copy merged.mkv
This is by far the easiest and fastest way to do what you want since it won't re-encode the files, just line them up one after another.
You can find your answer here in this old question:
Concatenate two mp4 files using ffmpeg
This answer is not restricted to MP4. But it will depend on the file format you wanna concatenate!
Once you have your new VIDEO file and AUDIO file, to merge them together:
ffmpeg -i AUDIO -i VIDEO -acodec copy -vcodec copy OUTPUT

Join multiple flv with ffmpeg

i am trying to join two flv files using -concat option in ffmpeg-1.1 . I have created a list named mylist.txt and placed two flv files into it, but the problem i am facing is that the output of first file in mylist.txt streams perfect but video breaks into pieces when it comes to the second file. Looks like i am using the wrong options with -concat, please guide me for suitable commands with -concat option. Following are the commands and configurations i am using for transcoding .flv files:-
mylist.txt
file '/root/1.flv'
file '/root/2.flv'
ffmpeg command :-
ffmpeg -re -f concat -i /root/mylist.txt -acodec copy -vcodec copy output.flv
Following link is the output of ffmpeg command :-
http://pastebin.com/P3uaUDEd
Unless the 2 files were encoded the same (and even if they were it could still be a problem) you would need to transcode the audio and video so that things like time stamps, bitrates, resolutions and other codec internals are correct in both streams. Change you acodec copy and vcodec copy to the codecs of your choice (x264 and mp3/aac are good choices).

Resources