shaosks

  用ffmpeg命令从视频文件中提取音频

  命令:ffmpeg -i D:\AI\bili_data\test.mp4 -vn -y -acodec copy D:\AI\bili_data\output.aac

  

 

# 从视频中提取音频
def video_to_audio(video_file,audio_file):
    # ffmpeg命令工具 在D:/FFmpeg/bin目录下
    try:
        # ffmpeg = r\'D:/FFmpeg/bin/ffmpeg -i %s -vn -y -acodec copy %s\' %(video_file,audio_file)
        # ffmpeg = r\'D:/FFmpeg/bin/ffmpeg -i D:\AI\bili_data\test.mp4 -vn -y -acodec copy D:\AI\bili_data\output.aac\'
        # 8000采样率
        ffmpeg = r\'D:/FFmpeg/bin/ffmpeg.exe -i %s -f wav -vn -ar 8000 -ac 1 -y %s\' % (video_file, audio_file)
        # print(ffmpeg)
        os.system(ffmpeg)
        # p = subprocess.Popen(ffmpeg, shell=False)
        # p.wait(5)
        print(\'提取完成\')
    except Exception as ex:
        print(\'提取音频发生异常\', ex)

# 从视频中截取图片,每秒钟一个图片
def cut_image_from_video(video_file,imagedir):
    try:
        # strls = os.path.split(video_file)
        # filename = strls[1].replace(\'.mp4\',\'\')
        #
        # imagedir = os.path.join(strls[0],filename)
        if not os.path.exists(imagedir):
            os.mkdir(imagedir)
        filename = os.path.join(imagedir, \'test_frame_%03d.jpg\')
        ffmpeg = r\'D:/FFmpeg/bin/ffmpeg -i %s -r 1 -q:v 2 -f image2 -y %s\' % (video_file, filename)
        # print(ffmpeg)
        os.system(ffmpeg)
        # p = subprocess.Popen(ffmpeg, shell=False)
        # p.wait(5)
        print(\'提取完成\')
    except Exception as ex:
        print(\'提取音频发生异常\', ex)

# 截取视频片段
def cutVideo(video_file,save_name):
    # file_time = ffmpeg.probe(file_name)[\'format\'][\'duration\']    # 视频总时长 秒
    start_time=\'00:02:20\' # 开始时间
    length =\'00:02:00\' # 截取多长时间 2分钟
    #length = \'120\' #秒
    ffmpeg = r\'D:/FFmpeg/bin/ffmpeg.exe -y -i  %s -ss %s -t %s -acodec copy -vcodec copy -async 1 %s\' % (video_file, start_time,length,save_name)
    # subprocess.call(\'D:/FFmpeg/bin/ffmpeg.exe -y -i \' + video_file + \' -ss \' + start_time + \' -t \' + length + \' -acodec copy -vcodec copy -async 1 \' + save_name)
    r = subprocess.call(ffmpeg)
    print(r)

 

 

  

 

分类:

技术点:

相关文章: