October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
DocumentaryTube
FFmpeg

How to Stream a YouTube Live Loop with a Linux Shell Script

A practical Linux shell-script method for looping a video into YouTube Live with FFmpeg, including stream-key safety, encoder settings, testing, and troubleshooting.

By DocumentaryTube Team 7 min read
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

You can loop a local video into YouTube Live from Linux with FFmpeg: give FFmpeg the stream URL and key from YouTube Live Control Room, then use -stream_loop -1 to repeat the file and -re to send it at its media rate. The script below is a starting point; test it with a private event and adjust its encoding settings for your source and upload connection.

What you need before starting

  • A Linux computer or server with FFmpeg installed and a reliable internet connection. The computer running FFmpeg must stay on and connected for the stream to continue.
  • A local video file you have the right to stream.
  • A YouTube channel eligible to livestream. YouTube says channels must be verified, have no live-streaming restrictions in the previous 90 days, and be operated by someone at least 16 years old. Check YouTube’s current live-streaming eligibility guidance.

Install FFmpeg through a trusted source for your Linux distribution, then check the installed build with ffmpeg -version. Available encoders vary by build; the example below requires the libx264 video encoder.

Get the YouTube stream URL and key

  1. Open YouTube Studio and enter Live Control Room. Create a stream or select a scheduled stream.
  2. Copy the server URL and stream key shown for that stream. Use the URL format displayed for your selected key; do not assume that every account or endpoint has the same format. YouTube recommends RTMPS for secure ingest. See Create a YouTube live stream with an encoder.
  3. Keep the key private. YouTube Help says, “Stream keys are like your YouTube stream’s password and address.” If it is exposed, reset it in YouTube Studio and update the encoder. See Manage live stream settings.

Write and run the Linux loop script

Save this as youtube-loop.sh. It reads the video path as its first argument, or uses loop.mp4 if you omit one. It expects the RTMPS server URL and stream key to be supplied as environment variables rather than written into the script.

#!/usr/bin/env bash
set -euo pipefail

: "${YOUTUBE_RTMPS_URL:?Set the stream URL from YouTube Live Control Room}"
: "${YOUTUBE_STREAM_KEY:?Set the stream key privately}"
INPUT=${1:-loop.mp4}

exec ffmpeg -re -stream_loop -1 -i "$INPUT" 
  -c:v libx264 -preset veryfast -pix_fmt yuv420p 
  -r 30 -g 60 -keyint_min 60 -sc_threshold 0 
  -b:v 2500k -maxrate 2500k -bufsize 5000k 
  -c:a aac -b:a 128k -ar 44100 
  -f flv "${YOUTUBE_RTMPS_URL%/}/${YOUTUBE_STREAM_KEY}"
  1. Save the file and make it executable: chmod +x youtube-loop.sh.
  2. Provide the URL and key privately in your shell environment, then run the script with the file path. For example: YOUTUBE_RTMPS_URL='URL_FROM_LIVE_CONTROL_ROOM' YOUTUBE_STREAM_KEY='YOUR_PRIVATE_KEY' ./youtube-loop.sh /path/to/video.mp4. Replace the quoted values with the exact values from Live Control Room. Avoid putting real credentials in shell history, a shared script, logs, or source control; use a protected environment or secret store appropriate to your setup.
  3. Watch FFmpeg’s output for connection or encoding errors, then check the preview and stream health in Live Control Room.

FFmpeg documents -stream_loop -1 as infinite input looping and -re as reading input at its native rate. See the FFmpeg documentation. This is a template, not a universal tested configuration. Its 30 fps, H.264, and bitrate values are illustrative of a 720p/30-style setup; match the bitrate and resolution to the actual source and a stable upload connection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

Choose encoder settings that fit your video

YouTube’s current encoder guidance recommends H.264 for RTMP/RTMPS, constant bitrate (CBR), a two-second keyframe interval (with a four-second maximum), and AAC or MP3 audio. For stereo audio, it lists 44.1 kHz and 128 Kbps. Consult YouTube’s encoder settings, bitrates, and resolutions and test the setup before relying on it.

Script setting What it does How to adapt it
-r 30 -g 60 -keyint_min 60 -sc_threshold 0 Sets 30 fps and a 60-frame GOP, corresponding to a two-second keyframe interval at 30 fps. For another frame rate, set the GOP length to approximately twice that rate for a two-second interval. Keep the keyframe interval within YouTube’s stated maximum.
-b:v 2500k -maxrate 2500k -bufsize 5000k Sets an illustrative video bitrate and rate-control buffer. Choose a bitrate supported by the source resolution and sustainable upload bandwidth; the values are not universal YouTube requirements.
-c:a aac -b:a 128k -ar 44100 Encodes audio as AAC at 128 Kbps and 44.1 kHz. Check that the input contains the audio you expect and that the stream’s audio is audible and in sync during testing.
-pix_fmt yuv420p Produces a broadly compatible pixel format for the encoded video. Check the output preview and stream health; compatibility and available formats depend on the FFmpeg build and source.

Test the event and start the live stream

  1. Use a controlled test event before publishing a long-running or public broadcast. Verify picture, sound, stream health, and that the file repeats as intended. YouTube recommends testing and monitoring; see its live-streaming tips.
  2. For a scheduled stream, encoder transmission and event activation can be separate steps. Wait for the Live Control Room preview, then use its Go live action unless you have deliberately enabled auto-start. Follow the settings for the specific event.
  3. When finished, stop FFmpeg deliberately and end the event using Live Control Room controls as appropriate. YouTube says streams under 12 hours are automatically archived; do not assume a single archive for a longer continuous loop. See YouTube’s encoder setup and archive guidance.

Keep the loop running: local Linux or hosted Linux

Where FFmpeg runs Availability considerations Trade-offs
Your Linux desktop or server Runs only while the machine, network connection, and FFmpeg process remain available. No separate hosting service is needed, but the computer must stay on and connected. You manage the operating system, process, and recovery.
An always-on hosted Linux instance Can keep the process away from home, subject to the host’s availability and configuration. Requires administration and has a hosting cost; no particular provider or price is established here. You remain responsible for FFmpeg and recovery.

FFmpeg’s formats documentation shows RTMP FIFO output options that can attempt recovery after temporary failures, but adding recovery logic does not guarantee that every interruption will recover. See FFmpeg Formats Documentation. Avoid -re for a live input; FFmpeg cautions that rate limiting a real-time input can cause packet loss. This script uses it for a local file.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

RTMPS or HLS?

For this simple shell-script file loop, use RTMPS by default: YouTube recommends it as RTMP over TLS/SSL, and it fits the FFmpeg RTMP output pattern above. YouTube also supports HLS for cases such as HDR or codecs not supported over RTMP, but segmented delivery has higher latency. Choose HLS only when the stream’s codec or other requirements call for it, and follow YouTube’s current ingest guidance rather than substituting an HLS URL into this RTMPS template.

Troubleshooting common failures

  • FFmpeg says the encoder is unavailable: Your installed build may not include libx264. Check ffmpeg -encoders and install a trusted build that includes a compatible H.264 encoder, or adjust the command to an encoder your build provides.
  • Connection fails immediately: Recheck the exact server URL and stream key for the selected YouTube stream, ensure the URL/key combination is assembled as expected by that endpoint, and confirm outbound network access. Reset an exposed key in Live Control Room and update the environment value.
  • The preview connects but the event is not public/live: A scheduled event may still need the preview and Go live action. Check whether auto-start is enabled for that stream.
  • Video stutters or YouTube reports poor stream health: Reduce the bitrate or resolution to a level the source and sustained upload connection can support, then test again. Check that the selected frame rate and keyframe interval are coherent.
  • Audio is missing, distorted, or out of sync: Confirm the source has the intended audio track, inspect FFmpeg output, and test the stream preview. Ensure the chosen audio codec and sample rate match the intended setup.
  • The stream stops after the loop runs for a while: Infinite file looping does not restart a failed FFmpeg process or repair every network interruption. Check the terminal or service logs and network, then restart the process; if you add FIFO recovery options, test their behavior rather than assuming uninterrupted recovery.
  • The archived video is shorter than the broadcast: YouTube’s automatic archive statement covers streams under 12 hours only. Plan separate, shorter events or another archive approach for longer continuous broadcasts.

Copyright and channel-policy checks

Only loop video and audio you have permission to broadcast. A file being available online or already uploaded to YouTube does not by itself establish that you may rebroadcast it. Review YouTube’s copyright and live-stream rules before scheduling, and consider how repetitive or reused material fits your channel and intended use. A looping script handles transmission; it does not grant rights or guarantee monetization or policy approval. Check the copyright safety checklist before going live.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or let it run in the cloud

If you would rather not keep Linux running at home, StreamNeo is a cloud service for keeping a YouTube channel live from uploaded videos. Upload a recording or build a playlist, add your YouTube stream key once, and go live. Nothing has to stay on at home; it loops from the cloud, supports uploaded quality up to 4K 60fps at one flat price per slot, and automatically recovers if YouTube drops the stream. The first day is free with no card. The monthly option is $9.99 per month. Start at StreamNeo’s free trial registration.

Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2Ă— USB C male to USB A female adapters and 2Ă— USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Screening Room

Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.