欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

老妈叫下广场舞视频,想用python转换下视频格式flv转换为mp4

程序员文章站 2022-03-20 08:25:32
...

 首先下载B站的广场舞视频

cmd 利用you-get直接下载 

老妈叫下广场舞视频,想用python转换下视频格式flv转换为mp4



# -*- coding:utf-8 -*-
# time: 2020/9/22--20:04
#__author__ = 'challen'
 
import requests, time, urllib.request, re, os
from moviepy.editor import *
import os, sys, threading, json
 
import imageio
 
imageio.plugins.ffmpeg.download()
 
 
if __name__ == '__main__':
    start_time = time.time()
    video_path = '/C:/Users/NING MEI/Desktop'
    file_list = os.listdir(video_path)
 
    commands = []
    for f in file_list:
        fileDir = video_path + '/' + f
        if not os.path.isdir(fileDir):
            continue
        file_list_dir = os.listdir(fileDir)
        for file in file_list_dir:
            if str(file).__contains__('.DS_Store'):
                continue
            if not str(file).__contains__('.flv'):
                continue
            if str(file).__contains__('._'):
                continue
            file = file.replace('._','')
            filePath = fileDir + '/' + file
            file = file.replace(' ','-').replace('(','-').replace(')','-').replace('(','-').replace(")",'-')
            filePathNew = fileDir + '/' + file
            if filePath != filePathNew:
                try:
                    os.rename(filePath, filePathNew)
                except:
                    pass
            tsPath = fileDir + '/' + file.replace('.flv','.ts')
            mp4Path = fileDir + '/' + file.replace('.flv','.mp4')
            cmd_ts_command = 'ffmpeg -i ' + filePathNew + ' -c copy -bsf:v h264_mp4toannexb -f mpegts ' + tsPath
            cmd_mp4_command = 'ffmpeg -i "concat:' + tsPath + '" -c copy -bsf:a aac_adtstoasc -movflags +faststart ' + mp4Path
            if cmd_ts_command not in commands:
                commands.append(cmd_ts_command)
                print(cmd_ts_command)
                cmd_ts_command_out = os.popen(r"" + cmd_ts_command, "r")
                print(cmd_ts_command_out.read())
                time.sleep(10)
            if cmd_mp4_command not in commands:
                print(cmd_mp4_command)
                cmd_mp4_command_out = os.popen(r"" + cmd_mp4_command, "r")
                print(cmd_mp4_command_out.read())
                time.sleep(10)
            try:
                print('删除:'+tsPath)
                print('删除:'+filePathNew)
                os.remove(tsPath)
                os.remove(filePathNew)
            except:
                pass
    print('强制睡眠')
    time.sleep(9999)

 

相关标签: python os