public
Created

A bash script for reversing videos using ffmpeg and sox.

  • Download Gist
ffmpeg_sox_reverse.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#!/bin/bash
# Created by Håvard Fossli <hfossli@gmail.com> in 2013
# This is free and unencumbered software released into the public domain.
# For more information, please refer to <http://unlicense.org/>
#
# Description
# A bash script for reversing videos using ffmpeg and sox.
#
# Keywords
# Terminal, bash, unix, mac, shell, script, video, movie, reverse, inverse, audio, video, ffmpeg, sox.
#
# Usefull links when working with ffmpeg
# - https://sites.google.com/site/linuxencoding/ffmpeg-tips
# - http://pvdm.xs4all.nl/wiki/index.php/Convert_an_AVCHD_/_MTS_file_to_MP4_using_ffmpeg
# - http://rodrigopolo.com/ffmpeg/cheats.php
# - http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs
#
 
# arg1: name of program
function assert_program_exists() {
command -v $1 >/dev/null 2>&1 || {
echo >&2 "You must have $1 installed in order to run this script. Try:";
echo "\$ brew install $1"
exit 1;
}
}
 
# arg1: path to input file / source
function fallback_output_path() {
if [[ -z "$1" ]] ; then
echo "Bad input to get_video_duration_in_seconds_from_ffmpeg_info()"
echo "Input givven $1"
exit 1
else
__FILE_NAME=`rev <<< "$1" | cut -d"." -f2- | rev`
__FILE_EXT=`rev <<< "$1" | cut -d"." -f1 | rev`
__OUT_FILE_FORMAT="${__FILE_NAME}-reversed.mp4"
echo $__OUT_FILE_FORMAT
exit 1
fi
}
 
function echo_variables {
echo
echo "### Variables ###"
echo
}
 
function usage {
echo "For help and detailed guide type:"
echo "\$ $0 -h"
}
 
function detailed_guide {
echo "
Example:
\$ $0 input.file output-filename
 
Info:
- input file may be any kind of file reconginzed by ffmpeg
 
Flags:
-i Path to input (string)
 
-o Path to output (string)
 
-o Path to output (string)
 
-o Path to output (string)
 
-o Path to output (string)
 
"
}
 
# Allow to be terminated with ctrl + c
trap "exit" INT
 
# Assert ffmpeg is installed
assert_program_exists "ffmpeg"
 
# Set variables and default values
typeset -i CHUNK_LEN
INPUT_PATH=''
OUTPUT_PATH=''
INCLUDE_AUDIO_STREAM=true
VERBOSE=false
PLAY_SOUND=false
HELP=false
DEINTERLACE=false
FFMPEG_IN_PARAMS=''
FFMPEG_OUT_PARAMS=''
 
# Grab input arguments
while getopts “i:o:dvip:q:sh” OPTION
do
case $OPTION in
i) INPUT_PATH=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;;
o) OUTPUT_PATH=$(echo "$OPTARG" | sed 's/ /\\ /g' ) ;;
d) DEINTERLACE=true ;;
v) VERBOSE=true ;;
a) INCLUDE_AUDIO_STREAM=false ;;
p) FFMPEG_IN_PARAMS="$OPTARG" ;;
q) FFMPEG_OUT_PARAMS="$OPTARG" ;;
s) PLAY_SOUND=true ;;
h) HELP=true ;;
?) usage
exit 1
;;
esac
done
 
if $HELP ; then
detailed_guide
exit 1
fi
 
if [[ -z $INPUT_PATH ]] ; then
echo "Invalid source"
usage
exit 1
fi
if [ -z "$OUTPUT_PATH" ] ; then
OUTPUT_PATH=$(fallback_output_path $INPUT_PATH)
fi
 
 
TMP_DIR=$(mktemp -dt "test")
AUDIO_PATH=${TMP_DIR}/backwards.wav
AUDIO_PARAMS=''
 
if $VERBOSE ; then
echo "Created empty directory $TMP_DIR"
ls -la $TMP_DIR
fi
 
 
if $DEINTERLACE ; then
ffmpeg -i $INPUT_PATH -an -qscale 1 -vf yadif $FFMPEG_IN_PARAMS ${TMP_DIR}/%06d.jpg
else
ffmpeg -i $INPUT_PATH -an -qscale 1 $FFMPEG_IN_PARAMS ${TMP_DIR}/%06d.jpg
fi
 
if $INCLUDE_AUDIO_STREAM ; then
assert_program_exists "sox"
 
ffmpeg -i $INPUT_PATH -vn -ac 2 ${TMP_DIR}/audio.wav
sox -V ${TMP_DIR}/audio.wav ${TMP_DIR}/audio_reverse.wav reverse
AUDIO_PARAMS="-i ${TMP_DIR}/audio_reverse.wav"
fi
 
cat $(ls -r $TMP_DIR/*jpg) | ffmpeg -f image2pipe -vcodec mjpeg -r 25 -i - -i $TMP_DIR/audio_reverse.wav $FFMPEG_OUT_PARAMS $OUTPUT_PATH
 
if $VERBOSE ; then
echo
echo "Will $TMP_DIR"
ls -la $TMP_DIR
fi
 
echo "Done!"
 
exit 1
 
rm -rf $TMP_DIR
 
if $PLAY_SOUND ; then
# & means async
say "Done! ." &
fi

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.