Author Topic: Python script for converting video's using ffmpeg  (Read 865 times)

Offline rayman2

  • Full Member
  • ***
  • Posts: 71
Python script for converting video's using ffmpeg
« on: April 22, 2012, 09:56:43 AM »
I wrote this script a few weeks ago to automatically convert video's to the right format for my DLNA-enabled tv. It creates directories for new "series". Videoconvertpath is the path where you will store your video's waiting to be converted, videooutputpath is the path where the converted video's will be stored. Any directory after videoconvertpath is treated as a new "series", so it creates a new directory in the outputpath. For example, /mnt/opslag/server/mediaconvert/Scrubs/ScrubsSe01Ep02.mkv would be converted to /mnt/opslag/server/media/Scrubs/ScrubsSe01Ep02.avi 3000kb video-bitrate, mp3 audio codec, xvid video, etc.
Subtitles in the same directory as the input with the same name-extension+.srt are copied as well. So if the file /mnt/opslag/server/mediaconvert/Scrubs/ScrubsSe01Ep02.srt exists, it gets copied to /mnt/opslag/server/media/Scrubs/ScrubsSe01Ep02.srt.
You might need to change the script a bit to work for your situation, for example, if you don't want to specify a vtag(fourcc), you should change "call(['ffmpeg','-i',i,'-vcodec',videocodec,'-vtag', videotag,'-acodec',audiocodec,'-ac','2','-b:v',videobitrate,'-y',path.join(outputpath,outputname)+'.'+outputcontainer])" to "call(['ffmpeg','-i',i,'-vcodec',videocodec,'-acodec',audiocodec,'-ac','2','-b:v',videobitrate,'-y',path.join(outputpath,outputname)+'.'+outputcontainer])", and the same with the other call line(there are two, just delete ",-vtag,videotag").
You can specify (one or multiple) files, directories, or a comination of the two.
Code: (python) [Select]
#!/usr/bin/env python2
#Change this:
videoconvertpath='/mnt/opslag/server/mediaconvert/'
videooutputpath='/mnt/opslag/server/media/Video/'
videocodec='mpeg4'
videotag='xvid'
videobitrate='3000k'
audiocodec='libmp3lame'
outputcontainer='avi'
supportedcontainers=['mkv','avi','mov','ts']
#End of variables, start of code
for arg in argv[1:]:
arg=path.abspath(arg)
print(arg)
if not path.exists(arg):
print("Path doesn't exist. Exiting...")
exit(1)
if not path.splitext(arg)[1][1:] in supportedcontainers:
print("Unsupported file. Exiting...")
exit(1)
if path.isfile(arg):
series=path.dirname(arg.split(videoconvertpath)[1])
elif path.isdir(arg):
series=path.dirname(arg.split(videoconvertpath)[1]+'/')
outputpath=path.join(videooutputpath,series)
if series!='' and not path.exists(outputpath):
call(['mkdir','-p', '%s'%outputpath])
if path.isfile(arg):
outputname=path.splitext(path.basename(arg))[0]
if path.isfile(path.splitext(arg)[0]+'.srt'):
call(['cp',path.splitext(arg)[0]+'.srt',path.join(outputpath,outputname)+'.srt'])
call(['ffmpeg','-i',arg,'-vcodec',videocodec,'-vtag', videotag,'-acodec',audiocodec,'-ac','2','-b:v',videobitrate,'-y',path.join(outputpath,outputname)+'.'+outputcontainer])
elif path.isdir(arg):
files=listdir(arg)
todo=[]
for i in files:
i=path.abspath(path.join(arg,i))
if path.splitext(i)[1][1:] in supportedcontainers:
todo.append(path.join(arg,i))
outputname=path.splitext(path.basename(i))[0]
if path.isfile(path.splitext(i)[0]+'.srt'):
call(['cp',path.splitext(i)[0]+'.srt',path.join(outputpath,outputname)+'.srt'])
call(['ffmpeg','-i',i,'-vcodec',videocodec,'-vtag', videotag,'-acodec',audiocodec,'-ac','2','-b:v',videobitrate,'-y',path.join(outputpath,outputname)+'.'+outputcontainer])

(Moved this post from "Software discussion" because I actually meant to place it here, but somehow ended up posting it there)

Online Just17

  • PCLinuxOS Tester
  • Super Villain
  • *******
  • Posts: 10665
  • MLUs Forever!
Re: Python script for converting video's using ffmpeg
« Reply #1 on: April 22, 2012, 02:24:04 PM »
I saw it there .....  and now here  :D

I haven't tried it .......  yet ......  nice one  ;)
MLUs rule the roost!

Linux XPS 3.4.38-pclos1.bfs  64 bit
Intel Core2 Quad CPU Q9450 @ 2.66GHz
4 GB RAM
MCP51 High Def Audio
GeForce GTX 550 Ti
PHILIPS  ‎DVD+-RW DVD8701
‎Logitech ‎BT Mini-Receiver
Afatech DTT