You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3调用FFmpeg实现RTSP转RTMP时遇语法错误求助

Hey Chris, let's work through this syntax error issue step by step—this is a common pitfall when mixing shell commands with Python string handling!

The Core Issues

Your error is likely coming from two key problems in how you're defining the FFmpeg command in Python:

  1. Quotation mark conflicts between your Python string and the RTSP URL's wrapping quotes
  2. Incorrect URL escaping: You're using HTML-style & instead of raw & in the RTSP link, which FFmpeg won't recognize as valid URL syntax

Fix 1: Resolve String Quotation Conflicts

When writing shell commands in Python, using triple quotes (either ''' or """) eliminates conflicts between inner and outer quotation marks. Here's a quick fix:

import subprocess

# Use triple single quotes to avoid quote clashes
ffmpeg_cmd = '''ffmpeg -i "rtsp://xx.xxx.xx.x:554/user=admin&password=123@&channel=1&stream=0.sdp" -f flv -r 25 -s 640x480 -an "rtmp://localhost:1935/uid/1/camera1"'''
subprocess.run(ffmpeg_cmd, shell=True)

Fix 2: Use a Safer Command Structure (Avoid shell=True)

A better practice is to split your command into a list of arguments. This avoids shell escaping entirely and reduces security risks:

import subprocess

# Split each command/parameter into a separate list element
ffmpeg_cmd = [
    'ffmpeg',
    '-i',
    'rtsp://xx.xxx.xx.x:554/user=admin&password=123@&channel=1&stream=0.sdp',
    '-f', 'flv',
    '-r', '25',
    '-s', '640x480',
    '-an',
    'rtmp://localhost:1935/uid/1/camera1'
]
# Add check=True to catch FFmpeg runtime errors early
subprocess.run(ffmpeg_cmd, check=True)

Critical Note: Fix the RTSP URL

You had & in your original RTSP link—this is an HTML escape sequence, not valid for a raw RTSP URL. Replace all instances of & with plain & so FFmpeg can correctly parse the stream parameters.

Quick Troubleshooting Tip

If you still run into issues, test the FFmpeg command directly in your Ubuntu terminal first. If it works there, the problem is definitely with how Python is handling the string/arguments. If it fails in the terminal, you'll need to debug the RTSP URL itself (check credentials, stream path, or camera accessibility).

内容的提问来源于stack exchange,提问作者Walshie1987

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 09:57:27