Skip to content

Instantly share code, notes, and snippets.

@binshengliu
binshengliu / fix-mp3.sh
Created August 29, 2021 00:42
Fix mp3 duration and encoding
# https://askubuntu.com/a/1000020/657385
ffmpeg -i file_orig.mp3 -acodec copy file_fixed.mp3
# https://www.xmodulo.com/convert-mp3-id3-tag-encodings-linux.html
mid3iconv -e gbk/big5 -d input.mp3
@binshengliu
binshengliu / unzip_zh.sh
Last active July 18, 2021 05:20
Unzip files with Chinese characters
# https://unix.stackexchange.com/a/501546/215915
env LC_ALL=zh_CN.GB2312 7z x file.zip
convmv -f zh_CN.GB2312 -t UTF8 -r .
@binshengliu
binshengliu / watermark.sh
Created June 26, 2021 00:15
Watermark a photo using command line
convert passport.jpg -background transparent -fill black -size 1024x256 -pointsize 24 -gravity center -annotate +0+205 'For ... Purpose' passport-water.jpg
@binshengliu
binshengliu / full-backup.sh
Last active May 17, 2021 00:01
Full System Backup
# https://wiki.archlinux.org/title/Rsync#Full_system_backup
rsync -aAXHv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup
# More human readable
rsync -aAXHh --info=progress2 --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} / /path/to/backup
@binshengliu
binshengliu / beamer-notes-second-screen.tex
Created March 23, 2021 22:54
Show beamer notes on second screen
% https://tex.stackexchange.com/a/84763/124998
\usepackage{pgfpages}
\setbeameroption{show notes}
\setbeameroption{show notes on second screen=right}
% Command line:
% pdfpc --notes=right presentation.pdf
@binshengliu
binshengliu / pdf-to-png.sh
Created March 22, 2021 10:52
Convert PDF to PNG
# https://askubuntu.com/a/50180
pdftoppm -png -r 300 input.pdf png/out
@binshengliu
binshengliu / indep_roll.py
Created August 13, 2020 01:31
Independently roll rows of a tensor.
# Copied from https://stackoverflow.com/a/56175538/955952
def indep_roll(arr, shifts, axis=1):
"""Apply an independent roll for each dimensions of a single axis.
Parameters
----------
arr : np.ndarray
Array of any shape.
@binshengliu
binshengliu / len_to_mask.py
Last active August 13, 2020 01:43
Convert different lengths into mask tensor
# lens = [3, 5, 4]
# What we want:
# mask = [[1, 1, 1, 0, 0],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 0]]
# https://stackoverflow.com/questions/53403306/how-to-batch-convert-sentence-lengths-to-masks-in-pytorch
def len_to_mask(lens: np.ndarray, seq_len: Optional[int] = None) -> np.ndarray:
if seq_len is None:
seq_len = max(lens)
@binshengliu
binshengliu / setup_log.py
Created July 8, 2020 02:22
Setup a basic logger to lot INFO to stderr
logging.basicConfig(
level=logging.INFO,
stream=sys.stderr,
format="[%(asctime)s][%(levelname)s] - %(message)s",
)
log = logging.getLogger(__name__)
@binshengliu
binshengliu / prepend_line_number.sh
Created June 16, 2020 00:53
Prepend line number and a tab to each line
# Number each line
cat FILE | sed -r 's/^[[:space:]]*//' | awk '{print NR, "\t", $0}'
# Remove empty lines and number
cat FILE | sed -r 's/^[[:space:]]*//' | grep -v '^$' | awk '{print NR, "\t", $0}'