如何转播电视信号并通过网页观看

前几天给大家简单介绍了一下如何使用树莓派搭建一个RTMP直播服务,并可以通过vlc等工具观看。这几天,我又改进了一下整个系统,现在可以通过网页观看直播,并通过MQTT服务控制一个Arduino发送红外信号,控制机顶盒。

现在,整套系统有这么几个东西。

  • 机顶盒
    • 通过网络接受直播信号
  • 采集卡
    • 采集机顶盒信号,并推送给PC
  • PC
    • 接受来自采集卡的信号,并推送给RTMP直播服务
  • 树莓派
    • RTMP服务,接收并转发视频信号
    • MMQT服务,通过网络发送信息给Arduino
    • PHP服务,通过网页收看直播,发送MMQT信息
  • Ardunio
    • 接收MMQT的信息,并根据信息发送红外信号控制机顶盒

机顶盒,采集卡,PC方面的事情就不详细说了,今天主要说一下树莓派和Ardunio的东西。

树莓派方面,还是给大家写了一个script,大家修改后可以直接运行。我也加了一些comment在里面。注意替换<your ip>, <your port>, <your service>之类的位置,运行之前记得搜一下有没有“<your”开头的地方,还有没有没有替换的

#!/bin/bash

# Raspberry Pi 5 RTMP Streaming Server Setup Script
# Installs NGINX with RTMP, configures HLS, and sets up systemd service
# Installs PHP and MMQT service

set -e

echo "🚀 Updating system..."
apt update && apt upgrade -y

echo "📦 Installing dependencies..."
apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev git wget


echo "📦 Installing mosquitto and mosquitto-clients..."
apt install -y mosquitto mosquitto-clients

echo "🔁 Enabling and starting mosquitto service..."
systemctl enable mosquitto
systemctl start mosquitto

echo "⚙️  Configuring Mosquitto for WebSockets..."
MOSQ_CONF="/etc/mosquitto/mosquitto.conf"

# Backup original config
cp $MOSQ_CONF ${MOSQ_CONF}.bak

cat <<EOF > $MOSQ_CONF
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

# MQTT listener
listener 1883
protocol mqtt

# WebSocket listener
listener 9001
protocol websockets

allow_anonymous true

# Optional authentication (uncomment below to enable)
# allow_anonymous false
# password_file /etc/mosquitto/passwd
EOF

echo "🔁 Restarting Mosquitto..."
systemctl restart mosquitto

echo "📦 Installing PHP and FPM..."
apt install -y php php-fpm php-cli

echo "✅ Ensuring PHP-FPM is running..."
# Double check the version of php
systemctl enable php8.2-fpm
systemctl start php8.2-fpm

echo "🌐 Downloading NGINX and RTMP module..."
cd /opt
git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.27.5.tar.gz
tar -zxvf nginx-1.27.5.tar.gz
cd nginx-1.27.5

echo "⚙️ Building NGINX with RTMP module..."
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
make -j$(nproc)
make install

echo "📝 Writing NGINX config with RTMP + HLS..."
cat > /usr/local/nginx/conf/nginx.conf <<'EOF'
user www-data;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    sendfile off;
    tcp_nopush on;
    directio 512;

    server {
        listen <your port>;
        root html;
    index index.php index.html index.htm;
        location / {
            try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;  # Update based on your PHP version
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*';
        }

    }
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application <your service> {
            live on;
            record off;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 3s;
            hls_playlist_length 60s;
        }
    }
}
EOF

echo "📁 Creating HLS output directory..."
mkdir -p /tmp/hls

echo "🔧 Creating systemd service for NGINX RTMP..."
cat > /etc/systemd/system/nginx-rtmp.service <<EOF
[Unit]
Description=NGINX RTMP Streaming Server
After=network.target

[Service]
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PIDFile=/usr/local/nginx/logs/nginx.pid
Type=forking
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

echo "🔁 Enabling and starting NGINX service..."
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable nginx-rtmp
systemctl start nginx-rtmp


echo "📄 Creating MQTT PHP script..."
cat > /usr/local/nginx/html/send.php <<'EOF'
<?php
// Simple security token (optional)
$secret = $_POST['key'] ?? '';
if ($secret !== '<your key2>') {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

$msg = $_POST['msg'] ?? '';

// Publish to Mosquitto
$cmd = escapeshellcmd("mosquitto_pub -h localhost -t 'tv/remote' -m '$msg'");
exec($cmd, $output, $retval);

echo json_encode([
    'status' => $retval === 0 ? 'sent' : 'error',
    'msg' => $msg
]);
?>
EOF

cat > /usr/local/nginx/html/index.php <<'EOF'
<?php
$key = $_GET['key'] ?? '';
if ($key !== '<your key1>') {
    http_response_code(403);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}
$channels = [
    '体育频道' => [
        ['msg' => 's00720', 'name' => '纬来体育'],
        ['msg' => 's00721', 'name' => '纬来体育 备用'],
        ['msg' => 's01420', 'name' => 'ELTA体育1'],
        ['msg' => 's01421', 'name' => 'ELTA体育1 备用'],
        ['msg' => 's01430', 'name' => 'ELTA体育2'],
        ['msg' => 's01431', 'name' => 'ELTA体育2 备用'],
        ['msg' => 's01440', 'name' => 'ELTA体育3'],
        ['msg' => 's01441', 'name' => 'ELTA体育3 备用1'],
        ['msg' => 's01442', 'name' => 'ELTA体育3 备用2'],
        ['msg' => 's01450', 'name' => 'ELTA体育4'],
        ['msg' => 's01451', 'name' => 'ELTA体育4 备用'],
    ],
    'Reboot' => [
        ['msg' => 'r', 'name' => 'Reboot TV Box'],
        ['msg' => 'a', 'name' => 'Reboot TV App']
    ]
];

$secretKey = '<your key2>';
$streamKey = '<your stream key>';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TV Streaming Panel</title>
    <style>
        body {
            margin: 0;
            font-family: "Segoe UI", sans-serif;
            background: #1c1c1c;
            color: white;
            display: flex;
            flex-direction: row;
            height: 100vh;
        }

        #player-container {
            flex: 1;
            background: black;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        video {
            width: 100%;
            height: 100%;
            background: black;
        }

        #sidebar {
            width: 300px;
            background: #2b2b2b;
            border-left: 2px solid #444;
            overflow-y: auto;
            padding: 20px;
            box-shadow: -2px 0 10px rgba(0,0,0,0.5);
            transition: width 0.3s ease;
        }

        h2 {
            font-size: 20px;
            color: #00bfff;
            margin: 0 0 10px;
        }

        .category {
            margin-bottom: 10px;
        }

        .category-header {
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            padding: 8px;
            background-color: #333;
            border-radius: 4px;
            user-select: none;
        }

        .category-header .arrow {
            display: inline-block;
            width: 1.2em;
        }

        .channel-list {
            display: none;
            margin-top: 5px;
            margin-left: 10px;
        }

        .channel-btn {
            display: block;
            width: 100%;
            margin: 6px 0;
            padding: 10px;
            background: #00bfff;
            color: #fff;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            cursor: pointer;
            text-align: left;
        }

        .channel-btn:hover {
            background: #0099cc;
        }

        .status {
            margin-top: 20px;
            font-size: 14px;
            color: #ccc;
        }

        /* 📱 Responsive styles */
        @media screen and (max-width: 768px) {
            body {
                flex-direction: column;
            }

            #sidebar {
                width: 100%;
                border-left: none;
                border-top: 2px solid #444;
                padding: 10px;
            }

            h2 {
                font-size: 18px;
            }

            .channel-btn {
                font-size: 16px;
                padding: 12px;
            }

            .category-header {
                font-size: 18px;
                padding: 10px;
            }
        }
    </style>
</head>
<body>

<div id="player-container">
    <video id="video" controls autoplay muted></video>
</div>

<div id="sidebar">
    <h2>Channel List</h2>

    <?php foreach ($channels as $category => $items): ?>
        <div class="category">
            <div class="category-header" onclick="toggleCategory(this)">
                <span class="arrow">▶</span> <?php echo htmlspecialchars($category); ?>
            </div>
            <div class="channel-list">
                <?php foreach ($items as $channel): ?>
                    <button class="channel-btn" onclick="changeChannel('<?php echo $channel['msg']; ?>')">
                        <?php echo htmlspecialchars($channel['name']); ?>
                    </button>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endforeach; ?>

    <div class="status" id="status">Status: Ready</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
<script>
    const streamKey = "<?php echo $streamKey; ?>";
    const secretKey = "<?php echo $secretKey; ?>";
    const video = document.getElementById('video');
    const statusBox = document.getElementById('status');
    const src = 'http://<your ip>:<your port>/hls/' + streamKey + '.m3u8';

    if (Hls.isSupported()) {
        const hls = new Hls();
        hls.loadSource(src);
        hls.attachMedia(video);
    } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
        video.src = src;
    }

    function changeChannel(msg) {
        statusBox.textContent = "Sending msg: " + msg + "...";

        fetch('send.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            body: new URLSearchParams({
                msg: msg,
                key: secretKey
            })
        })
        .then(res => res.json())
        .then(data => {
            if (data.status === 'sent') {
                statusBox.textContent = "✅ Msg sent: " + msg;
            } else {
                statusBox.textContent = "❌ Failed: " + (data.error || 'Unknown error');
            }
        })
        .catch(err => {
            statusBox.textContent = "❌ Network error: " + err.message;
        });
    }

    function toggleCategory(header) {
        const list = header.nextElementSibling;
        const arrow = header.querySelector('.arrow');
        const open = list.style.display === 'block';

        list.style.display = open ? 'none' : 'block';
        arrow.textContent = open ? '▶' : '▼';
    }

    // Auto-expand the first category on load
    document.addEventListener("DOMContentLoaded", () => {
        const firstHeader = document.querySelector(".category-header");
        if (firstHeader) toggleCategory(firstHeader);
    });
</script>

</body>
</html>
EOF

echo ""
echo "✅ All done!"
echo ""
echo "🎥 Set OBS to stream to: rtmp://<your ip>/<your service>/<your stream key>"
echo "📺 Watch via VLC or player: rtmp://<your ip>/<your service>/<your stream key>"
echo "🌐 Or in browser (HLS): http://<your ip>:<your port>/hls/<your stream key>.m3u8"
echo "✅ Setup complete. Visit http://<your ip>:<your port>/index.php?key=<your key1> to test."

总体来讲,这个script会安装nginx,并且设置php,rtmp,mmqt服务。并通过一个简单的网页实现从网页上观看直播信号,并通过mmqt发送一些信息出去。不过网页的安全性不是很高,基本授权是通过get发送一个密码,并检查这个强密码和script里hard code的密码一样不一样。另外一个事就是,因为网页端使用的HLS服务,会有大概30秒的延迟。如果用VLC直接观看rtmp信号的话,延迟会小很多。

Ardunio方面,要做两件事,第一件事是通过IR receiver来学习遥控器的信号。

#include <IRremote.h>

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(8, ENABLE_LED_FEEDBACK); // Receiver connected to pin 8
  Serial.println("Ready to receive IR signal...");
}

void loop() {
  if (IrReceiver.decode()) {
    // Print full raw dump for replay later
    IrReceiver.printIRResultShort(&Serial);
    IrReceiver.printIRSendUsage(&Serial);
    Serial.println();
    IrReceiver.resume(); // Ready to receive the next
  }
}

首先,我是把接收器安装在了Ardunio的8号接口,这时,就可以用这个接收器看遥控器发出来的信号是什么了。比如,当我按确认键,就会收到一下信息

Protocol=NEC Address=0xFF40 Command=0xD Raw-Data=0xF20DFF40 32 bits LSB first Gap=1940000us Duration=68650us
	Send with: IrSender.sendNEC(0xFF40, 0xD, <numberOfRepeats>);

这时我们就知道,可以发送什么样的信号了。当学习完所有的需要按的信号后,就可以进入下一步。简单来说,就是连接WIFI(我用的Arduino 4 withi wifi),然后连接MMQT的服务(搭设在树莓派上的)。通过MMQT接收到什么信息,就解析出来,并执行相应的操作。比如我这里,s开头的信息,表示要选台,他会输入台号,并执行一些其他的操作。如果收到r,表示重启机顶盒,他会模拟遥控器的几次操作。这边可能就不能直接抄了,需要你自己修改一下。另外,当发送什么按键的时候,Arduino的灯就会亮起一个相应的数字或者按键,方便我知道发送的啥信号。

#include "Arduino_LED_Matrix.h"
#include <IRremote.h>
#include <PubSubClient.h>
#include "WiFiS3.h"
#include "secrets.h" 

ArduinoLEDMatrix matrix;
char ssid[] = <your wifi ssid>;        // your network SSID (name)
char pass[] = <your wifi pwd>;    // your network password (use for WPA)
int status = WL_IDLE_STATUS;

// MQTT broker settings
const char* mqtt_server = "<your ip>"; // your Raspberry Pi IP address
const int mqtt_port = 1883;
const char* mqtt_topic = "tv/remote";

WiFiClient wifiClient;
PubSubClient client(wifiClient);

void setup() {
  Serial.begin(9600);      // initialize serial communication

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  printWifiStatus();                        // you're connected now, so print out the status
  
  client.setServer(mqtt_server, mqtt_port);
  client.setCallback(callback);

  matrix.begin();
  IrSender.begin(7); // Transmitter LED connected to pin 7
}

void loop() {
  // Reconnect Wi-Fi if disconnected
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi disconnected. Attempting reconnection...");
    while (WiFi.status() != WL_CONNECTED) {
      status = WiFi.begin(ssid, pass);
      delay(10000); // Wait for connection
    }
    printWifiStatus(); // Print new status once connected
  }

  // Reconnect MQTT if needed
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

void callback(char* topic, byte* message, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("]: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
  }
  Serial.println();

  execute(String((char*)message));
}

void reconnect() {
  unsigned long startAttemptTime = millis();
  
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("arduinoClient")) {
      Serial.println("connected");
      client.subscribe(mqtt_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      delay(2000);

      // Timeout after 30 seconds
      if (millis() - startAttemptTime > 30000) {
        Serial.println("MQTT reconnect timeout");
        break;
      }
    }
  }
}

void execute(String message) {
  char c = message[0];
  int i;
  int j;
  int num;
  switch(c) {
    case 's': // channel selection
      for (i = 1; i < message.length(); i++) {
        c = message[i];
        if (i < 5) { // channel number, 4 digits
          sendSignal(c);
          delay(500);
        }
        if (i == 5) { // process the last digit
          sendSignal('o'); // press ok
          num = c - '0'; // convert char to int
          delay(3000);
          for (j = 0; j < num; j++) {
            sendSignal('d'); // press 'down' based on the last digit
            delay(500);
          }
          if (num > 0) { // if 'down' is pressed, press 'ok'
            sendSignal('o');
          } else {
            sendSignal('b'); // close menu
          }
        }
      }
      break;
    case 'r': // restart tv box
      sendSignal('p'); // open power menu
      delay(1000);
      sendSignal('d'); // press down
      delay(1000);
      sendSignal('d'); // select reboot
      delay(1000);
      sendSignal('o'); // reboot
      delay(45000);
      sendSignal('o');
      delay(2000);
      sendSignal('r');
      delay(1000);
      sendSignal('r');
      delay(1000);
      sendSignal('o'); // select tv app
      delay(1000);
      sendSignal('o'); // enter tv app
      delay(10000);
      sendSignal('b'); // enter tv
      break;
    case 'a': // restart tv app
      sendSignal('b'); // back
      delay(1000);
      sendSignal('u'); // select quit
      delay(1000);
      sendSignal('o'); // confirm quit
      delay(3000);
      sendSignal('o'); // select tv
      delay(1000);
      sendSignal('o'); // enter tv
      delay(10000);
      sendSignal('b'); // enter tv
      break;
  }
  
  delay(1000);
  clearScreen();
}

void sendSignal(char c) {
  switch(c) {
    case '0':
      show0();
      IrSender.sendNEC(0xFF40, 0x0, 1);
      break;
    case '1':
      show1();
      IrSender.sendNEC(0xFF40, 0x1, 1);
      break;
    case '2':
      show2();
      IrSender.sendNEC(0xFF40, 0x2, 1);
      break;
    case '3':
      show3();
      IrSender.sendNEC(0xFF40, 0x3, 1);
      break;
    case '4':
      show4();
      IrSender.sendNEC(0xFF40, 0x4, 1);
      break;
    case '5':
      show5();
      IrSender.sendNEC(0xFF40, 0x5, 1);
      break;
    case '6':
      show6();
      IrSender.sendNEC(0xFF40, 0x6, 1);
      break;
    case '7':
      show7();
      IrSender.sendNEC(0xFF40, 0x7, 1);
      break;
    case '8':
      show8();
      IrSender.sendNEC(0xFF40, 0x8, 1);
      break;
    case '9':
      show9();
      IrSender.sendNEC(0xFF40, 0x9, 1);
      break;
    case 'o':
      showOK();
      IrSender.sendNEC(0xFF40, 0xD, 1);
      break;
    case 'b':
      showBack();
      IrSender.sendNEC(0xFF40, 0x42, 1);
      break;
    case 'p':
      showPower();
      IrSender.sendNEC(0xFF40, 0x4D, 1);
      break;
    case 'u':
      showUp();
      IrSender.sendNEC(0xFF40, 0xB, 1);
      break;
    case 'd':
      showDown();
      IrSender.sendNEC(0xFF40, 0xE, 1);
      break;
    case 'l':
      showLeft();
      IrSender.sendNEC(0xFF40, 0x10, 1);
      break;
    case 'r':
      showRight();
      IrSender.sendNEC(0xFF40, 0x11, 1);
      break;
  }
}

void show0() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show1() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show2() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show3() {
  byte frame[8][12] = {
    { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show4() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show5() {
  byte frame[8][12] = {
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show6() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show7() {
  byte frame[8][12] = {
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show8() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void show9() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showOK() {
  byte frame[8][12] = {
    { 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0 },
    { 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
    { 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showBack() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showPower() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showUp() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showDown() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showLeft() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void showRight() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void clearScreen() {
  byte frame[8][12] = {
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  };
  matrix.renderBitmap(frame, 8, 12);
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

然后就没然后……不过下一步可能看看如何让网页的声音可以在后台播放。我现在用iOS和chrome,如果把直播画面放到画中画,就可以在后台继续听直播。但是如果不用画中画,当我把屏幕关闭的时候,声音就会中断了,体验挺不好的,反正……

如何一键使用树莓派搭建RTMP直播服务

10年前,我写过一篇文章,给大家介绍了一下如何用最低的成本转播PlayStation 4到国内的直播平台。简单来说,就是用PS4自带的直播功能,推流到twitch,并使用树莓派截取信号并重新推送到其他的直播平台。这样做的好处是,不需要购买昂贵的视频采集卡,只需要一个树莓派就可以了。

10年后,我又有了类似的需求,只不过这次,我想做的是自己搭建一个直播服务来输出视频信号,这样,无论我身处何地,都可以通过网络来观看自己的视频。

当然,和10年前不同的,这次我购买了昂贵的视频采集卡,以及新的树莓派。并且,当时我研究了很久如何配置服务器之类的,这次chatgpt帮我一波搞定,并且连脚本都给我写好了。稍微调整一下就能用。所以也在这里分享给大家。这次的思路是,用树莓派搭建一个RTMP服务,然后用我的台式机将视频采集卡采集的内容通过OBS推给树莓派,这样我就可以通过VLS甚至浏览器直接观看内容了。

#!/bin/bash

# Raspberry Pi 5 RTMP Streaming Server Setup Script
# Installs NGINX with RTMP, configures HLS, and sets up systemd service

set -e

echo "🚀 Updating system..."
apt update && apt upgrade -y

echo "📦 Installing dependencies..."
apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev git wget

echo "🌐 Downloading NGINX and RTMP module..."
cd /opt
git clone https://github.com/arut/nginx-rtmp-module.git
wget http://nginx.org/download/nginx-1.27.5.tar.gz
tar -zxvf nginx-1.27.5.tar.gz
cd nginx-1.27.5

echo "⚙️ Building NGINX with RTMP module..."
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module
make -j$(nproc)
make install

echo "📝 Writing NGINX config with RTMP + HLS..."
cat > /usr/local/nginx/conf/nginx.conf <<EOF
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    sendfile off;
    tcp_nopush on;
    directio 512;

    server {
        listen 8080;

        location / {
            root html;
            index index.html index.htm;
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*';
        }
    }
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 3s;
            hls_playlist_length 60s;
        }
    }
}
EOF

echo "📁 Creating HLS output directory..."
mkdir -p /tmp/hls

echo "🔧 Creating systemd service for NGINX RTMP..."
cat > /etc/systemd/system/nginx-rtmp.service <<EOF
[Unit]
Description=NGINX RTMP Streaming Server
After=network.target

[Service]
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PIDFile=/usr/local/nginx/logs/nginx.pid
Type=forking
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

echo "🔁 Enabling and starting NGINX service..."
systemctl daemon-reexec
systemctl daemon-reload
systemctl enable nginx-rtmp
systemctl start nginx-rtmp

echo ""
echo "✅ All done!"
echo ""
echo "🎥 Set OBS to stream to: rtmp://<RPI_IP_ADDRESS>/live/<key>"
echo "📺 Watch via VLC or player: rtmp://<RPI_IP_ADDRESS>/live/<key>"
echo "🌐 Or in browser (HLS): http://<RPI_IP_ADDRESS>:8080/hls/<key>.m3u8"

记得用sudo run一下就好了……这个脚本会帮你自动下载nginx和rtmp插件,自动编译和安装,自动配置,自动设置自动启动……

欢迎讨论……

棒球日记

最近MLB中国在搞活动,想要征集一些棒球方面的up主,打算建立一个棒球自媒体生态圈的样子。因为以前也做过一些相关的视频,所以就也报了个名。于是,突然想总结一些自己这些年为了棒球都做过些什么?

如果说第一件事,我觉得就是成立UIC棒球社了。肉壳接触棒球是很早的事情了,因为棒球在天津的发展是相当不错的,所以肉壳很小的时候就大致了解了棒球规则,也玩过一些FC和Genesis上的棒球游戏。不过第一次在现实中打棒球是10年暑假,在外院和老毕玩了玩。后来11年寒假的时候,借着去日本旅游的机会,买了个棒球手套,回到学校就成立了UIC棒球社。就算是在校内为棒球做了一个小宣传。

后来一件比较大的事情是13年在香港念书的时候,在网上看到了一个用白板解释棒球规则的视频,觉得很好玩,就花了点时间把这个视频传到了优酷上。可惜的是,那个账号因为我后来也不维护了,目前视频都被暂时屏蔽了。不过网上至今流传着很多未经授权的转载。差不多同一个时间段,13,14年的时候吧,我还从YouTube上搬运了一些台湾的棒球教学视频。如今也被暂时屏蔽中,哈哈哈。

在15年寒假的时候,我在家呆着无聊,正好那会儿新浪云的二级开发者可以免费试用一些资源。于是,我就用爬虫写了一个网站,用来爬取几个台湾网站的棒球专栏和棒球新闻。当时在朋友圈还小火了一阵,甚至还有朋友来找我谈合作,不过后来不了了之了。再后来,新浪云把二级开发者的每月免费额度取消了,我试着充值维持运营,但是成本有点高,就hold不住了。

再后来,17年底,冬天那会儿,不知道想起啥了,突然想搞搞游戏直播(其实第一次入坑是15年底,刚来美国那会儿),于是就借机在哔哩哔哩和微信公众号上,传了几个视频。恰逢那会儿是MLB世界大赛期间,于是传了几场我用MLB the Show打模拟赛的视频,以及用MLB the Show讲解比赛规则的视频。尤其是那个讲解规则的视频,还是挺受大家欢迎的。只不过,17年底,我又开始做播客了,所以游戏视频这部分就停掉了。还好播客还一直坚持做到现在,虽然听的人不多,但是就算是多个业余爱好。

19年底的时候,因为想要给播客做个网站,于是又买了个网站空间。然后发现新的空间其实权限还挺高,所以就把之前做的棒球新闻聚合站,改了改又上线了。但是上线了几个月,就出bug了。时至今日还没有修好。要不在此立个flag吧……尽快修……

基本上,这就是我在公共领域,为宣传棒球做的贡献了。其实19年买了个GoPro,想说有过有机会可以做做棒球相关的vlog,比如去球场的见闻等等,然后就疫情了……找谁说理去……

菜狗肉壳爱打机

大概两年前,肉壳写了一篇如何在用最低额外成本直播PS4游戏,之后确实直播了一段时间,大概1,2个月吧,后来就不了了之了,因为当时住的地方网络不太好,直播斗鱼会非常卡,所以就不想再搞了。

两年之后,因为使命召唤回归二战,所以突然又很想重新搞直播,来直播这款新作的剧情(虽然这款新作没什么剧情可言,单机剧情差不多5个小时就结束了),所以搞了一块采集卡,就又开始搞起来了……这次要同步搞很多东西,所以大家可以拿纸笔记一下,如果有兴趣可以来关注我的节目,我每周至少直播一次,有机会的话每天都会直播,只是因为人在北美,所以直播时间可能会是工作日的中午,大家可以当作午饭节目来看……

我打算继续使用之前的名字“菜狗肉壳爱打机”来作为这个栏目的名字,首先,我打游戏很菜,你看了就知道,所以不要期望我可以做出什么神操作,那是不可能的……如果你是为了看肉壳出丑,欢迎来搞……

下面是我会直播,以及上传视频的账号和地址。

斗鱼直播:rokeer

http://www.douyu.com/rokeer

微信公众号:菜狗肉壳爱打机(rouke_game)

Bilibili:菜狗肉壳爱打机

https://space.bilibili.com/33518839

欢迎大家关注点赞,拜托啦……

 

如何用最低的成本转播PlayStation 4到国内的直播平台

自从肉壳和基友团晚上PS4的GTA之后,就一直想要试试把我们的游戏和语音直播到网上去,但是由于网络条件的限制,只能悻悻作罢。不过现在我的网络条件好了很多,所以这个想法就又蹦出来了。所以刚刚搞了6个小时,终于把我的信号推去了斗鱼上……

http://www.douyutv.com/rokeer

PS4自带的转播功能其实也是很强大的,可以直接转播到Twitch和另外两个不存在的网站上……不过,想转播到国内网站上就没这么容易了,一个比较直观的解决方法就是用HDMI采集卡,然后把采集卡收集到的信息Push去stream上去。但是HDMI采集卡价格从几百块到上千块,真的是贵的买不起,便宜的不敢买。所以只能找找替代方案,查了很多资料,看了很多网站,终于找到了一个相对比较实惠的解决方案。就是通过PS4自带的直播到Twitch上的功能,通过某些手段,劫持到视频流信号,然后再推到斗鱼上面去。想实现这个功能,你只额外需要一件物品:

树莓派 2

如果你不知道什么是树莓派也没关系,其实就是一台廉价的小电脑,只要不到40美元就可以买到一台,刷个Linux系统进去,接上显示器就可以用了。当然,如果你用电脑的话,可以装Linux进去也可以,或者虚拟机装Linux貌似也可以。Windows的话,直接拉到最下面看连接,我自己没试过。

刷系统什么的不在今天的讨论范围内,直接开搞。首先,要安装NginX,负责接受视频流和推送。打开命令行执行下列命令

sudo apt-get -y install nginx
sudo apt-get -y remove nginx
sudo apt-get clean

首先安装nginx包,再删掉,目的是为了获得nginx的启动文件,然后还要手动清除/etc/nginx文件夹里面的内容。因为后面我们要用make去安装新版本的nginx,所以不清除原来的内容的话,新的文件是不会覆盖旧的文件的。

sudo apt-get update
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

安装必要的组建,如果有问题的话,可以再执行

sudo apt-get install -y curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev lib

不过我在安装的时候,会发生找不到lib,以及后续找不到openssl的问题,所以还是建议只执行上面的安装。

cd /usr/scr
sudo git clone git://github.com/arut/nginx-rtmp-module.git
sudo wget http://nginx.org/download/nginx-1.8.0.tar.gz
sudo tar xzf nginx-1.8.0.tar.gz
cd nginx-1.8.0

我们把自己下载的nginx 1.8和rtmp的module都放在了/usr/src文件夹下,你也可以放在别的地方,但是可能会影响到其他命令的执行,到时候要记得自己手动修改目录

./configure --prefix=/var/www --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --without-http_proxy_module --add-module=/usr/src/nginx-rtmp-module

注意,可能需要修改add-module的目录,如果你吧rtmp module放在其他的文件夹下了的话,然后执行

sudo mkdir -p /var/www

创建一个文件夹,用于放置一些网页文件

sudo make

这个时间会比较久,大概几分钟

sudo make install

安装,之后可以尝试执行

nginx -v
sudo service nginx start
sudo service nginx stop

查看nginx版本,启动服务,关闭服务。如果没有报错,就应该是已经安装上了。然后修改nginx的配置文件,位置应该是/etc/nginx/nginx.conf

user root;
#Root is only OK if the server is not public. Otherwise you need to increase security on your own.
# user www-data;
#use up to 4 processes if you expect allot of traffic. But this causes issues with rtmp /stat page and possibly pushing/pulling
#worker_processes 4;
worker_processes 1;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#if you want gzip enabled
#gzip on;
#gzip_disable “msie6”;

server {
listen 80;
server_name localhost;

# sample handlers
#location /on_play {
# if ($arg_pageUrl ~* localhost) {
# return 201;
# }
# return 202;
#}
#location /on_publish {
# return 201;
#}
#location /vod {
# alias /var/myvideos;
#}
# rtmp stat
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}

location /stat.xsl {
# you can move stat.xsl to a different location
root /usr/src/nginx-rtmp-module;
}

# rtmp control
location /control {
rtmp_control all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 131072;
max_message 256M;
ping 30s;
notify_method get;
application app{
live on;

# You can push this stream to an external rtmp server while accessible locally.
# If you experience artefacts and delays on external server lower the bitrate.
# There seems to be a bug. When watching local stream and pushing to remote, the remote
# stream become really weird with random blocks and strange shadows.(consider 1 one for now)
# push rtmp://ip-address-external-rtmp/app/stream;

# sample play/publish handlers
#on_play http://localhost:80/on_play;
#on_publish http://localhost:80/on_publish;
# sample recorder
#recorder rec1 {
# record all;
# record_interval 30s;
# record_path /tmp;
# record_unique on;
#}
# sample HLS
#hls on;
#hls_path /tmp/hls;
#hls_sync 100ms;

}
# Video on demand
#application vod {
# play /var/Videos;
#}
# Video on demand over HTTP
#application vod_http {
# play http://localhost:80/vod/;
#}
}
}

没有缩进,好丑……不过无所谓,这样配置就好了。其他的不多说,有两点要注意一下,第一是在rtmp里,有一行application app,后面的app一定不要改成其他名字,因为这是Twitch的视频流的推送标识(可能也不是这个词,总之不要改),其次,这个配置只是获取视频流并转发,后面我们可以用OBS来抓去并重新推送,当然,你也可以直接推送到斗鱼上去,只要application app里面live on;下面添加

push rtmp://send.douyu.tv/live/[STREAMKEY];

上面是斗鱼直播地址,[STREAMKEY]改成你的直播码。

然后记得

sudo service nginx start

启动服务,nginx就配置好了。这时候,如果你用浏览器直接访问你的服务器,比如http://192.168.0.16/stat 应该就可以看到一些内容,未来我们需要使用这个页面的内容来获取视频流相关的信息。至于如何获取服务器ip地址?ifconfig一下就知道了。

备注,按理说,机器重启之后,应该手动重启服务,或者写脚本自动启动,但是我在使用的时候,发现不手动重启也OK,所以就先不写教程了,后面的dnsmasq也有同样的问题。

然后我们开启系统的转发功能,修改/etc/sysctl.conf文件,设置net.ipv4.ip_forward=1,并执行

sysctl -p /etc/sysctl.conf

让更新生效。然后我们安装dnsmasq,执行

sudo apt-get install dnsmasq

就OK了。然后修改配置文件/etc/dnsmasq.conf,在相应位置添加,

address=/live.twitch.tv/192.168.0.16

live.twitch.tv是twitch的推送地址,192.168.0.16是我的服务器地址,不过只做这一步已经没有用了,而且我本人来讲,也不知道这一步是否是必须的,不过我做了,没有出现问题,所以就没有试不做会怎样。

sudo service dnsmasq start

启动服务就好了。

不过由于twitch现在修改了服务,所以只配置上面的dns解析是不够的,所以我们要执行

nslookup live.twitch.tv

可以查看live.twitch.tv都解析到哪里了。如果使用不了nslookup,输入

sudo apt-get install dnsutils

就可以安装nslookup命令了。然后,配置路由表的,用于把ip地址直接转发到服务器,因为我本人不是很懂iptables的配置,所以在网上找了三条命令。

sudo iptables -t nat -A PREROUTING -d 199.9.248.0/21 -p tcp --dport 1935 -j DNAT --to-destination 192.168.0.16:1935
sudo iptables -t nat -A PREROUTING -d 199.9.0.0/16 -p tcp --dport 1935 -j DNAT --to-destination 192.168.0.16:1935
sudo iptables -t nat -A POSTROUTING -j MASQUERADE

前两条的作用是把ip地址转到我的服务器,最后一条是把结果转回去。我试了各种命令都不对,就在我快要放弃的时候,试了一下只输入

sudo iptables -t nat -A PREROUTING -d 199.9.248.0/21 -p tcp --dport 1935 -j DNAT --to-destination 192.168.0.16:1935

竟然成功了。如果你有兴趣,或者你比较懂的话,可以试试修改这个命令,我就懒得改了。另外,iptables的配置,重启服务器后,肯定要重新配置的,这个没有疑问。

至此,服务器设置完成。

然后我们只要手动配置PS4获取ip地址,子网掩码,网关和dns服务器都设置成咱们自己的服务器也就是192.168.0.16就可以了。

然后,我们就可以使用Twitch,推送我们的视频信号了,具体操作不讲了。等看到屏幕右上角的On Air就表示已经在推送了。这时候,我们回到http://192.168.0.16/stat ,刷新一下就可以看到我们的nginx接受到了视频流的推送。

然后我们就可以用OBS接收这个视频流,地址格式是 rtmp://192.168.0.16/app/livexxxxxxxxxx,这个livexxxxxxxx就是你在http://192.168.0.16/stat看到那串码。然后你就可以用OBS,处理你的视频并推送到斗鱼直播上了。

就这么点事情,我前前后后搞了6个小时,不过终于搞定了,也是不错,如果你有什么问题,欢迎留言。转载请标明出处。

最后,我要感谢以下网页的作者,没有你们,我也不可能完成这件事,多亏了以你们的文章,我才能解决这些棘手的问题。Thanks!

http://bbs.a9vg.com/thread-4160686-1-1.html

http://bbs.a9vg.com/thread-4199530-1-1.html

http://pkula.blogspot.com/2013/06/live-video-stream-from-raspberry-pi.html

http://yiqingsim.net/post/103165692292/setting-up-dnsmasq-as-a-dnsdhcp-server-on-a

https://phelps.io/local-ps4-streaming/

目前这篇文章,没有排版,没有插图,有时间再做吧,已经凌晨3点了,睡觉了……

http://www.douyutv.com/

http://www.douyutv.com/