Added csv support.

This commit is contained in:
Oshane Bailey 2015-09-20 12:16:20 -05:00
parent 2de78b03dc
commit e4ba28feae
5 changed files with 82 additions and 39 deletions

1
.gitignore vendored
View file

@ -1,3 +1,2 @@
*.pyc
*.mp4
*example.json

View file

@ -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

3
examples/manifest.csv Normal file
View file

@ -0,0 +1,3 @@
start_time,length,rename_to
0,34,video1
35,22,video2
1 start_time length rename_to
2 0 34 video1
3 35 22 video2

17
examples/manifest.json Normal file
View file

@ -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"
}
]

View file

@ -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: <int>, length: <int>, rename_to: <string>}"
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: <int>, length: <int>, rename_to: <string>}"
print "#############################################"
print e
raise SystemExit
def split_by_seconds(filename, split_length):