Skip to content

Instantly share code, notes, and snippets.

@error454
Last active March 19, 2016 19:24
Show Gist options
  • Save error454/cbde98bcfbafa010b42f to your computer and use it in GitHub Desktop.
Save error454/cbde98bcfbafa010b42f to your computer and use it in GitHub Desktop.
Some misc video scripts made for processing recorded camera footage and live video streams
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
F1::Run % "D:\Path\to\carve.bat " + Explorer_GetSelection()
Explorer_GetSelection(hwnd="") {
hwnd := hwnd ? hwnd : WinExist("A")
WinGetClass class, ahk_id %hwnd%
if (class="CabinetWClass" or class="ExploreWClass" or class="Progman")
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd==hwnd)
sel := window.Document.SelectedItems
for item in sel
ToReturn .= item.path " "
return Trim(ToReturn,"`n")
}
python "C:\path\to\carveVideo.py" %*
# Given two thumbnails generated by makeThumbs.py extract the video
# segment represented by them.
# TODO: Make slightly more accurate.
#
import subprocess
import os
import sys
import glob
import shutil
ThumbLength = 30
def SplitFile(file, fromMark, to):
newFilename = file.split(".mkv")[0] + "_" + str(fromMark) + "-" + str(to) + ".mkv"
cutFrom = str(max(fromMark - 2, 0) * ThumbLength)
cutTo = str((to - fromMark) * ThumbLength)
print "Cutting from: " + cutFrom + " for " + cutTo + " sec"
args = ["ffmpeg",
"-i", file,
"-ss", cutFrom,
"-t", cutTo,
"-vcodec", "copy",
newFilename]
child = subprocess.call(args)
# a batch file will call:
#"c:\Python27\python.exe" carveVideo.py %*
if len(sys.argv) > 2:
cutArg1 = sys.argv[1]
cutArg2 = sys.argv[2]
path = os.path.dirname(cutArg1)
print "Got path: " + path
#find the mkv
filenames = glob.glob(path + "\*.mkv")
file = ""
if len(filenames) > 0:
file = filenames[0]
else:
exit(0)
print "Got mkv: " + file
num1 = int(cutArg1.split(".png")[0].split("thumb")[1])
num2 = int(cutArg2.split(".png")[0].split("thumb")[1])
finalNum1 = num1
finalNum2 = num2
if num1 > num2:
finalNum1 = num2
finalNum2 = num1
print "Got num1: " + str(finalNum1) + " Num2: " + str(finalNum2)
SplitFile(file, finalNum1, finalNum2)
# Utilize the livestreamer utility to download a list of streams (i.e. youtube, twitch)
# The input file can be updated in real-time to startup new streams
# Livestreamer outputs in .flv format.
import os
import sys
import subprocess
import random
from time import sleep
MaxVideos = 10
Streams = {}
def GetFilename(stream):
filename = stream.split("http://10.0.0.1:/")[1]
basename = filename.split("/")[0]
testName = "incoming/" + basename + ".flv"
iter = 0
while os.path.isfile(testName):
iter = iter + 1
testName = "incoming/" + basename + str(iter) + ".flv"
return testName
def StartStream(stream):
global Streams
if len(Streams) >= MaxVideos:
return False
filename = GetFilename(stream)
#May need to adjust quality parameter here "best" depending on what your stream returns
#This should probably be parameterized per stream.
args = ["livestreamer", "-o", filename, stream, "best"]
child = subprocess.Popen(args)
Streams[stream] = child
return True
def FindEmptyStreams():
global Streams
for stream in Streams:
if Streams[stream].poll() != None:
return True
return False
def CloseEmptyStreams():
global Streams
if FindEmptyStreams():
NewStreams = {}
for stream in Streams:
if Streams[stream].poll() == None:
NewStreams[stream] = Streams[stream]
Streams = NewStreams
def EndlessDownload(file):
while 1:
with open(file) as f:
data = f.read()
for line in data.splitlines():
if "http" in line:
# this is flaky and hackish, it can take awhile to close streams
CloseEmptyStreams()
sleep(0.2)
CloseEmptyStreams()
if not line in Streams:
StartStream(line)
# don't kill the server
sleep(random.randrange(1,5))
#Only need to iterate the video list every minute and a half
sleep(90)
if len(sys.argv) > 1:
if os.path.isfile(sys.argv[1]):
EndlessDownload(sys.argv[1])
# Process a folder of .flv files. For each flv found:
# * Convert it to mkv
# * Create a thumbnail every 30 seconds in folder
# * Move mkv to folder
# * Delete original flv
import subprocess
import os
import sys
import glob
import shutil
def ConvertToMKV(file):
filename = os.path.join(folder, file.split(".")[0] + ".mkv")
args = ["ffmpeg",
"-i", file,
"-vcodec", "copy",
filename]
child = subprocess.call(args)
return filename
if len(sys.argv) > 1:
path = sys.argv[1]
filenames = glob.glob(path + "/*.flv")
for file in filenames:
folder = file.split(".flv")[0]
if not os.path.isdir(folder):
os.makedirs(folder)
newMKV = ConvertToMKV(file)
args = ["ffmpeg",
"-i", newMKV,
"-probesize", "1000000",
"-analyzeduration", "10000000",
"-vf", "fps=1/30",
folder + "/thumb%04d.png"]
child = subprocess.call(args)
if os.path.isfile(newMKV):
os.remove(file)
shutil.move(newMKV, os.path.join(path, folder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment