You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.3 KiB
100 lines
3.3 KiB
#!/usr/bin/env python3
|
|
|
|
## chmod +x vidsplit
|
|
## sudo cp vidsplit /usr/local/bin
|
|
## vidsplit --help
|
|
|
|
import os
|
|
import subprocess
|
|
import argparse
|
|
import sys
|
|
|
|
def get_file_extension(file_path):
|
|
"""Extract the file extension from the input file."""
|
|
return os.path.splitext(file_path)[1].lower()
|
|
|
|
def get_duration(input_file):
|
|
"""Get the total duration of the video in seconds."""
|
|
cmd = [
|
|
"ffprobe", "-i", input_file, "-show_entries", "format=duration",
|
|
"-v", "quiet", "-of", "default=noprint_wrappers=1:nokey=1"
|
|
]
|
|
try:
|
|
duration = float(subprocess.check_output(cmd, stderr=subprocess.PIPE).decode().strip())
|
|
return duration
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Error getting duration: {e}")
|
|
sys.exit(1)
|
|
|
|
def split_video(input_file, size_limit_mb, output_dir):
|
|
"""Split the video into chunks based on size limit."""
|
|
# Supported video extensions
|
|
valid_extensions = {'.mp4', '.avi', '.mkv', '.mov'}
|
|
file_ext = get_file_extension(input_file)
|
|
|
|
if file_ext not in valid_extensions:
|
|
print(f"Error: Unsupported file type '{file_ext}'. Supported types: {', '.join(valid_extensions)}")
|
|
sys.exit(1)
|
|
|
|
# Convert size limit to bytes
|
|
size_limit = size_limit_mb * 1000000
|
|
|
|
# Ensure output directory exists
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
# Extract basename
|
|
basename = os.path.splitext(os.path.basename(input_file))[0]
|
|
|
|
# Get total duration
|
|
total_duration = get_duration(input_file)
|
|
print(f"Total duration: {total_duration:.2f} seconds")
|
|
|
|
cur_duration = 0
|
|
i = 1
|
|
|
|
while cur_duration < total_duration:
|
|
output_file = os.path.join(output_dir, f"{basename}-{i}{file_ext}")
|
|
ffmpeg_cmd = [
|
|
"ffmpeg", "-ss", str(cur_duration), "-i", input_file,
|
|
"-fs", str(size_limit), "-c", "copy", "-y", output_file
|
|
]
|
|
|
|
try:
|
|
result = subprocess.run(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
if result.returncode != 0:
|
|
print(f"Error splitting chunk {i}: {result.stderr}")
|
|
sys.exit(1)
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"Error splitting chunk {i}: {e}")
|
|
sys.exit(1)
|
|
|
|
# Get duration of the new chunk
|
|
chunk_duration = get_duration(output_file)
|
|
cur_duration += chunk_duration
|
|
print(f"Created chunk {i} ({cur_duration:.2f}/{total_duration:.2f} seconds processed)")
|
|
i += 1
|
|
|
|
print("Splitting complete!")
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Split a video file into chunks based on size limit.")
|
|
parser.add_argument("input_file", help="Path to the input video file")
|
|
parser.add_argument("output_dir", help="Directory to store the split video files")
|
|
parser.add_argument("--size", type=float, default=110, help="Size limit for each chunk in MB (default: 110)")
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Validate input file
|
|
if not os.path.isfile(args.input_file):
|
|
print(f"Error: Input file '{args.input_file}' does not exist")
|
|
sys.exit(1)
|
|
|
|
# Validate size limit
|
|
if args.size <= 0:
|
|
print("Error: Size limit must be a positive number")
|
|
sys.exit(1)
|
|
|
|
split_video(args.input_file, args.size, args.output_dir)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|