-
Notifications
You must be signed in to change notification settings - Fork 8
/
ffmpeg.py
97 lines (75 loc) · 2.5 KB
/
ffmpeg.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
from subprocess import call, check_output
import re
import os
import json
class Ffmpeg():
def __init__(self):
self.ffmpeg = '/bin/ffmpeg.exe'
self.ffprobe = '/bin/ffprobe.exe'
self.wdir = os.getcwd()
self.sav_dir = os.environ['USERPROFILE'].replace("\\", "/") + \
"/.musicapp" + "/_temp"
if not os.path.exists(self.sav_dir):
os.mkdir(self.sav_dir)
os.chdir("C:/Users/GODWIN/Documents/GitHub/python-music-player/" +'bin/')
#i = 'C:\\Users\\GODWIN\\Music\\Joyce-Blessing-–-I-Swerve-You-Prod.-By-Linkin-www.Ghanasongs.com_.mp3'
#self.probe(i)
#self.convert(i, 'mp3')
def probe(self, i):
info = {}
out_print = check_output([
'ffprobe', '-i',
i,
'-show_format',
'-print_format',
'json',
'-v',
'error'
], shell=True)
raw = json.loads(out_print)
data = raw['format']
info['file'] = data['filename']
info['format_name'] = data['format_name']
info['size'] = data['size']
# calc the seconds
dura = float(data['duration'])
mins = int(dura / 60)
secs = int(dura - (mins * 60))
if secs < 10:
secs = '0' + str(secs)
else:
secs = str(secs)
if (mins) > 59.9:
hrs = int(dura / 3600)
calc_time = str(hrs) + ":" + str(mins) + ":" + secs
else:
calc_time = str(mins) + ":" + secs
print(calc_time)
# use the calc time as duration
info['duration'] = calc_time
if 'tags' in data and 'title' in data['tags']:
info.update(data['tags'])
else:
splits = os.path.split(i)
name = splits[1]
fake = {}
fake['tags'] = {'title': name, 'artist': 'Unknown',
'album': 'Unknown Album'}
info.update(fake['tags'])
return info
def convert(self, input_file, format_name):
i = input_file.replace("\\", "/")
splits = os.path.split(i)
file = splits[1]
file = file.replace('.' + format_name, '')
file = file + '.wav'
o = self.sav_dir + "/" + file
if not os.path.exists(o):
check_output([
'ffmpeg', '-i',
i,
o
], shell=True)
return 0
#love = Ffmpeg()