静看光阴荏苒
不管不顾不问不说也不念

FFmpeg循环推流脚本

撸了个FFmpeg的循环推流脚本,你的VPS除了吃灰以外还能24小时不间断直播视频。自己用的脚本,单纯分享一下,如有问题自行解决。

需要配合screen运行:

yum -y install screen

开个新窗口:

screen -S stream

功能:

1.目前支持循环推流mp4格式的视频,注意视频文件的名字不能含有空格或其他特殊符号。

2.视频加水印,水印位置默认在右上角。

完整代码如下:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#=================================================================#
#   System Required: CentOS7 X86_64                               #
#   Description: FFmpeg Stream Media Server                       #
#   Author: LALA                                    #
#   Website: https://www.lala.im                                  #
#=================================================================#

# 颜色选择
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
font="\033[0m"

ffmpeg_install(){
# 安装FFMPEG
read -p "你的机器内是否已经安装过FFmpeg4.x?安装FFmpeg才能正常推流,是否现在安装FFmpeg?(yes/no):" Choose
if [ $Choose = "yes" ];then
	yum -y install wget
	wget --no-check-certificate https://www.johnvansickle.com/ffmpeg/old-releases/ffmpeg-4.0.3-64bit-static.tar.xz
	tar -xJf ffmpeg-4.0.3-64bit-static.tar.xz
	cd ffmpeg-4.0.3-64bit-static
	mv ffmpeg /usr/bin && mv ffprobe /usr/bin && mv qt-faststart /usr/bin && mv ffmpeg-10bit /usr/bin
fi
if [ $Choose = "no" ]
then
    echo -e "${yellow} 你选择不安装FFmpeg,请确定你的机器内已经自行安装过FFmpeg,否则程序无法正常工作! ${font}"
    sleep 2
fi
	}

stream_start(){
# 定义推流地址和推流码
read -p "输入你的推流地址和推流码(rtmp协议):" rtmp

# 判断用户输入的地址是否合法
if [[ $rtmp =~ "rtmp://" ]];then
	echo -e "${green} 推流地址输入正确,程序将进行下一步操作. ${font}"
  	sleep 2
	else  
  	echo -e "${red} 你输入的地址不合法,请重新运行程序并输入! ${font}"
  	exit 1
fi 

# 定义视频存放目录
read -p "输入你的视频存放目录 (格式仅支持mp4,并且要绝对路径,例如/opt/video):" folder

# 判断是否需要添加水印
read -p "是否需要为视频添加水印?水印位置默认在右上方,需要较好CPU支持(yes/no):" watermark
if [ $watermark = "yes" ];then
	read -p "输入你的水印图片存放绝对路径,例如/opt/image/watermark.jpg (格式支持jpg/png/bmp):" image
	echo -e "${yellow} 添加水印完成,程序将开始推流. ${font}"
	# 循环
	while true
	do
		cd $folder
		for video in $(ls *.mp4)
		do
		ffmpeg -re -i "$video" -i "$image" -filter_complex overlay=W-w-5:5 -c:v libx264 -c:a aac -b:a 192k -strict -2 -f flv ${rtmp}
		done
	done
fi
if [ $watermark = "no" ]
then
    echo -e "${yellow} 你选择不添加水印,程序将开始推流. ${font}"
    # 循环
	while true
	do
		cd $folder
		for video in $(ls *.mp4)
		do
		ffmpeg -re -i "$video" -c:v copy -c:a aac -b:a 192k -strict -2 -f flv ${rtmp}
		done
	done
fi
	}

# 停止推流
stream_stop(){
	screen -S stream -X quit
	killall ffmpeg
	}

# 开始菜单设置
echo -e "${yellow} CentOS7 X86_64 FFmpeg无人值守循环推流 For LALA.IM ${font}"
echo -e "${red} 请确定此脚本目前是在screen窗口内运行的! ${font}"
echo -e "${green} 1.安装FFmpeg (机器要安装FFmpeg才能正常推流) ${font}"
echo -e "${green} 2.开始无人值守循环推流 ${font}"
echo -e "${green} 3.停止推流 ${font}"
start_menu(){
    read -p "请输入数字(1-3),选择你要进行的操作:" num
    case "$num" in
        1)
        ffmpeg_install
        ;;
        2)
        stream_start
        ;;
        3)
        stream_stop
        ;;
        *)
        echo -e "${red} 请输入正确的数字 (1-3) ${font}"
        ;;
    esac
	}

# 运行开始菜单
start_menu

效果:

赞(139)
未经允许不得转载:荒岛 » FFmpeg循环推流脚本
分享到: 更多 (0)

评论 71

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    什么播放器好像YouTube :smile:

    zturns5年前 (2019-01-21) Chrome 71.0.3578.89 Chrome 71.0.3578.89 iPhone iOS 12.1 iPhone iOS 12.1回复
    • 应该就是上一篇的自建hls推流频道。播放器应该是Dplayer

      刀下的小猪5年前 (2019-01-21) Google Chrome 71.0.3578.98 Google Chrome 71.0.3578.98 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 嗯,这个播放器搞下可以支持弹幕,挺不错的。

        LALA5年前 (2019-01-22) Google Chrome 70.0.3538.110 Google Chrome 70.0.3538.110 Windows 10 x64 Edition Windows 10 x64 Edition回复
  2. #2

    这次竟然不是丝袜妹子 :oops:

    唐风月5年前 (2019-01-22) Google Chrome 71.0.3578.80 Google Chrome 71.0.3578.80 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 其实都是大屌萌妹,你被照骗了。

      LALA5年前 (2019-01-22) Google Chrome 70.0.3538.110 Google Chrome 70.0.3538.110 Windows 10 x64 Edition Windows 10 x64 Edition回复
  3. #3

    博主,更换了dns以后,可以直接访问你的博客了! :wink:

    小白5年前 (2019-01-22) Firefox 60.0 Firefox 60.0 GNU/Linux x64 GNU/Linux x64回复
    • 顶呱呱呀。 :wink:

      LALA5年前 (2019-01-22) Google Chrome 70.0.3538.110 Google Chrome 70.0.3538.110 Windows 10 x64 Edition Windows 10 x64 Edition回复
  4. #4

    我要看妹子

    Rat’s5年前 (2019-01-25) Google Chrome 71.0.3578.99 Google Chrome 71.0.3578.99 Nokia Nokia回复
    • 西湖美景三月天嘞

      LALA5年前 (2019-01-25) Google Chrome 70.0.3538.110 Google Chrome 70.0.3538.110 Windows 10 x64 Edition Windows 10 x64 Edition回复
  5. #5

    循环推流是支持一个视频循环播放还是一个文件夹里面的视频顺序播放?

    qianmianyao5年前 (2019-02-28) WebView 4.0 WebView 4.0 Android 8.1.0 Android 8.1.0回复
    • 一个文件夹内的所有视频循环播放。

      LALA5年前 (2019-03-02) Google Chrome 71.0.3578.98 Google Chrome 71.0.3578.98 Windows 10 x64 Edition Windows 10 x64 Edition回复
  6. #6

    这个是可以推流到B站直播间吗?

    yusss5年前 (2019-08-22) Google Chrome 76.0.3809.100 Google Chrome 76.0.3809.100 Windows 10 x64 Edition Windows 10 x64 Edition回复
  7. #7

    大佬,应该如何在添加一个 当前电影片名 的提示信息?

    titil4年前 (2019-12-10) Google Chrome 78.0.3904.108 Google Chrome 78.0.3904.108 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 这些功能貌似用ffmpeg实现不了。。(可能是我不会)

      LALA4年前 (2019-12-10) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  8. #8

    大佬,提示错误:Video codec hevc not compatible with flv Could not write header for output file
    似乎是编码问题?

    eerdc4年前 (2019-12-17) Google Chrome 79.0.3945.79 Google Chrome 79.0.3945.79 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 应该是,你可以自己改一下ffmpeg后面的参数,这个可能有用:https://stackoverflow.com/questions/38309120/ffmpeg-mov-to-mp4-error-could-not-write-header-for-output-file-0-incorrect-co

      LALA4年前 (2019-12-20) Google Chrome 74.0.3729.169 Google Chrome 74.0.3729.169 Windows 10 x64 Edition Windows 10 x64 Edition回复
  9. #9

    牛掰 :mrgreen: ,待会测试一下可不可以在B站用,待会再看是否可以搞定 :smile: :mrgreen: :mrgreen:

    Utopia4年前 (2020-01-08) Google Chrome 72.0.3626.119 Google Chrome 72.0.3626.119 Windows 10 x64 Edition Windows 10 x64 Edition回复
  10. #10

    如何按照顺序播放?

    wodaini3年前 (2021-01-07) Google Chrome 87.0.4280.88 Google Chrome 87.0.4280.88 Mac OS X  10.15.5 Mac OS X 10.15.5回复
  11. #11

    想问下, 推流的视频好卡哦. 有没有办法控制一下上传的码率, 从而减少卡顿情况呢? :!: :!: :!:

    Tewboo1年前 (2022-12-22) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 可以的,ffmpeg有一个Preset的参数,可以设置编码速度:https://trac.ffmpeg.org/wiki/Encode/H.264

      LALA1年前 (2022-12-22) Google Chrome 86.0.4240.198 Google Chrome 86.0.4240.198 Windows 10 x64 Edition Windows 10 x64 Edition回复
  12. #12

    rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_1006862794_20751063&key=9a73bba14d1f23a6332061839d335397&schedule=rtmp&pflag=1: Input/output error
    这个error要怎么解决呀

    Jason1年前 (2022-12-23) Google Chrome 106.0.0.0 Google Chrome 106.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 我也遇到这个问题,你解决了吗

      张三1年前 (2023-01-09) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
      • 看我回复

        jona1年前 (2023-01-31) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
    • 看过AK视频过来这里集合了,我这也是同样的问题,希望博主给解答一下如何解决

      小司机1年前 (2023-01-11) Google Chrome 80.0.3987.163 Google Chrome 80.0.3987.163 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 看我回复。

        jona1年前 (2023-01-31) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
    • 看一下自己是不是海外的VPS,海外的IP在B站不让播,推流会出现这个错误。我也是经过10小时才捣腾明白。

      jona1年前 (2023-01-31) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
  13. #13

    大神 这个脚本是文件夹内的mp4文件循环播放吗 还是单一视频文件

    vv1年前 (2023-01-01) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
    • 文件夹内的所有mp4文件循环播放,如果文件夹内只有一个mp4文件那就只循环这一个视频。

      LALA1年前 (2023-01-02) Google Chrome 107.0.0.0 Google Chrome 107.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  14. #14

    博主,我自己抓源用PotPlayer录制了一段视频,格式选的是MP4,编码MPEG-4,但是运行脚本后,云显示推送了吧(应该吧忘了),但直播间却无法显示?

    还有经常性出现停播,我放了4个视频在VPS上的video文件夹分别以01,02,03,04格式都是MP4,但是脚本后常播放一个视频结束(也可能没结束),看时间像播放完一个视频,然后直播间因为断流关闭了?

    顺便博主有Ubuntu的文件夹循环脚本吗

    leo1年前 (2023-01-04) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 没有ubuntu的脚本,但是这个脚本按道理也可以用在ubuntu上面,你只需要自己手动安装一个ffmpeg,然后把脚本的这段函数删掉即可:ffmpeg_install()
      至于你说的这个直播间因为断流关闭了我就不太清楚是什么原因了。

      LALA1年前 (2023-01-04) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  15. #15

    能跑起来,不过遇到个问题:
    切换视频的时候,中间会有个切换过程,时间特别短,正常窗口和全屏是可以正常播放的。
    但如果是画中画模式,会导致退出小窗。

    怎么能缩短切换时间,使小窗不会自己退出?

    大西瓜1年前 (2023-01-04) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
  16. #16

    对了突然想了下,还没实验,博主可以用这命令推送到多个直播间吗??或者直接在是否水印这里循环下,直接在FFMEGP 前面加sudo nohup 可以直接挂后台推送到直播间,然后再新开finalshell再执行脚本,应该可以实现推送多个直播间吧??

    leo1年前 (2023-01-04) Google Chrome 108.0.0.0 Google Chrome 108.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  17. #17

    大佬问一下
    ls: cannot access ‘*.mp4’: No such file or directory
    -bash: e: command not found
    出现这个情况如何解决

    Lumine1年前 (2023-01-16) Firefox 102.0 Firefox 102.0 Windows 8.1 x64 Edition Windows 8.1 x64 Edition回复
    • 存视频的文件夹和ffmpeg放在同一根目录就行了

      ss1年前 (2023-04-16) Google Chrome 111.0.0.0 Google Chrome 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
      • 刚才打错了,视频文件夹和FFmpeg文件夹要在同一目录下,我这样就好了

        ss1年前 (2023-04-16) Google Chrome 111.0.0.0 Google Chrome 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  18. #18

    请问可以实现文件夹内视频文件随机播放吗

    aikyan1年前 (2023-01-17) Safari 16.1 Safari 16.1 iPad iOS 16.1 iPad iOS 16.1回复
  19. #19

    可以不可以一边放视频一边放音乐如何实现

    这次竟然不是丝袜妹子111年前 (2023-01-26) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  20. #20

    你好. 请问目前测试效果是 , 制定文件夹内n个视频随机播放 , 如何才能实现按照文件名顺序播放?

    Runner1年前 (2023-02-22) Google Chrome 110.0.0.0 Google Chrome 110.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
  21. #21

    请问aarch64架构的能直接套用吗

    rg1年前 (2023-03-03) Microsoft Edge 110.0.1587.54 Microsoft Edge 110.0.1587.54 Android 11 Android 11回复
  22. #22

    [tcp @0x23d7b70] Failed to resolve hostname live-push.bilivideo.com: Name or service not known[rtmp0x23d7c80] Cannot open connection tcp://live-push.bilivideo.com:1935
    rtmp:/live-push.bilivideo.con/live-bvc/7streamname-live_7975652_8468307&key=3b21074fcB4Le7e996e9dea592005bb28Eschedule=tmp&pflag=1: Input/output error 请问一下这是为什么呢

    rg1年前 (2023-03-05) Microsoft Edge 110.0.1587.63 Microsoft Edge 110.0.1587.63 Windows 10 x64 Edition Windows 10 x64 Edition回复
  23. #23

    关闭SSH就停止推流了? :cry:

    做个坏人啦1年前 (2023-03-19) Microsoft Edge 111.0.0.0 Microsoft Edge 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  24. #24

    ………….:cry:

    做个坏人啦1年前 (2023-03-19) Microsoft Edge 111.0.0.0 Microsoft Edge 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  25. #25

    TIKTOK 24小时直播可以用?

    黄大仙1年前 (2023-03-21) Google Chrome 103.0.5060.66 Google Chrome 103.0.5060.66 Windows 10 x64 Edition Windows 10 x64 Edition回复
  26. #26

    同问 抖音 Input/output error,怎么解决?

    leedong1年前 (2023-04-04) Google Chrome 111.0.0.0 Google Chrome 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  27. #27

    富强、民主、文明和谐真的YYDS

    沐妍1年前 (2023-04-11) Google Chrome 111.0.0.0 Google Chrome 111.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  28. #28

    为什么总是跑不通 :cry: ,结合你的代码和ak的视频,还是步步都是卡,可能我是小白吧 :eek:

    小试葵牛1年前 (2023-04-27) Safari 16.2 Safari 16.2 iPhone iOS 16.2 iPhone iOS 16.2回复
  29. #29

    我放文件比较小的视频他可以推流,放文件比较大的视频就不能推流是怎么回事

    1812个月前 (05-05) Firefox 112.0 Firefox 112.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  30. #30

    如何实现固定一个视频文件,循环推送目录下的音频文件呢?

    jianjin71211个月前 (05-20) Google Chrome 113.0.0.0 Google Chrome 113.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
  31. #31

    直接卡住了就一直放那几秒钟,怎么解决呢,

    DDSD10个月前 (06-29) Google Chrome 109.0.0.0 Google Chrome 109.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  32. #32

    大佬,就是这个视频已经解析出来了,但是在直播间一直播放不出来,我用的本地centos7,然后它最后一行显示frame= 7 fps=0.0 q=0.0 size= .KB time=00:00:00:00 bitrate=N/A speed=

    leoeric8个月前 (08-17) Microsoft Edge 115.0.1901.203 Microsoft Edge 115.0.1901.203 Windows 10 x64 Edition Windows 10 x64 Edition回复
  33. #33

    您好,想问下,跑的时候报 段错误 (核心已转储) 应该怎么修改

    问问问7个月前 (09-15) Google Chrome 116.0.0.0 Google Chrome 116.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
  34. #34

    为什么只播放那个文件夹里的一个视频呀

    calculus5个月前 (12-04) Microsoft Edge 119.0.2151.96 Microsoft Edge 119.0.2151.96 iPhone iOS 17.1 iPhone iOS 17.1回复
  35. #35

    请问如何直播非本地视频?有多个视频地址的url。怎么直播这些视频URL?

    kevin4个月前 (01-04) Google Chrome 120.0.0.0 Google Chrome 120.0.0.0 Mac OS X  10.15.7 Mac OS X 10.15.7回复
  36. #36

    你好,请问我可以修改脚本后和自己的文章一起发布在自己的博客吗?会注明原作者与链接。

    思维悦动2个月前 (02-18) Firefox 122.0 Firefox 122.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 可以的。

      LALA2个月前 (02-19) Google Chrome 121.0.0.0 Google Chrome 121.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复
    • 老哥博客数据库挂了 :shock:

      maniac3天前 Microsoft Edge 123.0.0.0 Microsoft Edge 123.0.0.0 Windows 10 x64 Edition Windows 10 x64 Edition回复

分享创造快乐

广告合作资源投稿