Join Videos Using FFMPEG: All 3 Concatenation Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello this is Stef! And welcome back so we   are here working with the Xubuntu 22.04 right  behind me and first of all I got to apologize   because of this long period without videos more  or less 50 days and but I decided to take a longer   vacation so here's some videos about the my period  spent okay so i guess yes no don't believe that yes yes and now we are talking about  concatenation so the operation of   joining more than one video all together  into a single longer video merging okay wrapped up the engine let's get started okay here we go so now we are in our  usual environment we have as data set   a set of 10 videos this is part of a unique  video where there is me counting from one   to ten so we show you you go with the  player and you play one two three four   etcetera etcetera until until 10. see later uh  the result so what we would like to do is to merge   all these videos together into one single video  so how can we do it with the fm pack fun pack   now i'm using fm pack 5.0.1 that is the latest  release and let's see that everything is working so here we go uh ready to be used we have  different approaches we have approaches   for files of the same type and files of different  types so if you would like to concatenate videos   with the same codec in the same container  there's no problem you can use an approach   and the other approach needs something else so for  the first approach we have two ways so when you   have videos with the same format and the same uh  codec uh we can use uh the concat de muxer or the   concat protocol so we see both of the approaches  if you need to work with them if you need   to concatenate videos of different formats and  different codecs there's the cone cat filter so   we got to use a filter at the moment no need to  worry we're going to explain everything about   the usage of the filter then later on there will  be another video for the usage of the filters in   general but for the moment bear with me we'll see  you later okay now it's time to use the demoxer   first of all what is a democracy the demuxer  is a series of particular elements included by   fmpeg that welcome the input streams so here the  documentation says that the mixers are configured   elements in fm pack that can read the multimedia  streams from particular type of file so to use the   concat the mixer first of all we need to create a  text file with the list of the input file we want   to concatenate so i will use spin vim yeah i  am i'm going up one level to the videos folder   and here we put the input files.txt so the name of  the file is arbitrary no need to put anything else   need to put any particular type of name and let's  open it i'm using the im no need to use a vam   it's not mandatory if you want to use another  text editor feel free if you want windows use   the notepad notepad plus plus it's a simple  text file so i will use vam now at the moment   i'm in comment mode i'll turn into insert  mode and here we are so what is the syntax   for the input files i want to put it um first  of all i got to take file and then the between   quotes um between sorry apostrophes we want to put  the name of the file so here we have video pieces to one dot mp4 then file dpo esis 02.84 okay so we finished and now we can  turn into common mode again and   you can save the video the text file so just  to check we have a cat videos input files input files dot txt and here we have all the list  one two three four five six seven eight nine ten   it's a bit tedious job so what i would like to  do is to create a script that is up for this uh   so let's see how to create the  script to make this a list of files   okay now let's get started with the creation of  the script and so this folder doesn't contain   anything so it's completely empty we're going to  create a new script file that takes all the input   files for the concatenation so we're going to use  bim for this and so game and list files dot sh   so first of all uh what i would like to do  is uh um so the core of the script itself   um is essentially we're going to take  the list of all the files in this   folder we put between a poster between  quotes to sanitize against the spaces   if you've got the folder with spaces which we  have because media pieces has a space in between   dot ext so here it says from all the files  in that folder with that extension do this so   i'm going to echo so i'm going to put in  the output and standard output this file   we have one at the beginning and dollar f then so moment this is the core we got to  set which is the folder and which is the extension   so um all the folder put folder equals  to the first command lineup parameter   and as extension the second command line parameter  so folder and extension here ready to be used um   we would like to sanitize um a bit the parameters  because uh right here um if we put something that   is not compliant the photo doesn't exist we end  up to an error which is something we don't want   uh first of all let me put the usual thing  that is in the beginning of the scripts and   so first of all we got to check if the folder  so if the first command line parameter exists so   if square brackets minus that minus  that says if the string is not is empty in this case if the string is  empty we're going to put the   default command line parameter so  this case we can put the videos folder on videos otherwise we've got to take this thing right here  so go to still put the upholster fees to avoid   things with spaces okay good   and you will put the condition where the  folder exists so um excuse me about to put   a message if the folder doesn't exist so we put  question mark minus d and the name of the folder remember space otherwise  there will be an error and   then echo the provided folder does you know exist and we exit with an error code that is a different  at zero so if this zero we end up we end the   program without any problems if we exit with one  it will end with an arrow and the end of the if   now we put another if for the extension that's  the same as before so if square brackets minus z   um in this case it's the  second comma line parameter um we put just in case it does it is not provided  we put any extensions we want to put any file   in the folder let's suppose that in the folder  there will be files with the same extension so   the demo works correctly then so the  extension would be an asterisk asterisk   means an extension otherwise  we put this condition here next   equals to the second one here finish and let's  hope everything works fine so let's end up   okay um now that the file is written uh what  we would like to do is to render the script   as an executable so go to invoke chmod um plus  x executable and let's files.sh so now we can   execute it without any problems and here we are  by default in the videos folder so input files.txt   is the only file that is in that folder there  is a folder with the files but in this case uh   the only file that is in that folder is  this what we would like to do is to set the   list sh with a fold that we set uh before so um  the video pieces folder is in home videos and   video pieces let's see if everything was fine and  it works fine so here we got all the list of files   so what we need to do we can either  um redirect it or copy paste but   the most direct thing is to invoke the  script and then redirect it into another file videos and here we have the files list 2.txt and go we don't see any output  but to change to the videos folder we have the file list and input  files uh well let me be consistent   and rename the files list into input pass2 here we go and here we have the input files enable  files too and they should match they have the same   files so now we are ready to concatenate  okay now we're working on windows and here   i'm going to do the same thing that we did  before so we're going to list all the files   uh in the folder um but in batch and not in  the shell so i'm going to create a new file   list files dot v80 for batch and we're going to  open it with the text editor i'm going to use   notepad plus plus so if you are comfortable with  other types feel free so now what we are going   to do is uh to so first of all set the core  command the command is something like this for i there is an iterator in the  folder we would like so um let's say something like this   first command line parameter slash  asterisk the second command line parameter to echo file and the name of the file in the iterators good um what we would like to do is okay first  of all let's say the echo off so we don't want   the uh annoying things in the um  as soon as we output the program   and we're going to take the command  line parameter so the first command   line parameter is the folder the second and  command line parameter is the extension so if the first command line parameter is  not m is empty so in this case i'm   going to save this empty we're going to  an error message so go to usage and if even the second common parameter there is an  extension is empty go to the usage good um let's put a nand right here and uh also here  we have the first i usage and then our end so echo error so this is the dr message did not set  all the common line line arguments properly echo we put a full stop so just to  say that we skip a line and echo zero of extension so this is the usage of uh  program uh let's quick check i think everything is   fine well the thing that we can put here instead  of the folder is like this and maybe it is not   taken so much so first of all let's put the quotes  and let's put it in the bonus format to put the thing in this way i'm sorry this is not dollar  this is a percent sorry i'm accustomed to the   uh unix ones and uh here we go um i think  it is working like this and so let's save   close open the command command  line okay now it's time to test our   script so we're going directly  into the videos folder videos sorry level up videos video pieces there we go and now we're going to take that  so users name and uh backstop list files and   rightly it says that there is an error good  because we didn't set the common electron meters   we put the current folder and then the mp4  that is uh the extension of the file that we   had before and here we have the list if you want  to create a desktop file you can use the same old   piping like in linux see users and files list we don't see any output here  but if we take the desktop desktop file everything should be working cd stop okay and add a list files sorry files list here we go the list of the files okay now it's  time to use a fm pack finally and now we are in   the home folder let me change to the folder where  fm peg is so first of all we invoke fm pack now   the demuxer needs to be chosen and the the  mixer needs uh is chosen using the minus   f flag we got the cone cat minus f cone cat you  got to put a flag before the input file in which   you in this case it is the text file that is  minus save zero what does it do it tells that uh   we want an unsafe environment so fmpeg by default  requires a safe environment for the comcat   so it requires uh only a limited set of characters  so numbers characters and a few punctuation so the   slash is not accepted so we want to accept the  slash since we have an absolute path and not a   relative path so -0 before the name of the file  otherwise by default it is set to one so 0 minus i   name of the file videos and in this  case we have both the files let's take   the second that is the one that we just  created so a bit more satisfaction and   then it's time to take the name of the  sorry the codec they want to use so   we already know what to do if you don't  remember there is a a link here in the um on top and let's take the copy so for me for  us uh it's okay to use the copy and finally the   output file so it is called captain fed videos  dot mkv so let me use mkb as a um the container   so i like the matraoshka container so much so we  use the mkv let's see if it works and it works   okay now it's time to analyze the video  and then playing it so you got the the analysis using them so the video now is almost 12 seconds long  and this is the video where counting so here one two three four five six seven eight nine ten  and here we go the second approach to conversion   is the concat protocol so the concat protocol as  a condition we require raw streams why because the   concat protocol concatenates the streams as they  are while the ones that we did before with the   concat de muxer we are going to concatenate files  why is that the function is a bit different we got   to think about the concatenation protocol as  the concatenation of files so use the concat   in the command line you're going to make a  concatenation so simple uh joining between one   one file and the other in the binary string  so the command is a similar to this um fm peg minus i in this case we got to  put uh in the minus i i'm calm cat   hold on this is the first  time that we see uh the colon   the types of input that we want to put here so  here we require raw streams so a typical file   for streams is a the ts so it is a the mpeg2ts  file so input one input two dot yes input three   dot yes x uh etc here we got to put the list  of all the files um we want to use minus c   copy or whatever codec you would like to use  then output.ts so in the end we're going to   create another type of stream like the input  one and this is the basics of the comment   to work with the concat protocol we need first of  all to prepare the files that we have so these are   the files that we used before with the previous  type of concatenation um now we try to concatenate   the video stream of these files first of all we  got to prepare in order to create a new stream so   first of all in the folder uh where we are so this  is uh my username i'm going to create a folder hey videos folder and the video pieces you will create the ts chunks okay so here what we're going to do is to invoke fm peg   minus i so this is the target is to create a np2  ts file so i'm going to take the input file that   is this one and then we're going to create  we're going to set the output parameters so   we're going to copy the stream so we want to  maintain the stream as it is and then change   the transform the format that is unpacked to ts so  to do this we need to invoke the bitstream filter   or short is a bsf uh we don't tell the  reasons in detail uh so if you would like   put a comment down below if you like the video  but it's important to make the video in the future   um this is essentially to welcome the stream  from the mp4 file into the mpeg-2 ts file so   here the stream is h2 the type of a  big stream filter is h264 mp4 to nxb mine minus f and pack ts this is the format of  the file as output and now we take the yes chunks   this case it is a zero one dot ts the string is done if we go in the folder  we show you directly we have the zero one   dot ts we have to do this job with all the files  if you would like it now we're going to take   a couple so going to create the zero one  and zero two so here i'm going to replace   the zero two with the zero two dot ts of  course we can automate with a um the script and also the zero three so let's try with the  first three just to show you i think it's working   uh of course there are also advanced things for  example to avoid intermediate files we can create   a pipe okay so now let's try to concatenate  the streams so to make the thing a bit easier   i'm going directly into the videos folder so  we avoid spaces and blah blah so the comment   is a bit easier so we're going to the folder  videos then video pieces and then ts chunks okay   then what i'm going to do is to invoke fm peg  in the location where i have it and it is in um   so now we are in videos um home  folder fm peg dash 5 0.1 um minus i i'm going to put between  quotes the concat column 0 1 ps 0 two dot ts and zero three dot gets   then we put the minus c copy and then  concatenate the delta kits and we have   a new file that is the concatenated ready to  be played so if we play it the normal player it should work c1 2 and 3. so now at this point  we can use the concatenation without any problems   so uh the one that i use the most is the first the  uh the mixer um there's one problem uh so you you   can use one or the other without any difference  so there will be a problem if you have videos of   different formats different containers different  codecs in that case we have to use either uh first   recompression so a first restreaming into another  file and then the concatenation the final one   or we got to use the filters so the filters  require the video to be at the same frame rate   with the same resolution and so in that  case we can work it's a bit more complex   but it allows you to use videos with  different nature so with codex formats etc   okay so the concat filter that by itself  it's a filter complex it's not a filter   we would like to do is to recap and recall some  knowledge from the stream mapping if you don't   remember if you're curious about what the stream  mapping is we use the same notation i put you   the video in the card here okay so uh how to  use the the field um now we're going to see   the fm tag we're going to invoke the fbac  command and we're going to put all the inputs   here after the minus high so a minus i input file  one minus i input file two minus i input file   three etc it's quite uh common to use so we got  to the zero one dot and p4 minus i l2 e4 minus i z3 dot mp4 minus i u4 dot mp4 okay uh so we take  the first four files we don't want to take the   whole video the whole thing otherwise it takes uh  quite a long time then after it we got to put the   minus filter complex what's the difference between  filter and filter complex so the filter complex   is a filter that accepts as input multiple files  while a filter except only a single single file   so in this case in the filter complex we're  going to accept so we go to map the streams   that we want in this case let me put the square  brackets so from the first file we take the first   video stream okay then for the same for the audio  so we take the video and the audio from the first   file then we do the same for the second so you  see that everything here is a zero starting so the   first is zero the second is one and the same with  this then we got to put the thing for the third   and two a zero and also three  b zero and three a zero good after this we got to put the cone cat this is  the filter concat we put this n that says that   the number of um   piles that we have so in this case we got to  concatenate four videos so n is equal to four and then we're going to see um the number of  streams the numbers of the types of streams   in this case we got to put a column v equals to  one and also a equals to one what does it mean   we're going to tell the software there will be  a single video stream and a single audio stream   then we're going to say the i'm going to  give a label to this video and this audio   so this will be the v outs and this will be  the a outs this means video video and audio out and then we're going to do a procedure  that is similar to the stream mapping so   let's take the map uh out v  sorry v out and then the man a out and then in the end finally we can take the  concatenated file uh in this case i'm going to   call it concatenated dot in this case we can  choose the string we want in this case the   mkb since uh um we're going to recompress  the font so we can choose the one we want   and here we go here i had a mistake i didn't  put wider complex instead of filter complex   okay and here we go so now you see that it  takes quite some more time because the video is   recompressed so just a second for it to finish and here we have a temporary mkb that's the time  to finalize and here we see that the video is is done one two three and four good okay so we've seen the concept of  concatenation we saw that it's the joining of   one video after the other in a unique   video output we saw the different ways we saw the  concat the mixer so the configuration in the input   to give the output file in the end so with the  minus f concat text file we also seen the way   to create this list of files in the text file  both in linux and in windows and we've seen   the concat demuxer and we've seen the filter  complex concat that gives the power to merge   videos from different containers and different  codecs this is the advantage the disadvantage is   that we cannot use the stream copy i personally  use the first method so the concave demuxer i first of all recompress the video in  the end if the videos are of different   streams uh so i i'm not allowed to use the conca  de mixer so i recompress and then i'll give the   video in the end but if you feel comfortable  with other types feel free to use them so uh   thanks thank you very much uh  for watching until this point   uh thank you very much for following me uh if  you don't please subscribe to my channel and i opened uh both tik tok and an instagram  channel so follow me you will find them in   the channel description and also the video  description across the description down below   and thank you very much for watching see you  next time with some other context regarding   fm video fm pack and much much more thank  you very much and see you next time ciao you
Info
Channel: Dev Stef
Views: 1,275
Rating: undefined out of 5
Keywords: ffmpeg, merge, join videos, ffmpeg tutorial, ffmpeg help, install ffmpeg, ffmpeg for videos, ffmpeg download, mkv, stream, ffmpeg windows, ffmpeg linux, linux, windows, ubuntu 22.04, ffmpeg windows 10, windows 10, ffmpeg join videos, ffmpeg join videos without re encoding, ffmpeg concatenate videos, ffmpeg concatenate videos without re encoding, concatenate videos with ffmpeg, concatenate videos ffmpeg, ffmpeg merge audio and video, concat, ffmpeg concat, mp4, how to install ffmpeg
Id: YeLQuXAz2og
Channel Id: undefined
Length: 39min 55sec (2395 seconds)
Published: Thu Aug 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.