Skip to content

Instantly share code, notes, and snippets.

@movalex
Created August 3, 2023 15:49
Show Gist options
  • Save movalex/4a12348ca625ba156471451b980be5da to your computer and use it in GitHub Desktop.
Save movalex/4a12348ca625ba156471451b980be5da to your computer and use it in GitHub Desktop.
FFmpeg cut video in chunks
import sys
import os
from pathlib import Path
CHUNK_LENGTH = 59
def cut_chunks(file_name=None, resize=False):
if not file_name:
print("enter file name")
return
file_name = Path(file_name)
if not file_name.exists():
print(f"file [{file_name.name}] not found")
return
ffprobe_command = (
f"ffprobe.exe -v error -show_entries format=duration "
f'-of default=noprint_wrappers=1:nokey=1 "{file_name.absolute()}"'
)
print(ffprobe_command)
file_duration = float(os.popen(ffprobe_command).read())
print(f"file duration {file_duration}")
start = 0.0
count = 0
resize_string = ""
if resize:
resize_string = "-vf scale=1080x1920:flags=lanczos "
while start <= file_duration:
os.system(
f'ffmpeg -y -ss {start} -i "{file_name}" -t {CHUNK_LENGTH}'
"-c:v libx264"
f'{resize_string} "{file_name.stem}"_scaled_chunk{count:02}.mp4'
)
start += CHUNK_LENGTH
count += 1
if count:
print(f"File cut in {count} chunks")
if __name__ == "__main__":
if len(sys.argv) > 1:
files = sys.argv[1:]
for file in files:
cut_chunks(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment