How to join avi files + python automation code

I downloaded videos from rapidshare.com, but like most large file size videos there, the videos I got were split into parts. This is because rapidshare.com has a file size upload limit of 250 MB AFAIK.

So I needed some way to join my avi files which I got from rapidshare.com. I think I could’ve lived with the fact that I can just let my avi videos be, without joining file1.avi.part1 to file1.avi.part2 together, for example. All I would’ve done is create a playlist of my split avi files and play them consecutively using MPlayer or VLC for example. I wouldn’t really care if there was a 1 second gap between the playback of file1.avi.part1 and file1.avi.part2. The problem was, I can only play file1.avi.part1 and not file1.avi.part2. This was a real pain since I wanted to watch the whole video/s.

The solution? Home grown in Linux (^)__(^)

Well, actually we need the

cat

command and we need

mencoder

installed. These shouldn’t be a problem nowadays.

For those out there who only want to join a few videos together, the first step is to issue this simple command:

$cat file1.avi.part1 file1.avi.part2 > file1.avi

where file1.avi is the temporary file which combines file1.avi.part1 and file1.avi.part2. But we’re not done yet, as the audio and video might not sync coming from file1.avi.part1 to file1.avi.part2. You can use

cat

on as many files as you want or on as many as your PC can accommodate btw.
What we do is this:

$ mencoder -forceidx -oac copy -ovc copy file1.avi -o file1_final.avi

in case you don’t trust me, you can always consult your man page (^)__(^)
The previous command synchronizes the audio and the video of file1.avi by streamcopying its audio and video into file1_final.avi.
And that should be it! Now you (and me) have a single contiguous file which you (or me) can watch.
But for users out there who mind all the typing, since they’ve got several avi files to join together, I’ve made a python program for you guys:

#! /usr/bin/python

try:
	import commands, os, sys, string, getopt
except ImportError, msg1:
	exit(msg1)

def help_message():
    print '''This program joins split avi files. The file names of the split avi files to be joined should be in the manner:
	(filename1).avi.part1, (filename1).avi.part2,...
	or
	(filename1).avi.001, (filename1).avi.002,...
	The point is, the filenames are separated by '.' from their file (avi) type extension. The final combined video has the format (filename1)_final.avi.
	This program also assumes there are at least 2 split files you want to join in a single file. Otherwise, it will just copy the single avi file into (filename1)_final.avi.
	A MEncoder installation is required.
    Options:
	-h		displays this help message
	--file=		expects a file containing the list of avi files to be joined
	--version	displays Python version

       e.g. To join avi files in the current directory, listed in split_files.txt:
	 join_avi.py --file=split_files.txt'''
    sys.exit(0)

def getFilenames(split_list):
	file = open(split_list, "r")
	lines = file.readlines()
	try:
		i=True
		for filename in lines:
			namae = filename.split(".")
			if i == True:
				os.system("cat "+namae[0]+"* > _"+namae[0]+"_.avi")
				os.system("mencoder -forceidx -oac copy -ovc copy _"+ namae[0]+"_.avi -o _"+namae[0]+"_final.avi")
				os.system("rm _"+namae[0]+"_.avi")
				i = False
			if i == False:
				if os.system("ls "+namae[0]+"*"):
					i = False
				else:
					i = True
	except ValueError, msg:
		exit(msg)
	file.close()

def isempty(self) :
    return (len(self) == 0) 

try:
	options, xarguments = getopt.getopt(sys.argv[1:],'ha', ['file=','version'])
except getopt.error:
	print "Error: You tried to use an unknown option or the argument for an option that requires it was missing. Try `options.py -h' for more information."
	sys.exit(0)

if isempty(options):
	help_message()

for a in options[:]:
    if a[0] == '-h':
        help_message()

for a in options[:]:
	if a[0] == '--file' and a[1] != '':
		getFilenames(a[1])
		options.remove(a)
		break
	elif a[0] == '--file' and a[1] == '':
		print '--file expects an argument'
		sys.exit(0)

for a in options[:]:
    if a[0] == '--version':
        print 'join_avi.py version 0.5'
        sys.exit(0)

If you have comments/suggestion/questions, please feel free to post and ask them.

You may also download the python source code from above here=> join_avi.odt. Just remember to save it as a text file with a .py extension for the Python interpreter. Unfortunately, WordPress only accepts jpg, jpeg, png, gif, pdf, doc, ppt, and odt for file uploads so I wasn’t able to upload the join_avi.py code.

Just be sure to remember that indention is very important to Python. If you don’t want to download the source code, you can view this page’s source code (in Firefox, right-click page then click View Page Source) and just copy the original and correct code with proper indentions, to a text editor, and save it with a .py extension.

Advertisements

Tags: , , , ,

14 Responses to “How to join avi files + python automation code”

  1. Maki Says:

    Just in case you are using windows, you can try a freeware software, HJSplit.
    It can merge .00* type of files AFAIK. I’m not sure if you can use it with wine though.

  2. f241vc15 Says:

    Actually, HJsplit is also available in Linux (^)__(^) Unfortunately, I found it a bit hard to use/understand, hehehe…That’s why I created my own instead, which I certainly do understand! (^)____(^) Thanks for the tip anyway!

  3. Craig Says:

    lxsplit (the Linux version of HJsplit) is the best choice for such rapidshare files: they have been split using HJsplit, so it seems to make sense to use the same program to rejoin them. And the only command needed is ‘lxsplit -j filename.avi.001’

    Now joining large videos which come in a 2 cd format is a different matter, and your guide is perfect for that :o)

  4. f241vc15 Says:

    Thanks for some more info Craig!
    Yes I did get some of the example video files I used from rapidshare.com. I think I did try lxsplit some time ago, but to my disappointment (can’t really remember why) I wasn’t able to run it correctly. One probable reason was because lxsplit might have required other packages. Whereas mine needed only the ‘cat’ tool and an MEncoder installation.
    What do you know? I guess I really am old fashioned! (^)__(^) sometimes I’d rather make my own programs rather than rely on others I can’t understand somehow (^)___(^)
    Thanks for the tip again, and perhaps in the future I’ll try lxsplit again!

  5. tim Says:

    I just joined some files using both cat and lxsplit, and both produced identical results. I used md5sum to verify this. Then I ran mencoder as described, obviously producing a different file. I’m at work so I can’t actually check the video itself – it would be rather interesting if mencoder was actually needed because then everyone (except us) would get sync problems.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: