Created
April 2, 2014 07:57
-
-
Save maddanio/9929780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def start_postprocessing_daemon(source_dir, target_dir, segments_suffix): | |
ensure_directory(target_dir) | |
def callback(monitored_path, event_path, event, discovered_through): | |
# logic always postprocesses the file before the current one, means last one will be left | |
if event_path.endswith("."+segments_suffix): | |
filename = os.path.basename(event_path) | |
counter = int(filename[7:14]) | |
if counter == 0: return | |
lastfilename = "segment%07i." % (counter - 1) + segments_suffix | |
inputpath = source_dir + os.path.sep + lastfilename | |
outputpath = target_dir + os.path.sep + lastfilename | |
return_code1 = subprocess.call(["ffmpeg","-i",inputpath,"-y","-vcodec","copy","-movflags","faststart",outputpath],stdout=open(os.devnull, 'wb'),stderr=open(os.devnull, 'wb')) | |
if return_code1 == 0: | |
os.remove(inputpath) | |
monitor = fsmonitor.get_fsmonitor()(callback) | |
monitor.daemon = True | |
monitor.start() | |
monitor.add_dir(source_dir,fsmonitor.FSMonitor.CREATED) | |
return monitor | |
def monitor_segments_and_extract_posters(segments_dir,segments_suffix,poster_suffix,quality=15): | |
"""note: suffixes are without dot""" | |
print "starting monitor on %s for segments with suffix %s making posters with suffix %s" % ( segments_dir, | |
segments_suffix, | |
poster_suffix ) | |
def callback(monitored_path, event_path, event, discovered_through): | |
if event_path.endswith("."+segments_suffix): | |
poster_path = event_path[:-(1+len(segments_suffix))] + "." + poster_suffix | |
success = False | |
while not success: | |
time.sleep(0.1) | |
start = time.clock() | |
return_code = subprocess.call(["ffmpeg","-i",event_path,"-ss","00:00:00","-f","image2","-vframes","1","-q:v",str(quality),poster_path], | |
stdout=open(os.devnull, 'wb'), | |
stderr=open(os.devnull, 'wb')) | |
success = return_code == 0 | |
print "extracted poster_frame from \"%s\" to \"%s\" in %f return code was %i" % (event_path, | |
poster_path, | |
time.clock() - start, | |
return_code) | |
monitor = fsmonitor.get_fsmonitor()(callback) | |
monitor.daemon = True | |
monitor.start() | |
monitor.add_dir(segments_dir,fsmonitor.FSMonitor.CREATED) | |
return monitor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment