aboutsummaryrefslogtreecommitdiff
path: root/env/.local/bin/scripts/gen-thumb-md
blob: 4308bf6420fe43c8ded8066c806d10657fc6e6ac (plain)
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
#!/bin/sh
script_dir="$( cd "$( dirname "$0" )" || exit > /dev/null && pwd )"

disable_thumb_gen=0
disable_auto_editor=0
while getopts "de" opt; do
    case "$opt" in
        d) disable_thumb_gen=1
            ;;
        e) disable_auto_editor=1
            ;;
        *) echo invalid arg
            exit
            ;;
    esac
done

shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift


[ "$1" ] && view_dir="$1"

if [ ! "$view_dir" ]; then
    echo NO DIR
    exit 1
fi

view_dir="$( cd "$view_dir" || exit > /dev/null && pwd )"


to_file() {
    printf -- "- [%s](<%s>)" "$2" "$1" >> genthumb.md
    if [ "$3" = 1 ]; then
        printf -- "\n   - ![%s](</tmp/gen-thumb-md/%s.png>)\n" "$2" "$2" >> genthumb.md
    else
        printf -- "\n   - ![%s](<%s>)\n" "$2" "$1" >> genthumb.md
    fi
}

gen_thumb() {
    file="$1"
    file_name="$(basename "$file")"
    case "$file" in
        *.avi|*.gif|*.mp4|*.mkv|*.webm|*.mov)
            if [ $disable_thumb_gen = 0 ]; then
                ffmpegthumbnailer -i "$file" -s 0 -q 5 -o "/tmp/gen-thumb-md/$file_name.png"
            fi
            to_file "$file" "$file_name" 1
            ;;
        *)
            to_file "$file" "$file_name" 0
            ;;
    esac
}


if [ ! -d /tmp/gen-thumb-md ]; then
    mkdir /tmp/gen-thumb-md
fi


printf -- "# gen thumb %s\n\n" "$view_dir" > genthumb.md

files="$(find "$view_dir" -type f)"

printf -- "%s" "$files" | while read -r file; do
    gen_thumb "$file"
done


if [ $disable_auto_editor = 0 ]; then
    $EDITOR ./genthumb.md
fi