Merge pull request #39 from carter300/master

Add output folder option
This commit is contained in:
c0decracker 2024-09-27 11:01:07 -07:00 committed by GitHub
commit 31fe165f28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View file

@ -78,6 +78,7 @@ start_time,length,rename_to
* -s or --split-size seconds to evenly split the videos
* -e or --extra extra optional options for ffmpeg, e.g. '-e -threads 8' to use 8 threads to speed up.
* -S or --split-filesize Split or chunk size in bytes (approximate). e.g -S 1048576 will split into files under 1 Megabyte
* -o or --output_dir set a output folder for split
#### Notes:

View file

@ -11,13 +11,14 @@ import subprocess
from optparse import OptionParser
def split_by_manifest(filename, manifest, vcodec="copy", acodec="copy",
def split_by_manifest(filename, manifest, output_dir=None, vcodec="copy", acodec="copy",
extra="", **kwargs):
""" Split video into segments based on the given manifest file.
Arguments:
filename (str) - Location of the video.
manifest (str) - Location of the manifest file.
output_dir (str) - Directory to save the split files.
vcodec (str) - Controls the video codec for the ffmpeg video
output.
acodec (str) - Controls the audio codec for the ffmpeg video
@ -28,6 +29,10 @@ def split_by_manifest(filename, manifest, vcodec="copy", acodec="copy",
print("File does not exist: %s" % manifest)
raise SystemExit
# Ensure the output directory exists
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(manifest) as manifest_file:
manifest_type = manifest.split(".")[-1]
if manifest_type == "json":
@ -44,6 +49,7 @@ def split_by_manifest(filename, manifest, vcodec="copy", acodec="copy",
fileext = filename.split(".")[-1]
except IndexError as e:
raise IndexError("No . in filename. Error: " + str(e))
for video_config in config:
split_args = []
try:
@ -55,8 +61,10 @@ def split_by_manifest(filename, manifest, vcodec="copy", acodec="copy",
if fileext in filebase:
filebase = ".".join(filebase.split(".")[:-1])
output_path = os.path.join(output_dir, filebase + "." + fileext) if output_dir else filebase + "." + fileext
split_args += ["-ss", str(split_start), "-t",
str(split_length), filebase + "." + fileext]
str(split_length), output_path]
print("########################################################")
print("About to run: " + " ".join(split_cmd + split_args))
print("########################################################")
@ -87,7 +95,7 @@ def ceildiv(a, b):
return int(math.ceil(a / float(b)))
def split_by_seconds(filename, split_length, vcodec="copy", acodec="copy",
def split_by_seconds(filename, split_length, output_dir=None, vcodec="copy", acodec="copy",
extra="", video_length=None, **kwargs):
if split_length and split_length <= 0:
print("Split length can't be 0")
@ -100,6 +108,10 @@ def split_by_seconds(filename, split_length, vcodec="copy", acodec="copy",
print("Video length is less then the target split length.")
raise SystemExit
# Ensure the output directory exists
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
split_cmd = ["ffmpeg", "-i", filename, "-vcodec", vcodec, "-acodec", acodec] + shlex.split(extra)
try:
filebase = ".".join(filename.split(".")[:-1])
@ -113,9 +125,9 @@ def split_by_seconds(filename, split_length, vcodec="copy", acodec="copy",
else:
split_start = split_length * n
split_args += ["-ss", str(split_start), "-t", str(split_length),
filebase + "-" + str(n + 1) + "-of-" +
str(split_count) + "." + fileext]
output_path = os.path.join(output_dir, f"{filebase}-{n+1}-of-{split_count}.{fileext}") if output_dir else f"{filebase}-{n+1}-of-{split_count}.{fileext}"
split_args += ["-ss", str(split_start), "-t", str(split_length), output_path]
print("About to run: " + " ".join(split_cmd + split_args))
subprocess.check_output(split_cmd + split_args)
@ -170,6 +182,12 @@ def main():
type="string",
action="store"
)
parser.add_option("-o", "--output-dir",
dest="output_dir",
help="Directory to save split files.",
type="string",
action="store"
)
parser.add_option("-v", "--vcodec",
dest="vcodec",
help="Video codec to use. ",