-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_conversion.sh
More file actions
executable file
·142 lines (118 loc) · 3.72 KB
/
format_conversion.sh
File metadata and controls
executable file
·142 lines (118 loc) · 3.72 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
# Resize image, keep aspect ratio and fill blank space
function jpg_to_png() {
local input="$1"
local output="$2"
convert "$input" -resize 200x200 -background white -gravity center -extent 200x200 "$output"
}
function heic_to_jpg() {
# ---- dependency checks ----
local deps=(parallel magick)
for dep in "${deps[@]}"; do
if ! command -v "$dep" >/dev/null 2>&1; then
echo "❌ Error: required dependency '$dep' not found in PATH." >&2
return 127
fi
done
# ---- shell options ----
if [[ -n $ZSH_VERSION ]]; then
setopt LOCAL_OPTIONS nocaseglob nullglob
elif [[ -n $BASH_VERSION ]]; then
shopt -s nocaseglob nullglob
fi
local files=(*.heic)
if [[ ${#files[@]} -eq 0 ]]; then
echo "⚠️ No HEIC files found in the current directory." >&2
return 1
fi
echo "Converting ${#files[@]} HEIC files to JPG using GNU parallel…"
parallel 'magick {} {.}.jpg' ::: "${files[@]}"
echo "✅ All conversions complete."
# ---- cleanup ----
if [[ -n $BASH_VERSION ]]; then
shopt -u nocaseglob nullglob
fi
}
# Converts SVG to PNG
# Usage svg_to_png input_image_path [output_image_height]
function svg_to_png() {
local -r filename=$(filename_without_extension "$1")
local output_filename=$filename.png
local output_image_height=${2:-32}
rsvg-convert -h "$output_image_height" "$1" >"$output_filename" && echo "Successfully created $output_filename" || echo "Error converting $1 to $output_filename"
}
function webp_to_png() {
local -r filename=$(filename_without_extension "$1")
local output_filename=$filename.png
local output_image_height=${2:-32}
dwebp "$1" -o "$output_filename"
}
function pdf_to_jpg() {
pdfimages -j "$1" page
}
function mov_to_mp4() {
local input_filename="$1"
local -r filename=$(filename_without_extension "$1")
local output_filename=$filename.mp4
local fps=60
ffmpeg -i "$input_filename" -loglevel error \
-fflags +genpts -r $fps "$output_filename" &&
echo "Successfully created $output_filename" ||
echo "Error converting $1 to $output_filename"
}
# flac2mp3: Convert FLAC files to MP3 while preserving ID3 tags
#
# Usage:
# flac2mp3 file1.flac file2.flac ...
# find . -name '*.flac' -print0 | xargs -0 flac2mp3
#
# Requires: metaflac, flac, lame
function flac2mp3() {
local deps=(metaflac flac lame)
for cmd in "${deps[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: missing dependency: $cmd" >&2
return 1
fi
done
for f in "$@"; do
[[ "$f" != *.flac ]] && continue
[[ ! -f "$f" ]] && {
echo "Skipping: $f (not found)"
continue
}
album=$(metaflac --show-tag=album "$f" | sed 's/[^=]*=//')
artist=$(metaflac --show-tag=artist "$f" | sed 's/[^=]*=//')
year=$(metaflac --show-tag=date "$f" | sed 's/[^=]*=//')
title=$(metaflac --show-tag=title "$f" | sed 's/[^=]*=//')
genre=$(metaflac --show-tag=genre "$f" | sed 's/[^=]*=//')
tracknumber=$(metaflac --show-tag=tracknumber "$f" | sed 's/[^=]*=//')
out="${f%.flac}.mp3"
echo "Converting: $f → $out"
flac --decode --stdout "$f" | lame --preset extreme --add-id3v2 \
--tt "$title" \
--ta "$artist" \
--tl "$album" \
--ty "$year" \
--tn "$tracknumber" \
--tg "$genre" \
- "$out"
done
}
function md2pdf() {
local -r filename=$(filename_without_extension "$1")
pandoc --standalone --variable=papersize:a4 --variable=geometry:margin=1in --from=gfm -o "$filename".pdf "$filename".md
}
function pdf2md() {
local -r filename=$(filename_without_extension "$1")
docling "$filename.pdf" --ocr-lang pl --output $filename
}
function rst2pdf() {
pandoc --standalone -V papersize:a4 -V geometry:margin=1in --from rst -o "$1".pdf "$1"
open "$1".pdf
}
function md2html() {
local infile="${1:--}"
local outfile="${2:--}"
pandoc -f markdown -t html "$infile" -o "$outfile"
}