The upload that fails without an error

I wanted a video avatar for my own account. I recorded a short clip on an iPhone, uploaded it, and nothing happened. No error message, no preview. The avatar just stayed the same.

The clip was HEVC (H.265). iPhones record in that by default, and the avatar pipeline rejects it without telling you why. ffprobe showed the codec right away. Re-encoding to H.264 fixed it. This post covers that fix.

What the avatar format actually requires

The limits I built against are 800x800 pixels, H.264, no longer than 10 seconds, under 2 MB, and no audio track. Telegram doesn't put all of this in one place, so treat these as the values that worked in my testing, not a formal spec.

Check the input before you upload anything:

ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,width,height,duration -of default=nw=1 input.MOV 

File size is the limit that bites. A 10 second clip at a sane bitrate fits in 2 MB. A 60 fps clip at a high bitrate doesn't. So the encode step needs a hard bitrate cap, not just a quality setting.

Cropping and encoding with ffmpeg

Phone footage is landscape or portrait, almost never square. You need to crop, and you want the center cut, not a stretched frame. My approach is two steps. First, cropdetect finds black letterbox bars if there are any. Second, scale so both sides cover 800 pixels, crop the middle square, and encode.

CROP=$(ffmpeg -hide_banner -i input.MOV -t 3 -vf cropdetect=limit=24:round=2 -f null - 2>&1 | grep -o 'crop=[0-9:]*' | tail -n 1) ffmpeg -hide_banner -loglevel error -i input.MOV \ -vf "$CROP,scale=800:800:force_original_aspect_ratio=increase:flags=lanczos,crop=800:800,format=yuv420p" \ -t 10 -an \ -c:v libx264 -preset medium -crf 28 \ -maxrate 1500k -bufsize 3000k \ -movflags +faststart \ avatar.mp4 

The cropdetect pass samples the first 3 seconds and prints crop=w:h:x:y. I grab that with grep. If the video has no bars, cropdetect returns the full frame, so the crop does nothing. If it returns nothing at all on your machine, drop the $CROP, part, because an empty variable leaves a dangling comma and ffmpeg will refuse the filter chain.

force_original_aspect_ratio=increase scales the short side up to 800, then crop=800:800 takes the middle square. -an drops audio. -t 10 trims at ten seconds. That trim is silent, so a longer clip just gets cut.

format=yuv420p matters. iPhone HEVC often arrives 10-bit, and yuv420p gives you 8-bit H.264 that every client decodes. The maxrate and bufsize pair caps the bitrate. 1500k over 10 seconds is about 1.9 MB, which stays under the 2 MB limit with container overhead. The crf value sets the quality target, but the cap decides the size.

Wrapping it in an aiogram 3 handler

The bot side is simple. Telegram's Bot API lets you download files up to 20 MB, so anything bigger has to be resent smaller. A document can be any file type, so I check the MIME type before touching it.

import asyncio import tempfile from pathlib import Path from aiogram import Bot, F, Router from aiogram.types import BufferedInputFile, Message router = Router() VF = 'scale=800:800:force_original_aspect_ratio=increase,crop=800:800,format=yuv420p' MAX_BYTES = 2 * 1024 * 1024 @router.message(F.video | F.animation | F.document) async def make_avatar(message: Message, bot: Bot) -> None: media = message.video or message.animation or message.document if message.document and not (message.document.mime_type or '').startswith('video/'): return status = await message.reply('Converting, give me a few seconds.') with tempfile.TemporaryDirectory() as tmp: src = Path(tmp) / 'in.bin' dst = Path(tmp) / 'avatar.mp4' tg_file = await bot.get_file(media.file_id) await bot.download_file(tg_file.file_path, destination=src) proc = await asyncio.create_subprocess_exec( 'ffmpeg', '-y', '-hide_banner', '-loglevel', 'error', '-i', str(src), '-vf', VF, '-t', '10', '-an', '-c:v', 'libx264', '-preset', 'medium', '-crf', '28', '-maxrate', '1500k', '-bufsize', '3000k', '-movflags', '+faststart', str(dst), ) await proc.wait() if proc.returncode != 0 or not dst.exists(): await status.edit_text('ffmpeg could not convert this file.') return data = dst.read_bytes() if len(data) > MAX_BYTES: await status.edit_text('Result is over 2 MB, try a shorter clip.') return await message.reply_video(BufferedInputFile(data, filename='avatar.mp4')) await status.delete() 

The handler blocks nothing. ffmpeg runs as an async subprocess, so other updates keep flowing while a conversion runs. The temp directory cleans itself up when the block exits.

How this became @liveavabot

The snippet above is a stripped-down version of what runs behind @LiveAvaBot. You send it a video or a GIF, the bot runs the same pipeline, and you get back an MP4 you can set as a video avatar. I wrote the wrapper. ffmpeg does all the real work.

Lessons and edge cases

HDR footage is the weak spot. Converting 10-bit HDR to 8-bit SDR without tone mapping gives washed-out colors. I haven't added a tone-mapping step yet, so some HDR clips come out flat.

Portrait iPhone clips carry a rotation flag. ffmpeg applies it by default, so the output is upright without extra work. I'd still test with a real portrait clip before trusting it.

Long 4K clips are slow. HEVC decoding dominates the runtime, so a 4K file takes much longer than the 10 seconds you keep. Trimming the source first is the obvious fix if this becomes a problem.

What's next

The biggest gap is crop position. Right now a landscape clip always gives you the center. I want a small picker so you can choose left, center, or right before the encode runs. That's an inline keyboard and a different crop offset, nothing exotic.

Built by me, @liveavabot. If a codec or container breaks the crop step for you, tell me which one and I'll add it to the test set.