阿八博客
  • 100000+

    文章

  • 23

    评论

  • 20

    友链

  • 最近新加了很多技术文章,大家多来逛逛吧~~~~
  • 喜欢这个网站的朋友可以加一下QQ群,我们一起交流技术。

ffmpeg-python 任意提取视频帧

欢迎来到阿八个人博客网站。本 阿八个人博客 网站提供最新的站长新闻,各种互联网资讯。 喜欢本站的朋友可以收藏本站,或者加QQ:我们大家一起来交流技术! URL链接:https://www.abboke.com/jsh/2019/1010/116542.html

基于时间提取任意一帧

import ffmpegimport numpyimport cv2import sysimport randomdef read_frame_by_time(in_file, time):    """    指定时间节点读取任意帧    """    out, err = (        ffmpeg.input(in_file, ss=time)              .output('pipe:', vframes=1, format='image2', vcodec='mjpeg')              .run(capture_stdout=True)    )    return outdef get_video_info(in_file):    """    获取视频基本信息    """    try:        probe = ffmpeg.probe(in_file)        video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)        if video_stream is None:            print('No video stream found', file=sys.stderr)            sys.exit(1)        return video_stream    except ffmpeg.Error as err:        print(str(err.stderr, encoding='utf8'))        sys.exit(1)if __name__ == '__main__':    file_path = '/Users/admin/Downloads/拜无忧.mp4'    video_info = get_video_info(file_path)    total_duration = video_info['duration']    print('总时间:' + total_duration + 's')    random_time = random.randint(1, int(float(total_duration)) - 1) + random.random()    print('随机时间:' + str(random_time) + 's')    out = read_frame_by_time(file_path, random_time)    image_array = numpy.asarray(bytearray(out), dtype="uint8")    image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)    cv2.imshow('frame', image)    cv2.waitKey()

▶ 相关资料

https://github.com/kkroening/ffmpeg-python/tree/master/examples

相关文章

暂住......别动,不想说点什么吗?
  • 全部评论(0
    还没有评论,快来抢沙发吧!