-
Notifications
You must be signed in to change notification settings - Fork 0
/
jpger
65 lines (48 loc) · 1.97 KB
/
jpger
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
#!/usr/bin/python3
# Script to convert multiple image files into jpg.
# Edit the first three variables accordingly.
# Dependencies: Python 3, FFmpeg.
import os
import mimetypes
import subprocess
# edit the variables: inputFilename, outputFilename, ffmpegcmd
# common string in the input filename
# that is ALSO NOT in the `outputFilename` below IF POSSIBLE; else remove the files that were generated by this script before running it again when some error happens.
inputFilename = 'img'
# output filename format
outputFilename = 'img{numbering}.jpg'
# copy the mkv cmd, remove --title, and insert {output} & {input} in proper place.
ffmpegcmd = "ffmpeg -v warning -i '{input}' './jpg/{output}'"
os.makedirs("./jpg", exist_ok=True)
def filterVid(file):
fullPath = os.path.join(os.getcwd(), file)
guessType = mimetypes.guess_type(fullPath)
isFile = os.path.isfile(fullPath)
isNotNoneType = guessType[0] is not None
isTypeMatched = isNotNoneType and (
'image' in guessType[0])
isCorrectInput = inputFilename in file
if isFile and isTypeMatched and isCorrectInput:
return True
else:
return False
def getNumbering(num):
return ("00" + str(num)) if (num < 10) else ("0" +
str(num)) if (num < 100) else str(num)
listdir = os.listdir()
filteredDir = list(filter(filterVid, listdir))
filteredDir.sort()
# increase by 1 when the file is a double page (indicated by a dash "-").
initialNum = 0
for idx, file in enumerate(filteredDir, start=1):
num = idx - initialNum
numbering = ""
if "-" in file: # if the image file is a double page
numbering = getNumbering(num) + "-" + getNumbering(num + 1)
initialNum += 1
else:
numbering = getNumbering(num)
outputFormat = outputFilename.format(numbering=numbering)
cmdFormat = ffmpegcmd.format(input=file, output=outputFormat)
print("\n", cmdFormat, "\n")
# subprocess.run(cmdFormat, text=True, shell=True)