Answers.Body: <p>IT will help merge <code>movie1..3.mp4</code></p>
  <p>For cut can add <code>-ss 00:00:XX</code> <code>-to 00:00:XX</code> for -i file</p>
  <pre><code>ffmpeg -y -hide_banner  \
   -i &quot;movie1.mp4&quot; -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate1.ts
  
  ffmpeg -y -hide_banner  \
   -i &quot;movie2.mp4&quot; -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate2.ts
  
  ffmpeg -y -hide_banner  \
   -i &quot;movie3.mp4&quot; -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate3.ts
  
  ffmpeg -y  -hide_banner \
   -i &quot;concat:intermediate1.ts|intermediate2.ts|intermediate3.ts&quot; \
   -i wind.wav \
   -c:v copy -c:a aac -b:a 64k \
   &quot;mergedmovie.mp4&quot;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-11T23:57:00.787Z
Answers.LastActivityDate: 2023-01-11T23:57:00.787Z
Answers.UserDisplayName: lexxai
Answers.Body: <p>Well, this doesn't solve the problem of the re-encoding, but at least it is an handy interface. Just use it (in Linux) with <code>vcat -i inputfile1,inputfile2[start-end],... -o &lt;outputfile&gt;</code> (it needs python3 and the <code>moviepy</code> package)</p>
  
  <pre><code>#!/usr/bin/python3
  
  import sys, getopt, re
  
  def printerror(errormsg):
    print("*** vcat - concatenate video segments using moviepy ***\n")
    print("ERROR:", errormsg,"\n")
    print("Usage: vcat -i inputfile1,inputfile2[start-end],... -o &lt;outputfile&gt;")
    print("Optional start and end points should be given in seconds. If files have spaces should be quoted (e.g. \"input file.mp4[5-30]\").")
  
  try:
   from moviepy.editor import VideoFileClip, concatenate_videoclips
  except ImportError:
   printerror("You don't seem to have moviepy installed. Install it with `pip install moviepy`.")
   exit(1)
  
  def main(argv):
      inputfiles_arg = ''
      outputfile = ''
      try:
          opts, args = getopt.getopt(argv,"hi:o:",["input=","output="])
      except getopt.GetoptError as err:
          printerror(str(err))
          sys.exit(2)
      for opt, arg in opts:
          if opt == '-h':
              printerror("")
              sys.exit()
          elif opt in ("-i", "--input"):
              inputfiles_arg = arg
          elif opt in ("-o", "--output"):
              outputfile = arg
      if outputfile =='':
          printerror("Output file not specified")
  
      iFiles = inputfiles_arg.split(',')
  
      clips=[]
      for iFile in iFiles:
          subclip = re.search(r"\[([0-9\-\.]+)\]", iFile)
          if subclip is None:
              clips.append(VideoFileClip(iFile))
          else:
              dims = subclip.group(1).split('-')
              if len(dims) != 2:
                  printerror("If a specific segment of a file is specified, this should be given as [startseconds-endseconds]")
              iFile = iFile.replace("["+subclip.group(1)+"]",'')
              clips.append(VideoFileClip(iFile).subclip(float(dims[0]),float(dims[1])))
  
      f = concatenate_videoclips(clips)
      f.write_videofile(outputfile)
  
  
  if __name__ == "__main__":
     main(sys.argv[1:])
  </code></pre>
  
  <p>Disclaimer: I don't use Python by a while, so sure there could be better approaches.. bdw it's really nice to be able to do what you want to do in a few hours because of the enormous documentation you can find on the net about python..</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-02T12:53:27.440Z
Answers.LastActivityDate: 2019-01-02T13:57:03.927Z
Answers.LastEditDate: 2019-01-02T13:57:03.927Z
Answers.UserDisplayName: Antonello
Body: <p>I have some screencast video files in mp4 container (h.264/mp3 codecs) that I want at times merge (i.e. C.mp4 =  A.mp4+B.mp4) or at times cut removing some intervals (e.g. D.mp4 = A.mp4[0,320{seconds}]+A.mp4[325,340]+C.mp4).</p>
  
  <p>I am now using kdenlive, it woks but it re-encode everything and it seems a bit too much for such a task.</p>
  
  <p>Is there a simpler way to do that without re-encoding (even, and perhaps preferred, command line or stable Python/Julia/R/.. library way) ?</p>
  
  <p>I am thinking at something like PDFtk for the pdfs :-))</p>
  
  <p>PS: Following suggestions from <a href="https://video.stackexchange.com/questions/23149/ffmpeg-cut-same-video-to-multiple-pieces">a related question</a> I did try <code>ffmpeg -ss 00:00:05 -to 00:00:10 -i test1.mp4 test2.mp4</code> but I got the error:</p>
  
  <blockquote>
    <p>Option to (record or transcode stop time) cannot be applied to input
    url test1.mp4 -- you are trying to apply an input option to an output
    file or vice versa. Move this option before the file it belongs to.</p>
  </blockquote>
  
  <p>I would like to be able to specify several start/stop times from a single or perhaps multiple input files.</p>
  
  <p>EDIT: More I look, more I find complicate solutions. My files are exactly the same codec/container/resolution..</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-09-18T20:04:53.577Z
Comments.Text: [Avidemux](https://superuser.com/a/638736/267534) can do that
Comments.UserDisplayName: Suncatcher
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-12-10T10:15:05.720Z
Comments.Text: Your ffmpeg version is too old. Try to install the latest version.
Comments.UserDisplayName: jdhao
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-10T05:36:51.873Z
Comments.Text: Replying to an old question, but for anyone coming across this - try the answer on this similar question here https://video.stackexchange.com/questions/30269/edit-small-portion-of-a-compressed-video-without-losing-quality/30272#30272
Comments.UserDisplayName: Peter Barton
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-04T21:39:40.453Z
Comments.Text: "PS: Following suggestions from a related question I did try ffmpeg -ss 00:00:05 -to 00:00:10 -i test1.mp4 test2.mp4 but I got the error:" Put the -to after the input. If still an error move the -ss after the input as well. i.e. ffmpeg -i input -ss -to...
Comments.UserDisplayName: kite
Tags: <ffmpeg><video-editor>
Title: How to cut and merge mp4 video segments without re-encoding?
UserDisplayName: Antonello
ThreadText: # How to cut and merge mp4 video segments without re-encoding?
  
  (Asked by: username_0 on 2019-01-02)
  
  I have some screencast video files in mp4 container (h.264/mp3 codecs) that I want at times merge (i.e. C.mp4 = A.mp4+B.mp4) or at times cut removing some intervals (e.g. D.mp4 = A.mp4[0,320{seconds}]+A.mp4[325,340]+C.mp4).
  
  I am now using kdenlive, it woks but it re-encode everything and it seems a bit too much for such a task.
  
  Is there a simpler way to do that without re-encoding (even, and perhaps preferred, command line or stable Python/Julia/R/.. library way) ?
  
  I am thinking at something like PDFtk for the pdfs :-))
  
  PS: Following suggestions from [a related question](https://video.stackexchange.com/questions/23149/ffmpeg-cut-same-video-to-multiple-pieces) I did try `ffmpeg -ss 00:00:05 -to 00:00:10 -i test1.mp4 test2.mp4` but I got the error:
  
  > Option to (record or transcode stop time) cannot be applied to input
  > url test1.mp4 -- you are trying to apply an input option to an output
  > file or vice versa. Move this option before the file it belongs to.
  
  I would like to be able to specify several start/stop times from a single or perhaps multiple input files.
  
  EDIT: More I look, more I find complicate solutions. My files are exactly the same codec/container/resolution..
  
  **Tags:** ffmpeg, video-editor
  
  > **Comments:**
  > **username_4**: "PS: Following suggestions from a related question I did try ffmpeg -ss 00:00:05 -to 00:00:10 -i test1.mp4 test2.mp4 but I got the error:" Put the -to after the input. If still an error move the -ss after the input as well. i.e. ffmpeg -i input -ss -to...  
  > **username_1**: [Avidemux](https://superuser.com/a/638736/267534) can do that  
  > **username_2**: Your ffmpeg version is too old. Try to install the latest version.  
  > **username_3**: Replying to an old question, but for anyone coming across this - try the answer on this similar question here https://video.stackexchange.com/questions/30269/edit-small-portion-of-a-compressed-video-without-losing-quality/30272#30272  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_5 on 2023-01-11. Score: 0
  
  IT will help merge `movie1..3.mp4`
  
  For cut can add `-ss 00:00:XX` `-to 00:00:XX` for -i file
  
  ```
  ffmpeg -y -hide_banner  \
   -i "movie1.mp4" -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate1.ts
  
  ffmpeg -y -hide_banner  \
   -i "movie2.mp4" -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate2.ts
  
  ffmpeg -y -hide_banner  \
   -i "movie3.mp4" -c copy -bsf:v h264_mp4toannexb -an -f mpegts \
   intermediate3.ts
  
  ffmpeg -y  -hide_banner \
   -i "concat:intermediate1.ts|intermediate2.ts|intermediate3.ts" \
   -i wind.wav \
   -c:v copy -c:a aac -b:a 64k \
   "mergedmovie.mp4"
  
  ```
  
  
  ---
  
  ### Answer by username_0 on 2019-01-02. Score: 0
  
  Well, this doesn't solve the problem of the re-encoding, but at least it is an handy interface. Just use it (in Linux) with `vcat -i inputfile1,inputfile2[start-end],... -o <outputfile>` (it needs python3 and the `moviepy` package)
  
  ```
  #!/usr/bin/python3
  
  import sys, getopt, re
  
  def printerror(errormsg):
    print("*** vcat - concatenate video segments using moviepy ***\n")
    print("ERROR:", errormsg,"\n")
    print("Usage: vcat -i inputfile1,inputfile2[start-end],... -o <outputfile>")
    print("Optional start and end points should be given in seconds. If files have spaces should be quoted (e.g. \"input file.mp4[5-30]\").")
  
  try:
   from moviepy.editor import VideoFileClip, concatenate_videoclips
  except ImportError:
   printerror("You don't seem to have moviepy installed. Install it with `pip install moviepy`.")
   exit(1)
  
  def main(argv):
      inputfiles_arg = ''
      outputfile = ''
      try:
          opts, args = getopt.getopt(argv,"hi:o:",["input=","output="])
      except getopt.GetoptError as err:
          printerror(str(err))
          sys.exit(2)
      for opt, arg in opts:
          if opt == '-h':
              printerror("")
              sys.exit()
          elif opt in ("-i", "--input"):
              inputfiles_arg = arg
          elif opt in ("-o", "--output"):
              outputfile = arg
      if outputfile =='':
          printerror("Output file not specified")
  
      iFiles = inputfiles_arg.split(',')
  
      clips=[]
      for iFile in iFiles:
          subclip = re.search(r"\[([0-9\-\.]+)\]", iFile)
          if subclip is None:
              clips.append(VideoFileClip(iFile))
          else:
              dims = subclip.group(1).split('-')
              if len(dims) != 2:
                  printerror("If a specific segment of a file is specified, this should be given as [startseconds-endseconds]")
              iFile = iFile.replace("["+subclip.group(1)+"]",'')
              clips.append(VideoFileClip(iFile).subclip(float(dims[0]),float(dims[1])))
  
      f = concatenate_videoclips(clips)
      f.write_videofile(outputfile)
  
  if __name__ == "__main__":
     main(sys.argv[1:])
  
  ```
  
  Disclaimer: I don't use Python by a while, so sure there could be better approaches.. bdw it's really nice to be able to do what you want to do in a few hours because of the enormous documentation you can find on the net about python..
  
  
  ---
  
  