SHARE
TWEET

Untitled

a guest Jan 19th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Written by Alexis Bezverkhyy <alexis@grapsus.net> in 2011
  4. # This is free and unencumbered software released into the public domain.
  5. # For more information, please refer to <http://unlicense.org/>
  6.  
  7. function usage {
  8.         echo "Usage : ffsplit.sh input.file chunk-duration [output-filename-format]"
  9.         echo -e "\t - input file may be any kind of file reconginzed by ffmpeg"
  10.         echo -e "\t - chunk duration must be in seconds"
  11.         echo -e "\t - output filename format must be printf-like, for example myvideo-part-%04d.avi"
  12.         echo -e "\t - if no output filename format is given, it will be computed\
  13.  automatically from input filename"
  14. }
  15.  
  16. IN_FILE="$1"
  17. OUT_FILE_FORMAT="$3"
  18. typeset -i CHUNK_LEN
  19. CHUNK_LEN="$2"
  20.  
  21. DURATION_HMS=$(ffmpeg -i "$IN_FILE" 2>&1 | grep Duration | cut -f 4 -d ' ')
  22. DURATION_H=$(echo "$DURATION_HMS" | cut -d ':' -f 1)
  23. DURATION_M=$(echo "$DURATION_HMS" | cut -d ':' -f 2)
  24. DURATION_S=$(echo "$DURATION_HMS" | cut -d ':' -f 3 | cut -d '.' -f 1)
  25. let "DURATION = ( DURATION_H * 60 + DURATION_M ) * 60 + DURATION_S"
  26.  
  27. if [ "$DURATION" = '0' ] ; then
  28.         echo "Invalid input video"
  29.         usage
  30.         exit 1
  31. fi
  32.  
  33. if [ "$CHUNK_LEN" = "0" ] ; then
  34.         echo "Invalid chunk size"
  35.         usage
  36.         exit 2
  37. fi
  38.  
  39. if [ -z "$OUT_FILE_FORMAT" ] ; then
  40.         FILE_EXT=$(echo "$IN_FILE" | sed 's/^.*\.\([a-zA-Z0-9]\+\)$/\1/')
  41.         FILE_NAME=$(echo "$IN_FILE" | sed 's/^\(.*\)\.[a-zA-Z0-9]\+$/\1/')
  42.         OUT_FILE_FORMAT="${FILE_NAME}-%03d.${FILE_EXT}"
  43.         echo "Using default output file format : $OUT_FILE_FORMAT"
  44. fi
  45.  
  46. N='1'
  47. OFFSET='0'
  48. let 'N_FILES = DURATION / CHUNK_LEN + 1'
  49.  
  50. while [ "$OFFSET" -lt "$DURATION" ] ; do
  51.         OUT_FILE=$(printf "$OUT_FILE_FORMAT" "$N")
  52.         echo "writing $OUT_FILE ($N/$N_FILES)..."
  53.         ffmpeg -i "$IN_FILE" -vcodec copy -acodec copy -ss "$OFFSET" -t "$CHUNK_LEN" "$OUT_FILE"
  54.         let "N = N + 1"
  55.         let "OFFSET = OFFSET + CHUNK_LEN"
  56. done
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top