Advertisement
Guest User

auti-epub

a guest
Jan 20th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. echo "welcome to auti-epub"
  4.  
  5. function usage {
  6. cat << USAGE
  7. Usage auti-epub input-file [subtitles]
  8.  
  9. Examples:
  10. auti-epub my-file.mp4 Output 'my-file.epub'
  11. auti-epub my-file.mp4 subs.srt Output 'my-file.epub' using subs.srt
  12.  
  13. Report bugs to <example@example.com>
  14. USAGE
  15. }
  16.  
  17. # >> Get input
  18.  
  19. input=$1
  20. # TODO: Allow outside subtitle file
  21. subs="$2"
  22.  
  23.  
  24.  
  25. # >> Check input
  26.  
  27. if [ "$input" == "" ]; then
  28. echo "Error: No input file"
  29. usage
  30. exit 1
  31. fi
  32.  
  33.  
  34.  
  35. # >> Create temp folder
  36.  
  37. rm -r tmp 2> /dev/null
  38. mkdir tmp
  39.  
  40.  
  41.  
  42. # >> Extract times to capture
  43.  
  44. echo "Extracting times to capture"
  45.  
  46. # No subtitle file provided, get it from the input file
  47. if [ "$subs" == "" ]; then
  48. # TODO: Detect which stream is the subtitle stream (if needed)
  49.  
  50. subs="tmp/subs.srt"
  51. # Extract the subtitles
  52. # https://superuser.com/a/927507
  53. ffmpeg -i $input -map 0:s:0 $subs 2> /dev/null
  54. fi
  55.  
  56. # Extract the fps
  57. # https://askubuntu.com/a/110269
  58. fps="$(ffmpeg -i $input 2>&1 | sed -n "s/.*, \(.*\) fp.*/\1/p")"
  59.  
  60. # Parse times from the subtitles
  61. # TODO: sort the subtitles by the sequence numver
  62. # TODO: delete the first line if it's empty
  63. # TODO: only look for the second line of a paragraph rather than grep
  64. times="$(grep -oE '^([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})' $subs)"
  65.  
  66. if [ "$times" == "" ]; then
  67. echo "error: there were no subtitles found"
  68. exit 1
  69. fi
  70.  
  71. # Convert times to frames
  72. function time_to_frames {
  73. fps=$1
  74. time=$2
  75.  
  76. # parse time
  77. hours="${time:0:2}"
  78. hours="$((10#$hours))" # Convert to base 10 https://stackoverflow.com/a/24777667
  79. minutes="${time:3:2}"
  80. minutes="$((10#$minutes))"
  81. # Ignoring the milliseconds bit seems to work just fine
  82. seconds="${time:6:2}"
  83. seconds="$((10#$seconds))"
  84. seconds="$((seconds + 1))" # round up so we're in the bounds
  85.  
  86. # Combine into seconds
  87. seconds="$(echo "($hours*60*60)+($minutes*60)+$seconds" | bc)"
  88. frame="$(echo "$seconds*$fps" | bc)"
  89. # Round up to the next frame
  90. # https://stackoverflow.com/a/32797696
  91. frame="$(echo "scale=0; ($frame+0.5)/1" | bc)"
  92. echo $frame
  93. }
  94.  
  95.  
  96.  
  97. # >> Extract the frames w/ subtitles
  98.  
  99. echo "Extracting frames w/ subtitles"
  100.  
  101. # Create the ffmpeg select string
  102. select_string=""
  103. while IFS= read -r line; do
  104. frame="$(time_to_frames $fps $line)"
  105. select_string="$select_string+eq(n\,$frame)"
  106. done <<< "$times"
  107. select_string="${select_string:1}" # remove leading '+'
  108.  
  109. # Bake in subtitles
  110. # TODO: Combine with extracting frames?
  111. extension="${input##*.}"
  112. subtitled="tmp/subtitled.$extension"
  113. ffmpeg -i $input -vf subtitles=$subs $subtitled 2> /dev/null
  114.  
  115. # Extract frames
  116. # https://stackoverflow.com/a/38259151
  117. mkdir tmp/frames
  118. ffmpeg -i $subtitled -vf subtitles=$subs -vf select="$select_string" -vsync 0 tmp/frames/%d.jpg 2> /dev/null
  119.  
  120.  
  121.  
  122. # >> Package it as an epub
  123.  
  124. echo "Packaging as an epub"
  125.  
  126. ./jpeg2epub --title "${input%.*}" tmp/frames/*
  127.  
  128.  
  129.  
  130. # >> Cleanup
  131.  
  132. rm -r tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement