#!/bin/bash

# Outputs a new video file by temporally cropping an existing video between two
# frame numbers.
# Example:
#  ./slice_video.sh my_video.mp4 1000 1000 my_video_slice.mp4
#    (my_video_slice.mp4 will be a video 1000 frames long starting at frame
#     1000 of my_video.mp4)

set -e -x

if [[ $# -ne 4 ]]; then
  echo "Usage: ./slice_video.sh [INPUT] [START] [LENGTH] [OUTPUT]"
  exit 0
fi

# Build ffmpeg.
bazel build -c opt --config=cuda third_party/ffmpeg:ffmpeg_tool

INPUT="$1"
START="$2"
LENGTH="$3"
OUTPUT="$4"

./bazel-bin/third_party/ffmpeg/ffmpeg_tool \
  -i "${INPUT}" \
  -ss "${START}" \
  -t "${LENGTH}" \
  -acodec copy \
  -vcodec copy \
  -r 28 \
  ${OUTPUT}
