From a337f262229f37f8d4ab5da2b41869d6cd3748b8 Mon Sep 17 00:00:00 2001 From: Julin S Date: Wed, 1 Mar 2023 23:27:07 +0530 Subject: [PATCH] include 2 old scripts --- .gitignore | 11 ++++++ README.org | 5 +++ pdfscript.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ submixer.sh | 53 +++++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 .gitignore create mode 100644 README.org create mode 100644 pdfscript.py create mode 100755 submixer.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..19c44bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# emacs +*~ +\#*\# +.\#* + +# vim +*.sw[op] + +# python +*.pyc +__pycache__/ diff --git a/README.org b/README.org new file mode 100644 index 0000000..174cbf8 --- /dev/null +++ b/README.org @@ -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 diff --git a/pdfscript.py b/pdfscript.py new file mode 100644 index 0000000..63b7a17 --- /dev/null +++ b/pdfscript.py @@ -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) diff --git a/submixer.sh b/submixer.sh new file mode 100755 index 0000000..6f00916 --- /dev/null +++ b/submixer.sh @@ -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