Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- echo "welcome to auti-epub"
- function usage {
- cat << USAGE
- Usage auti-epub input-file [subtitles]
- Examples:
- auti-epub my-file.mp4 Output 'my-file.epub'
- auti-epub my-file.mp4 subs.srt Output 'my-file.epub' using subs.srt
- Report bugs to <example@example.com>
- USAGE
- }
- # >> Get input
- input=$1
- # TODO: Allow outside subtitle file
- subs="$2"
- # >> Check input
- if [ "$input" == "" ]; then
- echo "Error: No input file"
- usage
- exit 1
- fi
- # >> Create temp folder
- rm -r tmp 2> /dev/null
- mkdir tmp
- # >> Extract times to capture
- echo "Extracting times to capture"
- # No subtitle file provided, get it from the input file
- if [ "$subs" == "" ]; then
- # TODO: Detect which stream is the subtitle stream (if needed)
- subs="tmp/subs.srt"
- # Extract the subtitles
- # https://superuser.com/a/927507
- ffmpeg -i $input -map 0:s:0 $subs 2> /dev/null
- fi
- # Extract the fps
- # https://askubuntu.com/a/110269
- fps="$(ffmpeg -i $input 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p")"
- # Parse times from the subtitles
- # TODO: sort the subtitles by the sequence numver
- # TODO: delete the first line if it's empty
- # TODO: only look for the second line of a paragraph rather than grep
- times="$(grep -oE '^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})' $subs)"
- if [ "$times" == "" ]; then
- echo "error: there were no subtitles found"
- exit 1
- fi
- # Convert times to frames
- function time_to_frames {
- fps=$1
- time=$2
- # parse time
- hours="${time:0:2}"
- hours="$((10#$hours))" # Convert to base 10 https://stackoverflow.com/a/24777667
- minutes="${time:3:2}"
- minutes="$((10#$minutes))"
- # Ignoring the milliseconds bit seems to work just fine
- seconds="${time:6:2}"
- seconds="$((10#$seconds))"
- seconds="$((seconds + 1))" # round up so we're in the bounds
- # Combine into seconds
- seconds="$(echo "($hours*60*60)+($minutes*60)+$seconds" | bc)"
- frame="$(echo "$seconds*$fps" | bc)"
- # Round up to the next frame
- # https://stackoverflow.com/a/32797696
- frame="$(echo "scale=0; ($frame+0.5)/1" | bc)"
- echo $frame
- }
- # >> Extract the frames w/ subtitles
- echo "Extracting frames w/ subtitles"
- # Create the ffmpeg select string
- select_string=""
- while IFS= read -r line; do
- frame="$(time_to_frames $fps $line)"
- select_string="$select_string+eq(n\,$frame)"
- done <<< "$times"
- select_string="${select_string:1}" # remove leading '+'
- # Bake in subtitles
- # TODO: Combine with extracting frames?
- extension="${input##*.}"
- subtitled="tmp/subtitled.$extension"
- ffmpeg -i $input -vf subtitles=$subs $subtitled 2> /dev/null
- # Extract frames
- # https://stackoverflow.com/a/38259151
- mkdir tmp/frames
- ffmpeg -i $subtitled -vf subtitles=$subs -vf select="$select_string" -vsync 0 tmp/frames/%d.jpg 2> /dev/null
- # >> Package it as an epub
- echo "Packaging as an epub"
- ./jpeg2epub --title "${input%.*}" tmp/frames/*
- # >> Cleanup
- rm -r tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement