Skip to content

Instantly share code, notes, and snippets.

@ceremcem
Last active June 7, 2020 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ceremcem/4c7b44edd6c21c873f7f27bdaf7f2baa to your computer and use it in GitHub Desktop.
Save ceremcem/4c7b44edd6c21c873f7f27bdaf7f2baa to your computer and use it in GitHub Desktop.
extract video from command line
#!/bin/bash
set -eu -o pipefail
safe_source () { [[ ! -z ${1:-} ]] && source $1; _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; _sdir=$(dirname "$(readlink -f "$0")"); }; safe_source
# end of bash boilerplate
# show help
# -----------------------------------------------
show_help(){
cat <<HELP
$(basename $0) [options] /path/to/source /path/to/destination
Options:
--from : Starting time, in "00:05:32" format
duration: (optional)
--length : Duration, in "00:2:22" format
--to : End time, in "00:3:05" format
--out : (optional) Output file name
HELP
exit
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
_from="0"
_length=
_to=
_out=
# ---------------------------
args=("$@")
_count=1
while :; do
key="${1:-}"
case $key in
-h|-\?|--help|'')
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--from) shift
_from="$1"
shift
;;
--length) shift
_length="$1"
shift
;;
--to) shift
_to="$1"
shift
;;
--out) shift
_out="$1"
shift
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
echo
echo "Unknown option: $1"
show_help
exit 1
;;
*) # Generate the positional arguments: $_arg1, $_arg2, ...
[[ ! -z ${1:-} ]] && declare _arg$((_count++))="$1" && shift
esac
[[ -z ${1:-} ]] && break
done; set -- "${args[@]}"
# use $_arg1 in place of $1, $_arg2 in place of $2 and so on, "$@" is intact
[[ -z "$_length" ]] || _length="-t $_length"
[[ -z "$_to" ]] || _to="-to $_to"
input="$_arg1"
out="${_out:-"${input%.*}-${_from}${_to}${_length}.${input##*.}"}"
out="${out//:/_}"
#echo "from: $_from"
#echo "to: $_length"
#echo "input: $input"
echo "output: $out"
ffmpeg -ss "$_from" $_length $_to -i "$input" \
-vcodec copy -acodec copy "$out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment