Browse Source

Added video to epub converter

This script is somewhat ridiculous. It takes a video and .vtt
subtitles file and converts it to an .epub file with images for each
subtitle. This makes it possible to "read" a TV episode on an E-Ink
tablet, for example.

I made this because I'm not good enough at listening to Japanese at
full speed. Now, I can "read the episode" first, then watch it later.
master
Macoy Madson 4 months ago
parent
commit
a32944bc6e
2 changed files with 90 additions and 0 deletions
  1. +22
    -0
      ReadMe.org
  2. +68
    -0
      src/VideoToEPUB.py

+ 22
- 0
ReadMe.org View File

@@ -68,6 +68,28 @@ This script automatically detects Japanese articles based on whether there are a
- Paste the token string into ~src/Calibre_Wallabag_To_EPUB.recipe~ at the URL line, along with your username
- Run the corresponding ~DownloadCalibreArticles*.sh~ script to create a .epub in the same directory with your articles
- Read that with an EPUB reader, e.g. [[https://github.com/makuto/typhon][Typhon]], with good Japanese dictionary support
** Using the Video to EPUB script
This script is somewhat ridiculous. It takes a video and .vtt subtitles file and converts it to an .epub file with images for each subtitle. This makes it possible to "read" a TV episode on an E-Ink tablet, for example.

I made this because I'm not good enough at listening to Japanese at full speed. Now, I can "read the episode" first, then watch it later.

The full pipeline requires both ~ffmpeg~ and ~pandoc~:

#+BEGIN_SRC sh
sudo apt install ffmpeg pandoc
#+END_SRC

And here's how to run it:

#+BEGIN_SRC sh
# Run the script
python3 VideoToEPUB.py MyVideo.mp4 MyVideo.ja.vtt output/
# Convert to EPUB
pandoc -f org -t epub output/MyVid.org -o ~/Documents/MyVid.epub
#+END_SRC

Note that this should work for any language, so long as the subtitles file is ~.vtt~ format. Any video format supported by ffmpeg should work.

* License
The repository itself is under the MIT license.



+ 68
- 0
src/VideoToEPUB.py View File

@@ -0,0 +1,68 @@
import sys
import subprocess
import os
import pathlib

def makeDirIfNonexistant(directory):
if not os.path.exists(directory):
os.makedirs(directory)

def main(videoToConvertName, subtitleFilename, outputDir):
subtitleFile = open(subtitleFilename, 'r')

subtitleLines = subtitleFile.readlines()
subtitleFile.close()

makeDirIfNonexistant(outputDir)
videoPath = pathlib.Path(videoToConvertName)
outputPath = pathlib.Path(outputDir)
videoName = videoPath.stem
outputFilename = outputPath / (videoName + '.org')

outputLines = []

outputLines.append("#+TITLE:{}\n\n".format(videoName))

accumulatedText = []
previousLine = None
# Skip first line (it's the format specifier)
for subtitleLine in subtitleLines[1:]:
# Detected subtitle timestamp
if '-->' in subtitleLine:
# Finish writing the last subtitle
# Lop off the previousLine number
if accumulatedText:
accumulatedText = accumulatedText[:-1]
outputLines.extend(accumulatedText)

# Make subtitle number the chapter title
outputLines.append('* ' + previousLine)
subtitleNumber = int(previousLine.strip())
outputImageFilename = outputPath / "{}.{}".format(subtitleNumber, 'jpg')
# TODO: Use the middle of the range instead?
screenshotTimestamp = subtitleLine[:12]
print(['ffmpeg', '-ss', screenshotTimestamp, '-i', videoToConvertName,
'-vframes', '1', '-q:v', '2', outputImageFilename])
# Note that this will do any space escaping necessary for us
# Never overwrite output files
subprocess.call(['ffmpeg', '-ss', screenshotTimestamp, '-n', '-i', videoToConvertName,
'-vframes', '1', '-q:v', '2', outputImageFilename])
outputLines.append('\n[[file:{}]]\n\n'.format(outputImageFilename.name))
accumulatedText = []
# Get the actual subtitle text
# Ignore newlines
elif subtitleLine.strip():
accumulatedText.append(subtitleLine)

previousLine = subtitleLine

outputFile = open(outputFilename, 'w')
outputFile.writelines(outputLines)
outputFile.close()

if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage:\npython3 VideoToEPUB.py [Video file] [VTT subtitle file] [output directory]")
print("\nNote that this script runs ffmpeg from the command line. Ensure it is in your PATH")
else:
main(sys.argv[1], sys.argv[2], sys.argv[3])

Loading…
Cancel
Save