如何解决Python使用存储在变量中将stdout和stderr发送到多个文件
这是我的脚本
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,')
next(csv_reader,None) # skip the headers
注意:
我建议添加 -y
参数,这样 FFmpeg 就不会问我们看不到的问题:
文件“stream1.mpeg”已经存在。覆盖? [是/否]
ffmpeg_instance = subprocess.Popen([ffmpeg_location,'-y',stderr=subprocess.PIPE)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。