include 2 old scripts

This commit is contained in:
Julin S 2023-03-01 23:27:07 +05:30
commit a337f26222
4 changed files with 170 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
# emacs
*~
\#*\#
.\#*
# vim
*.sw[op]
# python
*.pyc
__pycache__/

5
README.org Normal file
View File

@ -0,0 +1,5 @@
#+TITLE: README
Some scripts that I made for myself..
- pdfscript.py: process pdf files
- submixer.sh: merge ~.srt~ files into ~.ass~ files

101
pdfscript.py Normal file
View File

@ -0,0 +1,101 @@
"""
Requires Python>=3.6
"""
import argparse
import os
import pathlib
import subprocess
import tempfile
"""
select pages: gs
crop borders: pdfcrop
landscape/portrait: pdfnup
cmds:
- gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sPageList=$ARG -sOutputFile=$INFILE $OUTFILE
- pdfcrop --margins $ARG $INFILE $OUTFILE
- ./pdfnup --nup $LAYOUT $INFILE -o $OUTFILE --landscape
"""
CMDS = {
"gs": ("{gs} -sDEVICE=pdfwrite -dNOPAUSE -dBATCH"
"-sPageList={pages} sOutputFile={inp} {outp}"),
"pdfcrop": "{pdfcrop} --margins {margins} {inp} {outp}",
"pdfnup": "{pdfnup} --nup {layout} {inp} -o {outp} {landscape}"
}
def dispatch_cmd(op: str, args, inp: str, outp: str) -> None:
cmd = ""
if op == "gs":
cmd = CMDS["gs"].format(gs=args.gs_path,
pages=args.pages,
inp=inp,
outp=outp)
elif op == "pdfcrop":
cmd = CMDS["pdfcrop"].format(pdfcrop=args.pdfcrop_path,
margins=args.crop_margins,
inp=inp,
outp=outp)
elif op == "pdfnup":
landscape = ""
if args.landscape:
landscape = "--landscape"
cmd = CMDS["pdfnup"].format(pdfnup=args.pdfnup_path,
layout=args.layout,
inp=inp,
outp=outp,
landscape=landscape)
if args.dry:
print(cmd)
else:
subprocess.run(cmd.split())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("input")
parser.add_argument("output")
parser.add_argument("--landscape", action="store_true")
parser.add_argument("--layout", help="Like 2x1")
parser.add_argument("--crop-margins", help="l t r b")
parser.add_argument("--pages", help="pages to include") # how about a not-pages
parser.add_argument("--gs-path", help="Path to gs executable", default="gs")
parser.add_argument("--pdfcrop-path", help="Path to pdfcrop executable", default="pdfcrop")
parser.add_argument("--pdfnup-path", help="Path to pdfnup executable", default="pdfnup")
parser.add_argument("--dry", action="store_true", help="Do a dry run")
args = parser.parse_args()
# Queue up the selected operations
ops = []
if args.pages:
ops.append("gs")
if args.crop_margins:
ops.append("pdfcrop")
if args.layout:
ops.append("pdfnup")
with tempfile.TemporaryDirectory() as tmpdir:
tmpdirpath = pathlib.Path(tmpdir)
files = [args.input, tmpdirpath / "temp1.pdf",
tmpdirpath / "temp2.pdf"]
# idx into files list
inpidx = 0
outidx = 1
for op in ops[:-1]:
# infile -> outfile
dispatch_cmd(op, args, files[inpidx], files[outidx])
inpidx = outidx
if outidx == 1:
outidx = 2
else:
outidx = 1
else:
# infile -> outfile
dispatch_cmd(ops[-1], args, files[inpidx], args.output)

53
submixer.sh Executable file
View File

@ -0,0 +1,53 @@
#!/bin/bash
HEADER="[Script Info]
ScriptType: v4.00+
Collisions: Normal
PlayDepth: 0
Timer: 100,0000
Video Aspect Ratio: 0
WrapStyle: 0
ScaledBorderAndShadow: no
[V4+ Styles]
Format: Name,Fontname,Fontsize,PrimaryColour,SecondaryColour,OutlineColour,BackColour,Bold,Italic,Underline,StrikeOut,ScaleX,ScaleY,Spacing,Angle,BorderStyle,Outline,Shadow,Alignment,MarginL,MarginR,MarginV,Encoding
Style: Default,Arial,16,&H00FFFFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,2,10,10,10,0
Style: Top,Arial,16,&H00F9FFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,8,10,10,10,0
Style: Mid,Arial,16,&H0000FFFF,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,5,10,10,10,0
Style: Bot,Arial,16,&H00F9FFF9,&H00FFFFFF,&H00000000,&H00000000,-1,0,0,0,100,100,0,0,1,3,0,2,10,10,10,0"
OUTFILE="out.ass"
in1=$(mktemp --suffix ".ass")
in2=$(mktemp --suffix ".ass")
# Convert 1st srt to ass
ffmpeg -i $1 $in1
# Convert 2nd srt to ass
ffmpeg -i $2 $in2
# Write header
cat <<< $HEADER > $OUTFILE
# Get events from 1st input
# awk is easier
awk '
/\[Events\]/,EOF {
str = $0
sub(/,Default,/, ",Top,", str)
print str
}' $in1 >> $OUTFILE
# Get events from 2nd input
# awk is easier
awk -v ORS="" '
/\[Events\]/,EOF {
str = $0
sub(/,Default,/, ",Bot,", str)
print str
}' $in2 >> $OUTFILE
# cleanup
rm $in1
rm $in2