Python使用存储在变量中的将stdout和stderr发送到多个文件

标签 python python-3.x ffmpeg stdout with-statement

这是我的脚本

csv_location = '/home/scripts/streams.csv'
ffmpeg_location = '/usr/bin/ffmpeg'
ffmpeglogs = '/home/scripts/ffmpeglog/'

# Open the streams list csv file
with open(csv_location) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        ffmpeg_log = (ffmpeglogs + row[0]) # set the ffmpeg log to be named the stream name
        # Open log file for writing
        with open(ffmpeg_log, 'wb') as ffmpeg_output: 
            # Iterate through streams list
            for row in csv_reader:
                print(row)
                stream_output = (row[0] + ".mpeg") # stream output variable
                # Subprocess record 1 stream at a time & send the output t0 stdout & stdeer
                ffmpeg_instance = subprocess.Popen([ffmpeg_location, '-t', '10', '-i', row[1], stream_output], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                # sent output to ffmpeg log
                ffmpeg_output.write(ffmpeg_instance.communicate()[1])
这是我的 CSV 文件
Name,RTSP_URL
stream1,rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
stream3,rtsp://wowz.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
stream4,rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
所以我有一个脚本可以读取 CSV 文件,ffmpeg 会录制 10 秒的视频。然后将 FFMPEG 的输出吐出到一个文件中。我需要每台相机都有自己的文件。真的只是为每台摄像机记录 FFMPEG 输出。但我的问题是多个摄像机的 FFMPEG 输出被写入 1 个文件。
这是我想在/home/scripts/ffmpeglog/中看到的内容
stream1 stream3 stream4
这就是我在/home/scripts/ffmpeglog/中看到的内容
name stream1

最佳答案

你有一个额外的 for 循环。
第二个循环:for row in csv_reader:不是必需的,因为您只需要循环一次 CSV 行。
以下代码应该可以工作:

import subprocess
import csv

csv_location = '/home/scripts/streams.csv'
ffmpeg_location = '/usr/bin/ffmpeg'
ffmpeglogs = '/home/scripts/ffmpeglog/'

# Open the streams list csv file
with open(csv_location) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        ffmpeg_log = (ffmpeglogs + row[0]) # set the ffmpeg log to be named the stream name
        # Open log file for writing
        with open(ffmpeg_log, 'wb') as ffmpeg_output: 
            # Iterate through streams list
            #for row in csv_reader:
            print(row)
            stream_output = (row[0] + ".mpeg") # stream output variable
            # Subprocess record 1 stream at a time & send the output t0 stdout & stdeer
            ffmpeg_instance = subprocess.Popen([ffmpeg_location, '-t', '10', '-i', row[1], stream_output], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            # sent output to ffmpeg log
            ffmpeg_output.write(ffmpeg_instance.communicate()[1])

其他问题:
看起来您没有处理 CSV 文件的第一(标题)行。
第一行Name,RTSP_URL是您需要跳过的标题。
您可以按照以下帖子中的说明跳过标题:Skip the headers when editing a csv file using Python .
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)  # skip the headers

笔记:
我建议添加 -y论点,因此 FFmpeg 不会提出我们看不到的问题:

File 'stream1.mpeg' already exists. Overwrite? [y/N]

ffmpeg_instance = subprocess.Popen([ffmpeg_location, '-y', '-t', '10', '-i', row[1], stream_output], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

关于Python使用存储在变量中的将stdout和stderr发送到多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67134634/

相关文章:

python - 如何使第一个索引列为空?

FFmpeg (HLS) - 按大小而不是时间分割段(或限制段大小)

ffmpeg - 我可以使用 FFMPEG 从 RTSP 流中获取 q 值吗

python - 用于 POS 标记和 Lemmatizer 的多语言 NLTK

python - TypeError : 'function' object is not iterable' Python 3

python - 在树莓派 3 上使用 selenium 和 chromedriver 时出错 - Raspbian Jessie

python - 将 python.exe 重命名为 python3.exe 以便与 Windows 上的 python2 共存

php - 我们如何将pdf转换为相同分辨率的图像?

python - Matplotlib:如何设置极坐标图的 theta 范围?

python - Numpy:查找至少有一个零相邻的元素的最有效方法 ("edges")