AndroidHDMI for Channels (ah4c): A virtual channel tuner using HDMI Encoder(s) + streaming stick(s)

And really - my input is to not introduce any more complexity to the structure. For my debugging Ill just spin up my other container with a pull and Ill be up and running pretty quickly

The decision is between you and @bnhf how you want to proceed with the gating.

My 2 cents - KEEP IT SIMPLE

I guess we chatting in parallel - I like this.

I just dont want to see you get derailed from your "stream of conciousness"/vibe coding with the closed caption stuff etc..

Cool. I can see a really slick common ui, eventually you can have a mix of adb and atv tuners in the tuner status page etc.

Eventually we can backfill the PYATV versions of missing pieces in the UI like the remote emulation buttons, etc. perhaps even ATV pairing.

I agree. My preference at this point would be a brief pause in changes, and if no "must fix" items pop-up, pull PR#20 in. This is a big PR, but once we get it merged, we should be able to return to some more manageable PR sizes.

There will always be new features that can be added or improved upon, but for now this seems to me to be a very nice upgrade, with plenty of changes for people to adsorb and utilize.

You've done an amazing job on this, and have really given a major face and functionality lift to this project!

2 Likes

Makes sense.

I'd be fine with that, as long as we continue to support env vars as well.

None of this should be part of PR#20 though! Bug fixes only to that PR please, so we can get it merged soon. The changes I've made to the beta branch on top of PR#20 should be easy to replicate, so I think rather than creating a PR from the beta branch, I'll delete it and merge your PR. Once that's done, I'll add my changes back -- which are script, yaml and env related.

Just mentioning something I was playing with this morning.

Channels DVR doesnt display closed captioning in Live multi view.

So this morning using the PYATV commands, I was able to create a toggle for each tuners broadcast closed captioning for use in live situations.

I implemented a Docker container running a lightweight Python HTTP webhook service, and leveraged the Apple TV accessibility shortcut set to closed captions. three quick menu presses toggles the built in CC

Then I created a couple of apple shortcuts use get contents of URL , POST for each tuner.

Ive got it working with an Apple Watch complication right now.

In my tuner case this is very stable, because the ATV app I use adheres to the apple system level CC control and never changes. Some other apps - like Netflix dont.

Im getting ready for all my live view sports that are starting in a few weeks!!!

For the main apple tv that i use to view channels on my couch , i leverage channels api to toggle cc

So we have the best of both worlds. Woohoo!

What are the resource requirements for adding CC to ah4c?

1 Like

Synology running (3) Ospreys total - most times 1 or 2

If it needs folders, I’ll have to create before.

CPU: 2 cores / Intel(R) Celeron(R) J4025 CPU @ 2.00GHz
4GB Ram
Synology DS220+

On the plus side, it does have an iGPU. :slight_smile:

In ah4c, FireTV with LinkPi encoder, everything is working fine with a sling.m3u lineup. Can I add a channel from an app outside of the Sling app? I need to launch the Tennis Channel app, click Down twice and hit Enter, for a manual launch of the Live feed. I wish there was a deeplink for that but I have no idea how to find it. Any help greatly appreciated, thanks.

If you have the Tennis channel app installed on your firestick you can query from the docker console using adb commands and find out the package to launch, and there are definitely adb commands to do the button presses.

If you add the tennis channel in your m3u you can then add a case statement in your bmitune.sh to check for that channel and hardcode the adb command sequence in the tuning script. Then you may have add something to do housekeeping for that channel in stopbmitune.sh

Thats one way to get it done. .

Uncle CLAUDE can walk you through this process.

If you get stuck i can lend a hand later

1 Like

There's a new way to do this, that I just tested. Here are the basics:

Use a STREAMER_APP= value of scripts/all/all

Create an M3U called all.m3u with whatever virtual channels you want. In your case, you'd start with the contents of sling.m3u, and add everything to all.m3u. For each tuning URL in all.m3u, you'll be adding the device and provider to that URL, in the form:

http://{{ .IPADDRESS }}/play/tuner/firetv~dtvstreamdeeplinks~KWGN~6d8950b7-cdb0-8ce8-7b86-1fb8a938cbbe

In other words, after the tuner/ part, add <device>~<provider~, just as they appear in your current scripts/<device>/<provider> value for STREAMER_APP.

Get it? :slight_smile:

The all/all scripts are dispatchers, and will use the scripts from <device>/<provider> to do the actual tuning. Via this approach, ah4c is no longer limited to a single provider.

Try this first for Sling only, and then you can create scripts for the Tennis Channel, along with adding an M3U record for that channel.

1 Like

Slick ideas!

Btw i did notice another possible artifact of this merge. It might have happened during the first pull though, a bunch of new M3Us got populated on top of my existing m3u directory. It didnt wipe out my custom one.

I have the environment variables to pull and update scripts and m3us set to false

1 Like

Thanks for all the ideas and help. I ended up mixing both sling and the Tennis Channel app in the same m3u using a random hex token. Then used if/else logic in bmitune.sh. Here's the script in case it helps anybody:

#!/bin/bash
#bmitune.sh for firetv/sling

set -x

channelID="$1"
specialID="$1"
streamerIP="$2"
streamerNoPort="${streamerIP%%:*}"
adbTarget="adb -s $streamerIP"
packageName=com.sling

if [ "$channelID" == "fc65b6abb023efabfaea08e47fe89beb" ]; then
  packageName=com.tennischannel.tceverywhere.amazon
fi

finish() {
  echo "bmitune.sh is exiting for $streamerIP with exit code $?"
}
trap finish EXIT

updateReferenceFiles() {
  mkdir -p $streamerNoPort
  [[ -f "$streamerNoPort/stream_stopped" ]] || echo 0 > "$streamerNoPort/stream_stopped"
  [[ -f "$streamerNoPort/last_channel" ]] || echo 0 > "$streamerNoPort/last_channel"
  echo $$ > "$streamerNoPort/bmitune_pid"
  echo "Current PID for this script is $$"
}

matchEncoderURL() {
  case "$streamerIP" in
    "$TUNER1_IP") encoderURL=$ENCODER1_URL ;;
    "$TUNER2_IP") encoderURL=$ENCODER2_URL ;;
    "$TUNER3_IP") encoderURL=$ENCODER3_URL ;;
    "$TUNER4_IP") encoderURL=$ENCODER4_URL ;;
    *) exit 1 ;;
  esac
}

activeAudioCheck() {
  local startTime=$(date +%s)
  local maxDuration=60
  local minimumLoudness=-50
  local sleepDuration=0.5

  while true; do
    checkLoudness=$(ffmpeg -t 1 -i $encoderURL -filter:a ebur128 -map 0:a -f null -hide_banner - 2>&1 | awk '/I:        /{print $2}')

    if (( $(date +%s) - $startTime > $maxDuration )); then
      echo "Active audio stream not detected in $maxDuration seconds."
      exit 1
    fi

    if (( $(echo "$checkLoudness > $minimumLoudness" | bc -l) )); then
      echo "Active audio stream detected with $checkLoudness LUF."
      break
    fi

    if appFocusCheck 0; then
      echo "Active audio stream not yet detected -- loudness is $checkLoudness LUF. Continuing..."
      sleep $sleepDuration
    else
      echo "No active audio stream detected and app is not in focus after $(($(date +%s) - $startTime)) seconds -- attempting to tune again..."
      tuneChannel
    fi
  done
}

appFocusCheck() {
  if [ "$channelID" == "fc65b6abb023efabfaea08e47fe89beb" ]; then
    return 0   # trust the audio-loudness check alone for this app; skip focus-based retune
  fi
  appFocus=$($adbTarget shell dumpsys window windows | grep -E 'mCurrentFocus' | cut -d '/' -f1 | sed 's/.* //g')
  if [[ $appFocus == $packageName ]]; then
    return 0
  else
    return 1
  fi
}

specialChannels() {
    if [ $specialID = "exit" ]; then
      echo "Exit $packageName requested on $streamerIP"
      rm $streamerNoPort/last_channel $streamerNoPort/adbAppRunning
      $adbTarget shell am force-stop $packageName
      exit 0
    elif [ $specialID = "reboot" ]; then
      echo "Reboot $streamerIP requested"
      rm $streamerNoPort/last_channel $streamerNoPort/adbAppRunning
      $adbTarget reboot
      exit 0
    elif [[ -f $streamerNoPort/adbCommunicationFail ]]; then
      rm $streamerNoPort/adbCommunicationFail
      exit 1
    else
      echo "Not a special channel (exit nor reboot)"
    fi
}

tuneChannel() {
  if [ "$channelID" == "fc65b6abb023efabfaea08e47fe89beb" ]; then
    $adbTarget shell am start -n com.tennischannel.tceverywhere.amazon/com.foxxum.webapp.MainActivity
    sleep 6
    $adbTarget shell input keyevent 20
    sleep 1
    $adbTarget shell input keyevent 66
  else
    $adbTarget shell am start -a android.intent.action.VIEW -d https://watch.sling.com/1/channel/$channelID/watch
  fi
}

main() {
  updateReferenceFiles
  matchEncoderURL
  specialChannels
  tuneChannel
  activeAudioCheck
}

main

And sling.m3u looks like this...

#EXTINF:-1 channel-id="ESPN2" channel-number="9993" tvc-guide-stationid="45507" tvg-group="" tvg-logo="",ESPN2
http://{{ .IPADDRESS }}/play/tuner/2ad976f9aa4b4796a52ae3d64b50db9c

#EXTINF:-1 channel-id="TENNIS" channel-number="9999" tvc-guide-stationid="60316" tvg-group="" tvg-logo="",Tennis Fire TV
http://{{ .IPADDRESS }}/play/tuner/fc65b6abb023efabfaea08e47fe89beb

etc...

All works as expected. I agree with @anon38303228, using different streamer apps (and ATV!) in the same container would be awesome.

Yes, and that's what I outlined for you. I probably should have specifically asked you to do it the way I described, as that's the current implementation for multi-app support -- and I'd appreciate additional testers. The way you did it is fine, but is not the way I'd recommend for people to approach this.

I'm leaning towards merging the PR#20 (our current :beta4) into main today. Last call for any show-stopper issues! Anyone?

This is now done. New bnhf/ah4c:latest incorporates all of the WebUI and Closed Captions work done by @anon38303228. Congratulations to him for an impressive update to ah4c!

Please consider the :appletv tag now deprecated, with :latest now a unified project. Be sure to use the latest Docker Compose going forward:

services:
  # 2026.08.20
  # GitHub home for this project with setup instructions: https://github.com/sullrich/ah4c
  # Docker Hub home for this project: https://hub.docker.com/repository/docker/bnhf/ah4c
  ah4c:
    image: bnhf/ah4c:${TAG:-latest}
    container_name: ah4c
    hostname: ah4c
    dns_search: ${DOMAIN:-localdomain} # Specify the name of your LAN's domain, usually local or localdomain
    runtime: ${DOCKER_RUNTIME:-runc} # Closed captions only. Set DOCKER_RUNTIME=nvidia for an NVIDIA GPU with the CUDA engine build. Requires the NVIDIA container toolkit
    devices:
      - ${GPU_DEVICE:-/dev/null} # Closed captions only. Set GPU_DEVICE=/dev/dri to let the Vulkan engine build use an Intel or AMD GPU. Left at the default it passes /dev/null, which always exists and does nothing
    ports:
      - ${HOST_PORT:-7654}:7654 # Port used by this ah4c proxy
    environment:
      - IPADDRESS=${IPADDRESS} # Hostname or IP address of this ah4c extension to be used in M3U file (also add port number if not in M3U)
      - NUMBER_TUNERS=${NUMBER_TUNERS} # Number of tuners you'd like defined - add a matching TUNERn_IP and ENCODERn_URL line below for each beyond 9
      - TUNER1_IP=${TUNER1_IP} # Streaming device #1 with adb port in the form hostname:port or ip:port
      - TUNER2_IP=${TUNER2_IP} # Streaming device #2 with adb port in the form hostname:port or ip:port
      - TUNER3_IP=${TUNER3_IP} # Streaming device #3 with adb port in the form hostname:port or ip:port
      - TUNER4_IP=${TUNER4_IP} # Streaming device #4 with adb port in the form hostname:port or ip:port
      - TUNER5_IP=${TUNER5_IP} # Streaming device #5 with adb port in the form hostname:port or ip:port
      - TUNER6_IP=${TUNER6_IP} # Streaming device #6 with adb port in the form hostname:port or ip:port
      - TUNER7_IP=${TUNER7_IP} # Streaming device #7 with adb port in the form hostname:port or ip:port
      - TUNER8_IP=${TUNER8_IP} # Streaming device #8 with adb port in the form hostname:port or ip:port
      - TUNER9_IP=${TUNER9_IP} # Streaming device #9 with adb port in the form hostname:port or ip:port
      - ENCODER1_URL=${ENCODER1_URL} # Full URL for tuner #1 in the form http://hostname/stream or http://ip/stream
      - ENCODER2_URL=${ENCODER2_URL} # Full URL for tuner #2 in the form http://hostname/stream or http://ip/stream
      - ENCODER3_URL=${ENCODER3_URL} # Full URL for tuner #3 in the form http://hostname/stream or http://ip/stream
      - ENCODER4_URL=${ENCODER4_URL} # Full URL for tuner #4 in the form http://hostname/stream or http://ip/stream
      - ENCODER5_URL=${ENCODER5_URL} # Full URL for tuner #5 in the form http://hostname/stream or http://ip/stream
      - ENCODER6_URL=${ENCODER6_URL} # Full URL for tuner #6 in the form http://hostname/stream or http://ip/stream
      - ENCODER7_URL=${ENCODER7_URL} # Full URL for tuner #7 in the form http://hostname/stream or http://ip/stream
      - ENCODER8_URL=${ENCODER8_URL} # Full URL for tuner #8 in the form http://hostname/stream or http://ip/stream
      - ENCODER9_URL=${ENCODER9_URL} # Full URL for tuner #9 in the form http://hostname/stream or http://ip/stream
      - STREAMER_APP=${STREAMER_APP} # Streaming device name and streaming app you're using in the form scripts/streamer/app (use lowercase with slashes between as shown)
      - PYATV=${PYATV:-false} # Set to TRUE to run docker-start-pyatv.sh at container start for Apple TV tuners via pyatv, instead of the default docker-start.sh used for adb-based tuners. Case-insensitive; anything else runs the default.
      - CHANNELSIP=${CHANNELSIP} # Hostname or IP address of the Channels DVR server itself
      - ALERT_SMTP_SERVER=${ALERT_SMTP_SERVER} # The domainname:port of the SMTP server you'll be using like smtp.gmail.com:587. This is for sending ah4c alerts if tuning fails.
      - ALERT_AUTH_SERVER=${ALERT_AUTH_SERVER} # The auth server for the e-mail you'll be using like smtp.gmail.com
      - ALERT_EMAIL_FROM=${ALERT_EMAIL_FROM} # The e-mail address you'd like your ah4c failure alert e-mails to show as being from.
      - ALERT_EMAIL_PASS=${ALERT_EMAIL_PASS} # Gmail and Yahoo both support the creation of app-specific e-mail passwords, and this is the way to go! It's NOT recommended to use your everyday e-mail password.
      - ALERT_EMAIL_TO=${ALERT_EMAIL_TO} # The e-mail address you'd like your alert e-mails sent to.
      - ALERT_WEBHOOK_URL=${ALERT_WEBHOOK_URL} # URL to GET when an alert fires (same failures that trigger the e-mail); put $reason in the URL and it's replaced with the URL-encoded message. Blank disables it.
      - LIVETV_ATTEMPTS=${LIVETV_ATTEMPTS} # For FireTV Live Guide tuning only, set maximum number of attempts at finding the desired channel
      - CREATE_M3US=${CREATE_M3US:-false} # Set to true to create device-specific M3Us for use with Amazon Prime Premium channels -- requires a FireTV device
      - UPDATE_SCRIPTS=${UPDATE_SCRIPTS:-true} # Set to true if you'd like the sample scripts and STREAMER_APP scripts updated whether they exist or not
      - UPDATE_M3US=${UPDATE_M3US:-true} # Set to true if you'd like the sample m3us updated whether they exist or not
      - TZ=${TZ} # Your local timezone in Linux "tz" format
      - SPEED_MODE=${SPEED_MODE:-false} # Set to false if you'd like the target streaming app to be closed after each tuning cycle (limited script support).
      - KEEP_WATCHING=${KEEP_WATCHING} # In supported scripts, set the delay before resending a tuning deeplink to prevent "Are you still watching?" type messages. Examples: Use 4h for 4 hours or 240m for 240 minutes.
      - AUTOCROP_CHANNELS=${AUTOCROP_CHANNELS} # Space separated list of channels (by number) with black borders on 4 sides to autocrop while maintaining aspect ratio. Requires LinkPi Encoder!
      - LINKPI_HOSTNAME=${LINKPI_HOSTNAME} # Hostname or IP of the LinkPi Encoder's web API. Required for AUTOCROP_CHANNELS.
      - LINKPI_USERNAME=${LINKPI_USERNAME} # Username for the LinkPi Encoder's web API. Required for AUTOCROP_CHANNELS.
      - LINKPI_PASSWORD=${LINKPI_PASSWORD} # Password for the LinkPi Encoder's web API. Required for AUTOCROP_CHANNELS; not currently read by the bundled scripts, which log in with the LinkPi default password instead.
      - USER_SCRIPT=${USER_SCRIPT} # Path to a custom script to run alongside ah4c at container startup. Blank runs nothing extra.
      - NULL_FRAME_INSERTION=${NULL_FRAME_INSERTION:-false} # Set to TRUE to fill encoder stalls with MPEG-TS NULL packets (PID 0x1FFF) so the DVR never sees a zero-byte gap mid-recording. Case-insensitive (true/True/TRUE all work); anything else, including 1/yes, leaves the feature off.
      - PLAYBACK_DETECTION=${PLAYBACK_DETECTION:-false} # Set to TRUE to hold the stream until the device reports media audio playing and the picture is actually moving, then start on a keyframe, so a recording begins on the program rather than on the app's loading screen. Requires adb access to the tuner; network tuners only. Case-insensitive (true/True/TRUE all work); anything else, including 1/yes, leaves the feature off.
      - PLAYBACK_DELAY=${PLAYBACK_DELAY} # Set to a whole number of seconds to skip the start of each tune, so a recording begins on the program rather than on the app's loading screen. Piped through the bundled ffmpeg with -ss and stream copy; no re-encoding, and the skip starts on the next keyframe so it can run slightly past the configured value. The value is the total tune time, scripts included. Supported range is 2 to 30, since the DVR allows a tune about 30 seconds; values outside the range are clamped and logged. Ignored when PLAYBACK_DETECTION is TRUE; network tuners only. 0 or unset leaves the feature off.
      - PLAYBACK_STATIC_TIMEOUT=${PLAYBACK_STATIC_TIMEOUT} # Only used with PLAYBACK_DETECTION=TRUE. How many seconds the box may keep the exact player and media session it already had before the check stops watching adb and gates on motion alone. The default is 2, which suits Ospreys; apps that hold one player across channel changes, like the DirecTV app, need more. 0 or unset uses the default.
      - HEARTBEAT_INTERVAL=${HEARTBEAT_INTERVAL:-0} # In supported scripts (currently osprey), seconds between keepalive keyevents sent during playback to stop the app's UI inactivity timer from resetting the stream. Set to 0 to disable.
      - NVIDIA_VISIBLE_DEVICES=${NVIDIA_VISIBLE_DEVICES} # Closed captions only. Set to all alongside DOCKER_RUNTIME=nvidia to expose an NVIDIA GPU. Empty means no GPU and is the default
      - NVIDIA_DRIVER_CAPABILITIES=${NVIDIA_DRIVER_CAPABILITIES} # Closed captions only. Set to compute,utility when using an NVIDIA GPU, so the driver the CUDA engine build needs is passed in
    volumes:
      - ${HOST_DIR}/ah4c/scripts:/opt/scripts # pre/stop/bmitune.sh scripts will be stored in this bound host directory under streamer/app
      - ${HOST_DIR}/ah4c/m3u:/opt/m3u # m3u files will be stored here and hosted at http://<hostname or ip>:7654/m3u for use in Channels DVR - Custom Channels settings
      - ${HOST_DIR}/ah4c/adb:/root/.android # Persistent data directory for adb keys
      - ${HOST_DIR}/ah4c/captions:/opt/captions # Closed caption settings, and the speech model, engine and any GPU driver downloaded from the Closed Captions page. Stays empty unless you turn captions on
    restart: unless-stopped

And here's a set of sample env var overrides:

TAG=latest
CONTAINER_NAME=ah4c
HOSTNAME=ah4c
DOMAIN=localdomain tailxxxxx.ts.net
DOCKER_RUNTIME=runc
GPU_DEVICE=/dev/dri
HOST_PORT=7654
IPADDRESS=docker6:7654
NUMBER_TUNERS=5
TUNER1_IP=firestick-desk1:5555
ENCODER1_URL=http://linkpi-encoder2:8090/stream0
TUNER2_IP=firestick-desk2:5555
ENCODER2_URL=http://linkpi-encoder2:8090/stream1
TUNER3_IP=firestick-desk3:5555
ENCODER3_URL=http://linkpi-encoder2:8090/stream2
TUNER4_IP=firestick-desk4:5555
ENCODER4_URL=http://linkpi-encoder2:8090/stream3
TUNER5_IP=firestick-desk5:5555
ENCODER5_URL=http://linkpi-encoder2:8090/stream4
STREAMER_APP=scripts/firetv/dtvstreamdeeplinks
PYATV=false
CHANNELSIP=media-server10
ALERT_SMTP_SERVER=smtp.gmail.com:587
ALERT_AUTH_SERVER=smtp.gmail.com
[email protected]
ALERT_EMAIL_PASS=xxxxxxxxxxxxxxxx
[email protected]
ALERT_WEBHOOK_URL=
LIVETV_ATTEMPTS=
CREATE_M3US=false
UPDATE_SCRIPTS=true
UPDATE_M3US=true
TZ=America/Denver
SPEED_MODE=false
KEEP_WATCHING=235m
AUTOCROP_CHANNELS=
LINKPI_HOSTNAME=
LINKPI_USERNAME=
LINKPI_PASSWORD=
USER_SCRIPT=
NULL_FRAME_INSERTION=false
PLAYBACK_DETECTION=true
PLAYBACK_STATIC_TIMEOUT=12
PLAYBACK_DELAY=
HEARTBEAT_INTERVAL=
NVIDIA_VISIBLE_DEVICES=
NVIDIA_DRIVER_CAPABILITIES=
HOST_DIR=/data
2 Likes

I echo this

Just updated here to latest tag, and looking great so far.

Congrats to BOTH of you!!!

1 Like

:sparkle: :dizzy: :boom: :lollipop: :sparkles: :tada: :tada: :tada:
perfect timing! I fixed my NanoA1bios :nerd_face:

Great work guys! now that i can use the interface a bit more I really like having all/most of what I need in 1 place. :slight_smile:

does not work for me, but i'm using HOST_DIR=/mnt/c/data
simply redeploying after portainer automatically removes the bad variable works fine. able to download drivers and use captions with GPU and they survive a Re-pull without the env variable.

also CDVR_M3U_NAME=dtv.m3u was missing from your sample but works :+1:
which leads us to the GUI not having a way to create or import a m3u, you can only edit the ones provided.

maybe you could/should include mine