这是我的脚本
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/