Skip to content

Instantly share code, notes, and snippets.

@rohitrangan
Created October 5, 2012 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rohitrangan/3841212 to your computer and use it in GitHub Desktop.
Save rohitrangan/3841212 to your computer and use it in GitHub Desktop.
Combining multiple mp4's to become a single mp4.

A program to convert any given number of mp4's to a single mp4

Requirements:

  • ffmpeg compiled with libx264 and libvorbis. Even ffmpeg without libx264 and libvorbis will do with minor changes to the source code.
  • Python 2.7 (Tested on Python 2.7.1, running on Mac OSX Lion)
  • A shell containing the 'cat' command. (Preferably the BASH Shell)

Precautions:

  • Make sure that you have ample free space on your drive as the conversion from mp4 to mpg increases the file size by a large factor.
  • It is discouraged to merge more than 3-4 mp4's at a time.

Instructions:

  • Copy the code into an empty text file and give it a name, say "JoinMP4.py"
  • Execute chmod u+x /PATH/TO/JoinMP4.py in the BASH shell, giving the path to the created JoinMP4.py.
  • Execute the python script by ./JoinMP4.py
#!/usr/bin/env python
import sys
import time
from subprocess import check_output
for i in range(1, len(sys.argv)):
print sys.argv[i],
fname = "Part" + str(i) + ".mpg"
print fname
check_output(["ffmpeg", "-i", sys.argv[i], "-sameq", fname])
print "Converted all mp4's to mpg's. Now combining them into a single file."
check_output("cat Part*.mpg > ./Final.mpg", shell=True)
print "Removing all the intermediate files..."
time.sleep(3)
check_output("rm Part*.mpg", shell=True)
print "Now converting mpg back to mp4."
print "The size may be smaller than the original mp4 files.",
print "This is because of a change in the audio and video codec."
time.sleep(3)
check_output(["ffmpeg", "-i", "Final.mpg", "-vcodec", "libx264", "-acodec", "libvorbis", "-sameq", "Final.mp4"])
time.sleep(3)
print "Removing the last remaining mpg file..."
check_output("rm Final.mpg", shell=True)
print "Finished merging the files. The output file is Final.mp4"
@divyalakhwani31
Copy link

I'm getting the following error -

ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Option 'sameq' was removed. If you are looking for an option to preserve the quality (which is not what -sameq was for), use -qscale 0 or an equivalent quality factor option.
Failed to set value '1' for option 'sameq': Invalid argument
Error parsing global options: Invalid argument
Traceback (most recent call last):
File "demo.py", line 46, in
check_output(["ffmpeg", "-i", '/tmp/prev.mp4', "-sameq", fname1])
File "/usr/lib/python3.6/subprocess.py", line 356, in check_output
**kwargs).stdout
File "/usr/lib/python3.6/subprocess.py", line 438, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['ffmpeg', '-i', '/tmp/prev.mp4', '-sameq', 'Part2.mpg']' returned non-zero exit status 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment