diff --git a/.gitignore b/.gitignore index f3ee611..feb4467 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ *.pyc *.mp4 -*example.json diff --git a/README.md b/README.md index f887150..e950e89 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,10 @@ This splits `big_video_file.mp4` into 10 chunks. Each chunk will be suffixed wit ## Splitting videos into unequal chunks -In order to create unequal chunks of a video, you'll need to create ***manifest.json*** : +In order to create unequal chunks of a video, you'll need to create ***manifest.json***. + + +***manifest.json*** ```json @@ -38,6 +41,20 @@ Afterwards run: This splits `big_video_file.mp4` into 2 video files, video1.mp4 and video2.mp4. The video1.mp4 is a 34 seconds clip, starting from 0:00 to 0:34 of the `big_video_file.mp4`. + +Alternatively, you can use a ***manifest.csv*** file to accomplish the task above. + +***manifest.csv***: + +```CSV + +start_time,length,rename_to +0,34,video1 +35,22,video2 + +``` + + #### Manifest Options * start_time - number of seconds into the video or start time diff --git a/examples/manifest.csv b/examples/manifest.csv new file mode 100644 index 0000000..4793771 --- /dev/null +++ b/examples/manifest.csv @@ -0,0 +1,3 @@ +start_time,length,rename_to +0,34,video1 +35,22,video2 diff --git a/examples/manifest.json b/examples/manifest.json new file mode 100644 index 0000000..f92327b --- /dev/null +++ b/examples/manifest.json @@ -0,0 +1,17 @@ +[ + { + "start_time": 0, + "length": 34, + "rename_to": "video1" + }, + { + "start_time": 35, + "length": 22, + "rename_to": "video2.mp4" + }, + { + "start_time": 50, + "end_time": "00:01:20", + "rename_to": "video3.mp4" + } +] diff --git a/ffmpeg-split.py b/ffmpeg-split.py index 86b0b16..b98ff84 100644 --- a/ffmpeg-split.py +++ b/ffmpeg-split.py @@ -1,5 +1,6 @@ #!/usr/bin/env python +import csv import subprocess import re import math @@ -17,47 +18,53 @@ def split_by_manifest(filename, manifest): raise SystemExit try: - with open(manifest, "r") as manifest_file: - config = json.load(manifest_file) + with open(manifest) as manifest_file: + manifest_type = manifest.split(".")[-1] + if manifest_type == "json": + config = json.load(manifest_file) + elif manifest_type == "csv": + config = csv.DictReader(manifest_file) + else: + print "Format not supported. File must be a csv or json file" + raise SystemExit + + split_cmd = "ffmpeg -i '"+filename+"' -vcodec copy " + split_count = 1 + split_error = [] + try: + fileext = filename.split(".")[-1] + except IndexError as e: + raise IndexError("No . in filename. Error: " + str(e)) + for video_config in config: + split_str = "" + try: + split_start = video_config["start_time"] + split_length = video_config.get("end_time", None) + if not split_length: + split_length = video_config["length"] + filebase = video_config["rename_to"] + if fileext in filebase: + filebase = ".".join(filebase.split(".")[:-1]) + + split_str += " -ss " + str(split_start) + " -t " + \ + str(split_length) + \ + " '"+ filebase + "." + fileext + \ + "'" + print "About to run: "+split_cmd+split_str + output = subprocess.Popen(split_cmd+split_str, + shell = True, stdout = + subprocess.PIPE).stdout.read() + except IndexError as e: + print "############# Incorrect format ##############" + print "The format of each json array should be:" + print "{start_time: , length: , rename_to: }" + print "#############################################" + print e + raise SystemExit except Exception as e: print e raise SystemExit - split_cmd = "ffmpeg -i '"+filename+"' -vcodec copy " - split_count = 1 - split_error = [] - try: - fileext = filename.split(".")[-1] - except IndexError as e: - raise IndexError("No . in filename. Error: " + str(e)) - - for video_config in config: - split_str = "" - try: - split_start = video_config["start_time"] - split_length = video_config.get("end_time", None) - if not split_length: - split_length = video_config["length"] - filebase = video_config["rename_to"] - if fileext in filebase: - filebase = ".".join(filebase.split(".")[:-1]) - - split_str += " -ss " + str(split_start) + " -t " + \ - str(split_length) + \ - " '"+ filebase + "." + fileext + \ - "'" - print "About to run: "+split_cmd+split_str - output = subprocess.Popen(split_cmd+split_str, - shell = True, stdout = - subprocess.PIPE).stdout.read() - except IndexError as e: - print "############# Incorrect format ##############" - print "The format of each json array should be:" - print "{start_time: , length: , rename_to: }" - print "#############################################" - print e - raise SystemExit - def split_by_seconds(filename, split_length):