Last active
July 19, 2023 15:54
-
-
Save arpruss/067db7bb41a792c0ed2f870f94036fa1 to your computer and use it in GitHub Desktop.
Revisions
-
arpruss revised this gist
Jul 19, 2023 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,8 +14,8 @@ def getDuration(filename): return float(duration[0])*3600+float(duration[1])*60+float(duration[0]) def trySplit(filename,parts): print("Trying to split %s into %d parts." % (filename,parts)) duration = getDuration(filename) print("Original duration: %g sec." % duration) splitSize = int(math.ceil(duration / parts)) print("Aiming at %d sec parts." % splitSize) @@ -54,6 +54,7 @@ def help(): sys.exit(0) args = sys.argv[1:] while args: if not args[0].startswith("-"): break @@ -71,7 +72,7 @@ def help(): if not args: help() name = args[0] size = os.stat(name).st_size -
arpruss revised this gist
Jul 19, 2023 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,11 +14,11 @@ def getDuration(filename): return float(duration[0])*3600+float(duration[1])*60+float(duration[0]) def trySplit(filename,parts): print("Trying to split into %d parts." % parts) duration = getDuration(sys.argv[1]) print("Original duration: %g sec." % duration) splitSize = int(math.ceil(duration / parts)) print("Aiming at %d sec parts." % splitSize) position = 0 nameParts = os.path.splitext(filename) for i in range(parts): @@ -33,7 +33,7 @@ def trySplit(filename,parts): cmd += ["-ss", "%d" % start] if i + 1 < parts: cmd += ["-to", "%d" % position] outname = "%s-part%02d%s" % (nameParts[0],i+1,nameParts[1]) cmd += ["-i", filename, "-c", "copy", outname] print(cmd) try: @@ -76,7 +76,7 @@ def help(): size = os.stat(name).st_size if size <= maxSize: print("Size is already %d, which is good enough!" % size) sys.exit(0) partCount = int(math.ceil((float(size) / maxSize))) -
arpruss revised this gist
Jul 19, 2023 . 1 changed file with 29 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -25,6 +25,8 @@ def trySplit(filename,parts): start = position if i > 0: start -= overlapSeconds if start < 0: start = 0 position += splitSize cmd = ["ffmpeg"] if i > 0: @@ -45,14 +47,39 @@ def trySplit(filename,parts): print("Too big!") return False return True def help(): print("python videosplitter.py [--max maxByteSize] [--overlap overlapSeconds] filename") print("(Note: need to have ffprobe and ffmpeg on the path.)"); sys.exit(0) args = sys.argv[1:] while args: if not args[0].startswith("-"): break if args[0] == "--": args = args[1:] break if args[0] == "--max": maxSize = int(args[1]) args = args[2:] elif args[0] == "--overlap": overlapSeconds = int(args[1]) args = args[2:] else: help() if not args: help() name = args[0] size = os.stat(name).st_size if size <= maxSize: print("Size is %d, which is good enough" % size) sys.exit(0) partCount = int(math.ceil((float(size) / maxSize))) while not trySplit(name,partCount): partCount += 1 -
arpruss revised this gist
Jul 19, 2023 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -52,7 +52,7 @@ def trySplit(filename,parts): print("Size is %d, which is good enough" % size) sys.exit(0) partCount = int(math.ceil((float(size) / maxSize))) while not trySplit(sys.argv[1],partCount): partCount += 1 -
arpruss created this gist
Jul 19, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ import os import sys import math import subprocess maxSize = 999999999 overlapSeconds = 6 def getDuration(filename): pipe = subprocess.Popen(["ffprobe", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) for line in pipe.stderr: if line.strip().startswith("Duration: "): duration = line[12:].split(",")[0].split(":") return float(duration[0])*3600+float(duration[1])*60+float(duration[0]) def trySplit(filename,parts): print("Trying to split into %d parts" % parts) duration = getDuration(sys.argv[1]) print("Duration: %g sec" % duration) splitSize = int(duration / parts) print("aiming at sizes of size %d sec" % splitSize) position = 0 nameParts = os.path.splitext(filename) for i in range(parts): start = position if i > 0: start -= overlapSeconds position += splitSize cmd = ["ffmpeg"] if i > 0: cmd += ["-ss", "%d" % start] if i + 1 < parts: cmd += ["-to", "%d" % position] outname = "%s-part%02d%s" % (nameParts[0],i,nameParts[1]) cmd += ["-i", filename, "-c", "copy", outname] print(cmd) try: os.unlink(outname) except: pass subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() size = os.stat(outname).st_size print("Size: %d" % size) if size > maxSize: print("Too big!") return False return True size = os.stat(sys.argv[1]).st_size if size <= maxSize: print("Size is %d, which is good enough" % size) sys.exit(0) partCount = int(math.ceil((size*1.05 / maxSize))) while not trySplit(sys.argv[1],partCount): partCount += 1