summaryrefslogtreecommitdiff
path: root/bin/tgencode
blob: e0a1a220939e5f6915d2ba14fa37c906a5079e83 (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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env zsh
#
# Copyright (C) 2024-2026 Maria Lisina
# SPDX-License-Identifier: Apache-2.0
#
# Telegram Media Re-encoder

if (( __env != 1 ))
then
    exec /usr/bin/env -i PATH="${PATH}" __env=1 "${0}" ${@} 2> /dev/null
fi

set -e

version="3.3"

if [[ -n "${1}" ]]
then
    while getopts hl:r:f:ap:c:d opts
    do
        case "${opts}" in
            (h)
                help=1
            ;;
            (l)
                loglevel=${OPTARG}
            ;;
            (r)
                fps=${OPTARG}
            ;;
            (f)
                filters="${OPTARG}"
            ;;
            (a)
                noaudio=1
            ;;
            (p)
                preset=${OPTARG}
            ;;
            (c)
                crf=${OPTARG}
            ;;
            (d)
                delete=1
            ;;
            (*)
                echo "Unrecognized options" \
                    "\nSee '${0} -h'"
                exit 1
            ;;
        esac
    done

    shift $((OPTIND - 1))
else
    help=1
fi

if (( help == 1 ))
then
    echo "Telegram Media Re-encoder v${version}" \
        "\n\nUsage: ${0} [options] [file]" \
        "\n\nOptions:" \
        "\n  -h\t\tShow help information" \
        "\n  -l <lvl>\tLog level, default: info" \
        "\n  -r <num>\tSet custom frame rate" \
        "\n  -f <str>\tSet custom filters" \
        "\n  -a\t\tRemove audio stream" \
        "\n  -p <pst>\tPreset, default: medium" \
        "\n  -c <num>\tConstant Rate Factor, default: 23" \
        "\n  -d\t\tDelete original file"
    exit 0
fi

for zmod in zsh/files
do
    if ! zmodload ${zmod}
    then
        failed+=(${zmod})
    fi
done

if [[ -n "${failed}" ]]
then
    echo "Failed to load Z Shell modules: ${failed}" \
        "\nUpdate your Z Shell or get a version with all required modules"
    exit 1
fi

for req in ffmpeg
do
    if ! command -v ${req} > /dev/null
    then
        missing+=(${req})
    fi
done

if [[ -n "${missing}" ]]
then
    echo "Missing dependencies: ${missing}" \
        "\nFor more information follow: https://command-not-found.com/"
    exit 1
fi

if [[ -z "${loglevel}" ]]
then
    loglevel=info
fi

if [[ -z "${fps}" ]]
then
    fps=source_fps
fi

if [[ -z "${filters}" ]]
then
    filters="pad=ceil(iw/2)*2:ceil(ih/2)*2"
fi

if (( noaudio == 1 ))
then
    acodec=(an)
else
    acodec=(acodec aac)
fi

if [[ -z "${preset}" ]]
then
    preset=medium
fi

if [[ -z "${crf}" ]]
then
    crf=23
fi

if [[ -z "${1}" ]]
then
    echo "No input file specified. See '${0} -h'"
    exit 1
fi

exit=0
input=(${@})

for file in ${input}
do
    if ! [[ -f "${file}" ]]
    then
        exit=1
        echo "No file found: ${file}"

        continue
    fi

    output="${file%.*}_t.mp4"
    echo "Re-encoding: ${file}"

    if ffmpeg -i "${file}" \
        -loglevel ${loglevel} \
        -map_metadata -1 \
        -profile:v high \
        -vcodec libx264 \
        -vf "fps=${fps},${filters}" \
        -pix_fmt yuv420p \
        -${acodec[@]} \
        -preset ${preset} \
        -level 4.0 \
        -crf ${crf} \
        -movflags +faststart \
        "${output}" -y 2>&1
    then
        if (( delete == 1 ))
        then
            rm -f "${file}"
            mv "${output}" "${file%.*}.mp4"

            echo "Succesfully re-encoded: ${file%.*}.mp4"
        else
            echo "Succesfully re-encoded: ${output}"
        fi
    else
        exit=1
        echo "Failed to re-encode: ${file}"
    fi
done

exit ${exit}