油管直播转推流到B站

ffmpeg下载地址: http://ffmpeg.zeranoe.com/builds/
youtube-dl下载地址: http://www.youtube-dl.org/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os,platform

def liveTrans(fromPath, toPath, ffmpegPath, youtubeDlPATH):

getRealFromPathCMD = youtubeDlPATH + " -g " + fromPath
realFromPath = os.popen(getRealFromPathCMD).read().strip()


if platformName[:7] == "Windows":
postCMD = ffmpegPath + ' -i "' + realFromPath + '" -vcodec copy -acodec aac -f flv "' + toPath + '"'
else:
postCMD = ffmpegPath + ' -i "' + realFromPath + '" -c:v copy -c:a aac -b:a 320k -ar 44100 -strict -2 -f flv "' + toPath + '"'
os.system(postCMD)


if __name__ == '__main__':
platformName = platform.platform()

if platformName[:7] == "Windows":
ffmpegPATH = r"D:\ProgramFiles\ffmpeg\bin\ffmpeg.exe"
youtubeDlPATH = r"D:\ProgramFiles\ffmpeg\bin\youtube-dl.exe"
else:
ffmpegPATH = "ffmpeg"
youtubeDlPATH = "youtube-dl"

ffmpegPath = "ffmpeg"
youtubeDlPath = "youtube-dl"
liveTrans(fromPath="frompath here",
toPath="toPath here",
ffmpegPath=ffmpegPATH, youtubeDlPATH=youtubeDlPATH)

emm,结果windows下报错连接不上youtube那个流的链接…新租的centos倒是可以…不知道是windows下的ffmpeg命令错了还是那个流不走代理…
QQ图片20190325184115.png

centos下安装ffmpeg和youtube-dl:

1
2
3
4
5
6
7
8
yum install -y epel-release
sudo rpm –import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
sudo rpm –import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
yum install -y ffmpeg

wget http://youtube-dl.org/latest/youtube-dl -O /usr/local/bin/youtube-dl
chmod a+x /usr/local/bin/youtube-dl

ffmpeg支持多个输出,linux下postCMD改为这个,边推流边保存到本地.

1
postCMD = ffmpegPath + ' -i "' + realFromPath + '" -c:v copy -c:a aac -b:a 320k -ar 44100 -strict -2 -f flv "' + toPath + '"' + ' -c:v copy -c:a aac -b:a 320k -ar 44100 -strict -2 -f flv "./tmp.flv"'

参考: https://www.bilibili.com/read/cv1083415/

顺便,ffmpeg裁剪视频(指时间上裁剪):

1
./ffmpeg -i inputFilePath -ss 05:00 -c copy outputFilePath