1

Here is my scripts

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])

Here is my CSV File

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

So I have a script that reads a CSV file and ffmpeg records the video for 10 seconds. Then spits the output of the FFMPEG to a file. I need each camera to have its own file. Really just to log FFMPEG output for each camera. But my issue is that the FFMPEG output for multiple cameras get written to 1 file.

Here is what I want to see in /home/scripts/ffmpeglog/

stream1 stream3 stream4

Here's what I'm actually what I see in /home/scripts/ffmpeglog/

name stream1
D0n
  • 25
  • 4

1 Answers1

0

You have an extra for loop.

The second loop: for row in csv_reader: is not required, because you need to loop the CSV rows only once.

The following code should work:

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])

Other issue:
It looks like you are not handling the first (header) row of the CSV File.
The first row Name,RTSP_URL is a header that you need to skip.
You can skip the header as described in the following post: Skip the headers when editing a csv file using Python.

csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader, None)  # skip the headers

Note:
I recommend adding -y argument so FFmpeg will not ask a question we cannot see:

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)
Rotem
  • 30,366
  • 4
  • 32
  • 65