Skip to content

Instantly share code, notes, and snippets.

@ja-k-e
Last active August 29, 2015 14:02
Show Gist options
  • Save ja-k-e/768c9880be7470c918ab to your computer and use it in GitHub Desktop.
Save ja-k-e/768c9880be7470c918ab to your computer and use it in GitHub Desktop.
Takes .txt files in project root, finds raw video with same filename in `raw` subdir, and cuts videos according to data on each line in the .txt file.
#!/bin/sh
# for each text file, convert corresponding video
for FILE in *.txt; do
# get movie file name
FILE_NAME=${FILE%.*}
# if movie exists
if [ -a "raw/$FILE_NAME.mp4" ]; then
# create output directory
mkdir output/$FILE_NAME
# start building ffmpeg command
cmnd="ffmpeg -i raw/$FILE_NAME.mp4 "
# for each line in text file
while ((line_number++)); read START STOP NAME; do
# replace spaces with underscores
NEW_NAME=${NAME// /_}
# append to ffmpeg command -> re-encoding each file so it will take longer (and a ton of cpu), but the precision is 1000 times better.
cmnd=$cmnd"-ss $START -to $STOP -map 0 output/$FILE_NAME/$FILE_NAME$(printf %03d $line_number)-$NEW_NAME.mp4 "
done < $FILE # end while read do
# run created ffmpeg command
$cmnd
# append "_CONVERTED" to movie file
mv raw/$FILE_NAME.mp4 raw/$FILE_NAME"_CONVERTED.mp4"
# switch underscores back to spaces
for CLIP in output/$FILE_NAME/*.mp4; do
# get path
DIR_NAME=$(dirname ${CLIP})
# get CLIP name
CLIP_NAME=$(basename $CLIP)
# make new CLIP name with spaces for underscores
NEW_NAME=${CLIP_NAME//_/ }
# rename CLIP
mv $CLIP "$DIR_NAME/$NEW_NAME"
done
else
echo "---------------------------------------------------------------------------------"
echo "*********************************************************************************"
echo ""
echo "raw/$FILE_NAME.mp4 does not exist!"
echo "Make sure $FILE_NAME.mp4 is in the `raw` directory if you haven't already converted it."
echo "Converted videos have `_CONVERTED` after their name."
echo ""
echo "*********************************************************************************"
echo "---------------------------------------------------------------------------------"
fi
done
# directory structure:
| ffmpeg_bulk_splitter.sh
| [file_name_1].txt
| [file_name_2].txt
| raw/
|-- [file_name_1].mp4
|-- [file_name_2].mp4
00:00:01.1 00:00:02.1 Name of first 1-second clip
00:00:02.2 00:00:04.2 Name of second 2-second clip
00:00:00 00:00:00 DUMMY
# script requires dummy line at end, not sure why
# avoid special characters in clip names!!!
# script will replace spaces with underscores then remove them after processing.
# script will not replace ! ? " ' / . etc. -> they can break this script.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment