Why iPhone videos fail as Telegram video avatars
I uploaded a clip shot on my iPhone and nothing happened. No error, no spinner, the avatar just never appeared. The file was HEVC (H.265) inside a .MOV container. Telegram's video avatar pipeline wants H.264, and it drops anything else without telling you why.
I lost an evening blaming my Telegram client before I ran ffprobe and saw codec_name=hevc.
What the Telegram spec actually requires
These are the limits I encode. Anything outside them gets rejected or silently dropped.
- Codec: H.264 video, no HEVC.
- Resolution: 800x800, square.
- Duration: 10 seconds max.
- Size: 2 MB max.
- Audio: none. The track has to go.
The square rule matters more than it looks. Portrait and landscape phone footage both need a center crop, not a letterbox with black bars.
Cropping and encoding with ffmpeg
The trick is two passes. The first pass runs cropdetect to find the active picture area, so black borders get cut. The second pass crops to that area, scales to fill 800x800, center crops the rest, and encodes.
# pass 1: find the active picture area in the first 10 seconds ffmpeg -hide_banner -t 10 -i in.mov -vf cropdetect=24:16:0 -f null - 2>&1 | grep -o 'crop=[0-9:]*' | tail -1 # pass 2: replace 1080:1080:0:420 with what pass 1 printed ffmpeg -y -hide_banner -i in.mov -t 10 -an \ -vf 'crop=1080:1080:0:420,scale=800:800:force_original_aspect_ratio=increase,crop=800:800,format=yuv420p' \ -c:v libx264 -preset medium -crf 28 \ -movflags +faststart avatar.mp4 A few flags do real work here. -an drops audio. format=yuv420p forces 8-bit 4:2:0, which is the most widely accepted pixel format, and it also converts 10-bit HEVC sources that would otherwise pass through in a pixel format the encoder setup doesn't expect. +faststart moves the moov atom to the front so the file starts playing before it fully downloads.
CRF 28 is my starting point. If the output goes over 2 MB, I bump CRF by 2 and encode again. Ten seconds at 800x800 usually fits, but noisy footage like crowds or water can push it over.
iPhone portrait clips carry a rotation tag. ffmpeg applies it by default, so the output comes out upright unless you use an old build or disable autorotate.
Wrapping it in an aiogram 3 handler
The bot side is short. It downloads the file, runs the conversion in a subprocess so the event loop keeps running, and replies with the result.
import asyncio import tempfile from pathlib import Path from aiogram import Bot, F, Router from aiogram.types import FSInputFile, Message router = Router() async def detect_crop(src: Path) -> str: proc = await asyncio.create_subprocess_exec( 'ffmpeg', '-hide_banner', '-t', '10', '-i', str(src), '-vf', 'cropdetect=24:16:0', '-f', 'null', '-', stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.PIPE, ) _, err = await proc.communicate() lines = [line for line in err.decode().splitlines() if 'crop=' in line] if not lines: return '' return lines[-1].split('crop=')[1].split(' ')[0] async def convert(src: Path, dst: Path) -> None: crop = await detect_crop(src) vf = 'scale=800:800:force_original_aspect_ratio=increase,crop=800:800,format=yuv420p' if crop: vf = f'crop={crop},' + vf proc = await asyncio.create_subprocess_exec( 'ffmpeg', '-y', '-hide_banner', '-i', str(src), '-t', '10', '-an', '-vf', vf, '-c:v', 'libx264', '-preset', 'medium', '-crf', '28', '-movflags', '+faststart', str(dst), stdout=asyncio.subprocess.DEVNULL, stderr=asyncio.subprocess.DEVNULL, ) if await proc.wait() != 0: raise RuntimeError('ffmpeg failed') @router.message(F.video | F.animation) async def handle_video(message: Message, bot: Bot) -> None: media = message.video or message.animation with tempfile.TemporaryDirectory() as tmp: src = Path(tmp) / 'in.mov' dst = Path(tmp) / 'avatar.mp4' await bot.download(media, destination=src) await convert(src, dst) if dst.stat().st_size > 2 * 1024 * 1024: await message.reply('Result is over 2 MB, try a shorter clip.') return await message.reply_video(FSInputFile(dst)) Bots can't set your personal avatar, so the user saves the reply and sets it from the app. The snippet skips retries and queueing. Those are the parts that matter once more than one person is uploading at the same time.
How I packaged it as @liveavabot
The conversion is the easy part. The bot itself is mostly plumbing: upload handling, a size check, and error messages for files that won't fit. You send any video or GIF to @LiveAvaBot and get back an 800x800 H.264 file with audio removed.
Current numbers, roughly. 541 users in total, 3 active in the last 24 hours, 5 conversions in the same window, and 0 Stars revenue so far. Small, but the HEVC case is the one I built it for, and it's the one that kept failing silently for me.
Lessons and what's next
- cropdetect is a heuristic. A dark screen recording can get cropped too aggressively, so I check the output instead of trusting it blindly.
- Newer iPhones shoot 10-bit HEVC with HDR metadata. The format=yuv420p step handles the pixel format, but I haven't verified every HDR variant.
- Next up: a CRF retry loop, progress messages for longer clips, and a proper job queue.
- Not handled yet: 4K sources. Decode time dominates on a small VPS, and I haven't tuned for it.
Built by me: @liveavabot. If a file still gets rejected, paste the ffprobe output in the comments and I'll take a look.
![AMR Operations, Deployment, and MLOps — Ch.04: Fleet Management [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fphajz60uq731julzkt59.png)
![AMR Operations, Deployment, and MLOps — Ch.03: Remote Monitoring [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgf8gh1nt81hujq8e6uup.png)
![AMR Operations, Deployment, and MLOps — Ch.02: Field Operations [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkzb8hmbgu0kqd06z853j.png)
![AMR Operations, Deployment, and MLOps — Ch.01: Deployment Strategy [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3kfyl7aa0ui2y8vm900y.png)
