参考:https://www.quarkbook.com/?p=733

材料

  • 树莓派(我用的4B)
  • 摄像头(ps3 eye)
  • bilibili直播认证

推流命令

  • 推流USB摄像头(包含音频,音频源为音频文件)
ffmpeg -thread_queue_size 512 -f video4linux2 -s 1280*720 -i "视频源" -stream_loop -1 -i "音频源" -vcodec h264_omx -pix_fmt yuv420p -r 30 -s 1280*720 -g 60 -b:v 10M -bufsize 10M -acodec aac -ac 2 -ar 44100 -ab 128k -f flv "推流地址"
  • 推流USB摄像头(包含音频,音频源麦克风)
ffmpeg -thread_queue_size 512 -f video4linux2 -s 1280*720 -i "视频源" -i "音频源" -vcodec h264_omx -pix_fmt yuv420p -r 30 -s 1280*720 -g 60 -b:v 10M -bufsize 10M -acodec aac -ac 2 -ar 44100 -ab 128k -f flv "推流地址"
  • 推流USB摄像头(不包含音频)
ffmpeg -thread_queue_size 512 -f video4linux2 -s 1280*720 -i "视频源" -vcodec h264_omx -pix_fmt yuv420p -r 30 -s 1280*720 -g 60 -b:v 10M -bufsize 10M -an -f flv "推流地址"
  • 推流视频
ffmpeg -re -i "视频源" -vcodec copy -acodec aac -b:a 192k -f flv "推流地址" 
// node 开机自启动脚本 (pm2)

const { exec } = require('child_process');

function play() {
  return new Promise((resolve, reject) => {
    const cmd = 'ffmpeg -thread_queue_size 512 -f video4linux2 -s 1280*720 -i "/dev/video0" -stream_loop -1 -i "./1.mp3" -vcodec h264_omx -pix_fmt yuv420p -r 30 -s 1280*720 -g 60 -b:v 10M -bufsize 10M -acodec aac -ac 2 -ar 44100 -ab 128k -f flv "推流地址"';
    exec(cmd, (error, stdout, stderr) => {
      if (error) {
        console.error(`exec error: ${error}`);
        return reject(`exec error: ${error}`);
      }
      console.log(`stdout: ${stdout}`);
      console.error(`stderr: ${stderr}`);
      resolve(`stdout: ${stdout}`);
    }); 
  });
}


const boot = async function() {
  try {
    const res = await play();
  } catch(e) {
    setTimeout(() => {
      boot(); 
    }, 5000);
  }
};


boot();