-
Notifications
You must be signed in to change notification settings - Fork 0
/
fps.py
64 lines (47 loc) · 1.95 KB
/
fps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import csv, os, shutil, argparse, errno,subprocess
import glob
def argparser_prepare():
class PrettyFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
max_help_position = 35
parser = argparse.ArgumentParser(description='change video speed by change fps withouth recompress',
formatter_class=PrettyFormatter)
parser.add_argument( 'path', type=str,
help='path to file')
parser.add_argument( 'fps', type=int,
help='new fps')
return parser
def determine_codec(filename):
command = ['ffprobe', '-show_format', '-pretty', '-show_entries', 'stream=codec_name', '-loglevel', 'quiet', filename]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
ffmpeg_result = out.decode()
if 'codec_name=hevc' in ffmpeg_result:
return 'h265'
return 'h264'
parser = argparser_prepare()
args = parser.parse_args()
src = args.path
fps = args.fps
#dst=$(dirname $1)/$(basename $1| cut -d. -f1)-48fps.mp4
dst = os.path.splitext(src)[0]+'-'+str(fps)+'.mp4'
if not(os.path.isfile(src)):
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), src)
source_codec = determine_codec(src)
if source_codec == 'h265':
cmd = '''
ffmpeg -hide_banner -loglevel error -i {src} -y -map 0:v -c:v copy -bsf:v hevc_mp4toannexb raw.h265
ffmpeg -hide_banner -loglevel error -fflags +genpts -r {fps} -y -i raw.h265 -c:v copy {dst}
'''
cmd = cmd.format(src=src, dst=dst, fps=fps)
os.system(cmd)
os.unlink('raw.h265')
elif source_codec == 'h264':
cmd = '''
ffmpeg -hide_banner -loglevel error -i {src} -y -map 0:v -c:v copy -bsf:v h264_mp4toannexb raw.h264
ffmpeg -fflags +genpts -r {fps} -y -i raw.h264 -c:v copy {dst}
'''
cmd = cmd.format(src=src, dst=dst, fps=fps)
os.system(cmd)
os.unlink('raw.h264')