Today I hadn't much to do, we had a shitload of snow..
I made some movies of the snow storm with my cellphone yesturday night. Today when I transfered them to my computer I noticed they where in 3gp format. Whatever it is, it wont play on Ubuntu. By chance I quickly found a solution on a Ubuntu forum, which looks like this (sorry I lost the URL):
mencoder file.3gp -ovc lavc -lavcopts vcodec=msmpeg4v2 \
-oac mp3lame -lameopts vbr=3 -o file.avi
Pretty neat. But there's now way I'm gonna remember all these parameters, let alone typing it for every files.
Having nothing better to do since my car is fucking stuck in the snow, I decided to code myself a small python utility to make the task less painfull and hopefully improve my Python skills because I'm still quite noob. So if you have comments or better way to achieve the same results please let me know :)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# py3gp2avi.py - 1.0
# MIT License (http://www.opensource.org/licenses/mit-license.php)
# (c) Maxime Haineault ~ haineault@gmail.com
# http://haineault.com/
# Installation:
# sudo ln -sT /path/to/py3gp2avi.py /usr/local/bin/py3gp2avi
# sudo chmod a+x /usr/local/bin/py3gp2avi
# usage:
# py3gp2avi
# py3gp2avi file.3gp
# py3gp2avi dir/
# py3gp2avi ~/video/
import os, shutil, logging as log
from optparse import OptionParser
# Meaningul & helpful error handling :)
try:
buf = os.popen('whereis mencoder', 'r')
path = buf.read().split()[1]
buf.close()
except IndexError:
log.error('Mencoder package is required.\nTry: sudo apt-get install mencoder')
if __name__ == '__main__':
def convert(path, file, dest=False):
out = '%s.avi' % os.path.join(os.path.split(file)[0], os.path.splitext(os.path.split(file)[1])[0])
log.info('converting: %s to %s' % (file, out))
buf = os.popen('%s %s -ovc lavc -lavcopts vcodec=msmpeg4v2 -oac mp3lame -lameopts vbr=3 -o %s'
% (path, file, out))
log.debug(buf.read())
buf.close()
parser = OptionParser('Usage: %prog [options] file|directory')
parser.add_option('-c', '--clean', action='store_true', help='Delete 3gp files after conversion')
parser.add_option('-d', '--debug', action='store_true', help='Show debug informations')
(options, args) = parser.parse_args()
loglevel = options.debug and log.DEBUG or log.INFO
log.basicConfig(level=loglevel, datefmt='%H:%M:%S',
format='[%(asctime)s %(levelname)s] %(message)s')
if len(args) == 1:
arg1 = os.path.expanduser(args[0])
else:
buf = os.popen('pwd')
arg1 = buf.read().rstrip('\n')
buf.close()
if os.path.exists(arg1):
if os.path.isfile(arg1):
convert(path, arg1)
if (options.clean):
os.remove(arg1)
else:
for f in sorted(os.listdir(arg1)):
if f.endswith('3gp'):
convert(path, os.path.join(arg1, f))
dir(options)
if (options.clean):
os.remove(os.path.join(arg1, f))
@dbr
Yeah, I guess.. But unfortunately, no.
Quite interestingly it worked fine in VLC player. However I find it more convenient to have my movies in AVI format.
An as I said, It was more an self exercise to learn Python. I'm sure there is plenty of more advanced, well coded, more documented open source projects to do the same task. But I like to code my tools and know how they works, I think it's a great way to learn :p
Nice one. Thx for sharing. I was actually thinking of writing one, i started a little but somehow left it :) I too have the same philosophy.. dont bother if something better is available out there.. if you want to learn something just go ahead and get your hands dirty.. that way u will get a lot more insights into everything you try.
thanks again Kiran
If memcoder can decode-and-reencode the file, chances are mplayer can play the .3gp file...?
permalink dbr ~ March 9, 2008 at 11:14 p.m.