PyYUV4MPEG
HORRIBLE! DEPRECATED! TRY MY
PYREX VERSION INSTEAD!
No future development, maintenance or support is planned for this
swig-based version. All effort is now going in to the Pyrex version.
Python and C++ bindings for
MJPEGtools 'yuv4mpeg' API.
Simple. Closely follows the C API for yuv4mpeg, and adds a high-level
class called 'Stream'. Documentation included.
Download: here.
Here's a minimal example:
import sys
import yuv4mpeg
# create a yuv4mpeg stream
stream = yuvmpeg.Stream()
# bind it to stdin and stdout
stream.setFiles(sys.stdin, sys.stdout)
# grab header from input, send to output
stream.copyHeader()
# now move all the frames
while True:
try:
stream.readFrame()
except EOFError:
break
stream.writeFrame()
Here's a filter in Python that actually has some use:
"""
yuvexcerpt.py - a simple tool for extracting an excerpt of video from
standard input.
"""
import sys, os, traceback, getopt
import yuv4mpeg
progname = sys.argv[0]
args = sys.argv[1:]
def getarg():
return args.pop(0)
def usage(msg=None):
"""
spit usage info and quit with error
"""
if msg:
sys.stderr.write(msg+"\n")
sys.stderr.write("\n".join([
"Usage: %s [-ss starttime] [-t endtime]" % progname,
"Type '%s -h' for help" % progname,
"",
]))
sys.exit(1)
def help():
"""show help info and quit with no error"""
sys.stderr.write("\n".join([
"%s: Cut an excerpt of a stream, based on time" % progname,
"Options:",
" -ss - set start time in seconds, default 0.0 (from beginning)",
" -t - length of excerpt to pass through - default -1 (to end)",
" -h, --help - display this help",
"Valid time format is [[hh:]mm:]ss[.ss]",
"",
]))
sys.exit(0)
def parsetime(arg):
"""
parse a given time value - refer help()
"""
try:
parts = arg.split(":")
hours = mins = secs = 0.0
secs = float(parts.pop())
if parts:
mins = int(parts.pop())
if parts:
hours = int(parts.pop())
except:
usage("invalid time format" % progname)
return hours * 3600 + mins * 60 + secs
def runPipe(startTime, length):
"""
transfer an excerpt of yuv from stdin to stdout
"""
stream = yuv4mpeg.Stream()
stream.copyHeader()
curTime = 0.0
if length > 0:
endTime = startTime + length
else:
endTime = 1000000000
# get framerate, convert from ratio to float
fps = stream.getFrameRate()
fps = 1.0 * fps.n / fps.d
# and derive seconds per frame
framePeriod = 1.0 / fps
sys.stderr.write("fps=%f period=%f\n" % (fps, framePeriod))
# the big loop
while (curTime < endTime):
try:
stream.readFrame()
except EOFError:
break
curTime += framePeriod
if curTime >= startTime and curTime < endTime:
stream.writeFrame()
def main():
"""
the front end
"""
# set default parameters
startTime = 0.0
length = -1
# fetch/validate args
while args:
arg = getarg()
if arg == '-ss':
startTime = parsetime(getarg())
elif arg == '-t':
length = parsetime(getarg())
elif arg in ['-h', '--help']:
help()
else:
usage()
try:
runPipe(startTime, length)
except KeyboardInterrupt:
sys.stderr.write("Terminated by user\n")
return
if __name__ == '__main__':
main()