1

I am trying to get the duration of a media file I have by running these lines from a python script:

test_file = 'path_to_file.mp4'
ffmpeg_get_mediafile_length = ["ffmpeg", "-i", test_file, '2>&1 | grep "Duration"']
output = subprocess.Popen(ffmpeg_get_mediafile_length,
                        stdout = subprocess.PIPE
                        ).stdout.read()
print output # this gives None
matches = re_length.search(output)
print matches

Here is the ffmpeg logs I get alongside with the error I get:

ffmpeg version 3.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.37)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/3.4 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'path_to_media_file':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.83.100
  Duration: 00:22:25.89, start: 0.000000, bitrate: 6059 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 6057 kb/s, 15 fps, 30 tbr, 90k tbn, 180k tbc (default)
    Metadata:
      handler_name    : VideoHandler
[NULL @ 0x7fedac802200] Unable to find a suitable output format for '2>&1 | grep "Duration"'
2>&1 | grep "Duration": Invalid argument
A_Matar
  • 2,210
  • 3
  • 31
  • 53
  • 2
    You can't pass a shell redirection in a command with no shell. – Charles Duffy Nov 06 '17 at 13:45
  • That's not a valid ffmpeg command. Did you mean ffprobe? – Aran-Fey Nov 06 '17 at 13:48
  • 1
    If you wonder why this is a duplicate, you should know that pipes `|` and redirects `>` are both shell features. Subprocess does not invoke a shell by default. https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – Håken Lid Nov 06 '17 at 14:01

2 Answers2

3

You could try ffprobe, which is capable to output JSON and you dont need to grep and regexp the result - but the duration is returned in seconds:

import shlex
import subprocess
import json    

filePath = '/home/f3k/Downloads/tr5.mp4'
command = shlex.split('/usr/bin/ffprobe -v quiet -print_format json -show_format -show_streams')
command.append(filePath)
proc = subprocess.Popen(command, stdout = subprocess.PIPE)
stdout, _ = proc.communicate()

output = json.loads(stdout)
print  (output['format']['duration'])

returns:

1483.081723
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
2

2>&1 | grep Duration is a shell redirection. You can only use that with a shell.

The shortest thing, to do, then, is to generate a shell script and set shell=True:

ffmpeg_get_mediafile_length = 'ffmpeg -i %s 2>&1 | grep "Duration"' % (pipes.quote(test_file))

Alternately, requiring no changes to other lines of code:

ffmpeg_get_mediafile_length = [
  'sh', '-c', 'ffmpeg -i "$1" 2>&1 | grep Duration',
  '_', test_file
]
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441