Skip to content

Instantly share code, notes, and snippets.

@arpruss
Last active July 19, 2023 15:54
Show Gist options
  • Select an option

  • Save arpruss/067db7bb41a792c0ed2f870f94036fa1 to your computer and use it in GitHub Desktop.

Select an option

Save arpruss/067db7bb41a792c0ed2f870f94036fa1 to your computer and use it in GitHub Desktop.

Revisions

  1. arpruss revised this gist Jul 19, 2023. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions videosplitter.py
    Original 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 into %d parts." % parts)
    duration = getDuration(sys.argv[1])
    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
  2. arpruss revised this gist Jul 19, 2023. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions videosplitter.py
    Original 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)
    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)
    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,nameParts[1])
    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 %d, which is good enough" % size)
    print("Size is already %d, which is good enough!" % size)
    sys.exit(0)

    partCount = int(math.ceil((float(size) / maxSize)))
  3. arpruss revised this gist Jul 19, 2023. 1 changed file with 29 additions and 2 deletions.
    31 changes: 29 additions & 2 deletions videosplitter.py
    Original 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(sys.argv[1]).st_size
    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(sys.argv[1],partCount):
    while not trySplit(name,partCount):
    partCount += 1
  4. arpruss revised this gist Jul 19, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion videosplitter.py
    Original 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((size*1.05 / maxSize)))
    partCount = int(math.ceil((float(size) / maxSize)))

    while not trySplit(sys.argv[1],partCount):
    partCount += 1
  5. arpruss created this gist Jul 19, 2023.
    58 changes: 58 additions & 0 deletions videosplitter.py
    Original 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