Home > front end >  Extract number of frames without using shell-True
Extract number of frames without using shell-True

Time:03-28

I have the following code that extract number of frames from video:

filename = "test.mp4"
video_path = os.path.join(os.getcwd(), filename)
with open(video_path, "rb") as file:
    cmd = f'ffmpeg -i {video_path} -r 10 -q:v 3 -s 1x1 -f image2 -update 1 - >/dev/null'
    proc = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
    (stdout, stderr) = proc.communicate()

    # i.e: 'frame= 2062 fps=966 q=3.0 Lsize=N/A time=00:10:34.46 bitrate=N/A dup=1049 drop=3563 speed= 297x'
    decoded = stderr.decode('utf-8')
    frame_count_line = decoded.splitlines()[-2]
    m = FRAME_COUNT_REGEX.search(frame_count_line)
    if m:
        print(int(m.groups()[0]))

but because of security reasons I want to remove shell=True parameter. Unfortunately after that the code above throws:

FileNotFoundError: [Errno 2] No such file or directory

Please help to fix it

CodePudding user response:

Put your command into a list e.g.

import subprocess
media = 'some media.mp4'
try:
    comm = subprocess.Popen(['ffmpeg', '-hide_banner', '-i', media, '-f', 'null', '-'],
                              stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
except Exception as e:
    print("Error: ", str(e))

try:
    with comm.stdout:
        for i in iter(comm.stdout.readline, b''):
            if i != '':
                print(i.strip())
            else:
                break
except Exception as e:
    print("Error: ", str(e))

You could also use ffprobe rather than ffmpeg because ffprobe allows you to select things like -count_frames and -show_frames.
Counting frames will only output the final figure, so if you need to show what's going on, you might be better with -show_frames. e.g.

ffprobe -show_frames -count_frames -v error -select_streams v:0 -show_entries stream=nb_read_frames -show_entries frame=coded_picture_number -of default=nokey=1:noprint_wrappers=1 -i media

or

ffprobe -count_frames -v error -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -i media

or

ffprobe -show_frames -v error -select_streams v:0 -show_entries frame=coded_picture_number -of default=nokey=1:noprint_wrappers=1 -i media
  • Related