Skip to content

Instantly share code, notes, and snippets.

@chrjxj
Forked from hiwonjoon/python-ffmpeg.py
Created January 26, 2020 11:42
Show Gist options
  • Save chrjxj/8a3449bc93f8fe662dfeb1ec6bb5867c to your computer and use it in GitHub Desktop.
Save chrjxj/8a3449bc93f8fe662dfeb1ec6bb5867c to your computer and use it in GitHub Desktop.

Revisions

  1. @hiwonjoon hiwonjoon revised this gist Mar 18, 2018. 1 changed file with 25 additions and 1 deletion.
    26 changes: 25 additions & 1 deletion python-ffmpeg.py
    Original file line number Diff line number Diff line change
    @@ -51,4 +51,28 @@ def read_frame(fileloc,frame,fps,num_frame,t_w,t_h) :
    out, err = ffmpeg.communicate()
    if(err) : print('error',err); return None;
    video = np.fromstring(out, dtype='uint8').reshape((num_frame,t_h,t_w,3)) #NHWC
    return video
    return video

    def write_frames(frames,filepath) :
    assert( frames.dtype == 'uint8' )

    n, h, w, c = frames.shape
    pix_fmt = 'rgb24' if c == 3 else 'gray'

    # adopted from http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/
    command = [ 'ffmpeg',
    '-loglevel', 'fatal',
    '-y', # (optional) overwrite output file if it exists
    '-f', 'rawvideo',
    '-vcodec','rawvideo',
    '-s', '%dx%d'%(w,h), # size of one frame
    '-pix_fmt', pix_fmt,
    '-r', '24', # frames per second
    '-i', '-', # The imput comes from a pipe
    '-an', # Tells FFMPEG not to expect any audio
    '-vcodec', 'libx264',
    filepath ]
    ffmpeg = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
    out, err = ffmpeg.communicate(frames.tostring() )

    if(err) : print('error',err)
  2. @hiwonjoon hiwonjoon revised this gist Aug 22, 2017. 1 changed file with 28 additions and 8 deletions.
    36 changes: 28 additions & 8 deletions python-ffmpeg.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    import subprocess
    import datetime
    import numpy as np

    THREAD_NUM=4

    def get_video_info(fileloc) :
    command = ['ffprobe',
    '-v', 'fatal',
    @@ -17,18 +20,35 @@ def get_video_info(fileloc) :
    'fps': float(out[2].split('/')[0])/float(out[2].split('/')[1]),
    'duration' : out[3] }

    def _read_frame(filename,time) :
    def get_video_frame_count(fileloc) : # This function is spearated since it is slow.
    command = ['ffprobe',
    '-v', 'fatal',
    '-count_frames',
    '-show_entries', 'stream=nb_read_frames',
    '-of', 'default=noprint_wrappers=1:nokey=1',
    fileloc, '-sexagesimal']
    ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
    out, err = ffmpeg.communicate()
    if(err) : print(err)
    out = out.split('\n')
    return {'file' : fileloc,
    'frames' : out[0]}

    def read_frame(fileloc,frame,fps,num_frame,t_w,t_h) :
    command = ['ffmpeg',
    '-loglevel', 'error',
    '-ss', str(time), #from this time
    '-i', filename,
    '-vframes', str(1), #get single frame
    '-loglevel', 'fatal',
    '-ss', str(datetime.timedelta(seconds=frame/fps)),
    '-i', fileloc,
    #'-vf', '"select=gte(n,%d)"'%(frame),
    '-threads', str(THREAD_NUM),
    '-vf', 'scale=%d:%d'%(t_w,t_h),
    '-vframes', str(num_frame),
    '-f', 'image2pipe',
    '-pix_fmt', 'rgb24',
    '-vcodec', 'rawvideo', '-']
    #print(command)
    ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
    out, err = ffmpeg.communicate()
    if(err) : print(err)
    image = np.fromstring(out, dtype='uint8').reshape((960,1280,3))
    return image
    if(err) : print('error',err); return None;
    video = np.fromstring(out, dtype='uint8').reshape((num_frame,t_h,t_w,3)) #NHWC
    return video
  3. @hiwonjoon hiwonjoon revised this gist Aug 22, 2017. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions python-ffmpeg.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,21 @@
    import subprocess
    import numpy as np

    def _get_duration(filename) :
    def get_video_info(fileloc) :
    command = ['ffprobe',
    '-v', 'error',
    '-show_entries', 'format=duration',
    '-v', 'fatal',
    '-show_entries', 'stream=width,height,r_frame_rate,duration',
    '-of', 'default=noprint_wrappers=1:nokey=1',
    filename, '-sexagesimal']
    fileloc, '-sexagesimal']
    ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
    out, err = ffmpeg.communicate()
    if(err) : print(err)
    return out
    out = out.split('\n')
    return {'file' : fileloc,
    'width': int(out[0]),
    'height' : int(out[1]),
    'fps': float(out[2].split('/')[0])/float(out[2].split('/')[1]),
    'duration' : out[3] }

    def _read_frame(filename,time) :
    command = ['ffmpeg',
  4. @hiwonjoon hiwonjoon revised this gist May 17, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions python-ffmpeg.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    import subprocess
    import numpy as np

    def _get_duration(filename) :
    command = ['ffprobe',
    '-v', 'error',
  5. @hiwonjoon hiwonjoon renamed this gist May 17, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @hiwonjoon hiwonjoon created this gist May 17, 2017.
    26 changes: 26 additions & 0 deletions python-ffmpeg
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    def _get_duration(filename) :
    command = ['ffprobe',
    '-v', 'error',
    '-show_entries', 'format=duration',
    '-of', 'default=noprint_wrappers=1:nokey=1',
    filename, '-sexagesimal']
    ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
    out, err = ffmpeg.communicate()
    if(err) : print(err)
    return out

    def _read_frame(filename,time) :
    command = ['ffmpeg',
    '-loglevel', 'error',
    '-ss', str(time), #from this time
    '-i', filename,
    '-vframes', str(1), #get single frame
    '-f', 'image2pipe',
    '-pix_fmt', 'rgb24',
    '-vcodec', 'rawvideo', '-']
    #print(command)
    ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
    out, err = ffmpeg.communicate()
    if(err) : print(err)
    image = np.fromstring(out, dtype='uint8').reshape((960,1280,3))
    return image