tomaseo2022 commited on
Commit
58607b6
·
1 Parent(s): 7778353

Upload 2 files

Browse files
Files changed (2) hide show
  1. urls.py +6 -0
  2. utils.py +79 -37
urls.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Predefined URLs used to make google translate requests.
4
+ """
5
+ BASE = 'https://translate.google.com'
6
+ TRANSLATE = 'https://{host}/translate_a/single'
utils.py CHANGED
@@ -1,37 +1,79 @@
1
- from pydub import AudioSegment
2
- #from pydub.utils import mediainfo
3
- from pydub.utils import make_chunks
4
- import math
5
- #flac_audio = AudioSegment.from_file("sample.flac", "flac")
6
- #flac_audio.export("audio.wav", format="wav")
7
- def split_audio_wav(filename):
8
- myaudio = AudioSegment.from_file(filename , "wav")
9
- channel_count = myaudio.channels #Get channels
10
- sample_width = myaudio.sample_width #Get sample width
11
- duration_in_sec = len(myaudio) / 1000#Length of audio in sec
12
- sample_rate = myaudio.frame_rate
13
- print("sample_width=", sample_width)
14
- print("channel_count=", channel_count)
15
- print("duration_in_sec=", duration_in_sec)
16
- print("frame_rate=", sample_rate)
17
- bit_rate =16 #assumption , you can extract from mediainfo("test.wav") dynamically
18
- wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec) / 8
19
- print("wav_file_size = ",wav_file_size)
20
- file_split_size = 40000000 # 40mb OR 40, 000, 000 bytes
21
- total_chunks = wav_file_size // file_split_size
22
- #Get chunk size by following method #There are more than one ofcourse
23
- #for duration_in_sec (X) --> wav_file_size (Y)
24
- #So whats duration in sec (K) --> for file size of 40Mb
25
- # K = X * 40Mb / Y
26
- chunk_length_in_sec = math.ceil((duration_in_sec * 40000000 ) /wav_file_size) #in sec
27
- chunk_length_ms = chunk_length_in_sec * 1000
28
- chunks = make_chunks(myaudio, chunk_length_ms)
29
- number_chunks=len(chunks)
30
- chunks_list=[]
31
- #Export all of the individual chunks as wav files
32
- for i, chunk in enumerate(chunks):
33
- chunk_name = "chunk{0}.wav".format(i)
34
- print("exporting", chunk_name)
35
- chunk.export(chunk_name, format="wav")
36
- chunks_list.append(chunk_name)
37
- return chunks_list
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A conversion module for googletrans"""
2
+ import json
3
+ import re
4
+
5
+
6
+ def build_params(query, src, dest, token, override):
7
+ params = {
8
+ 'client': 'webapp',
9
+ 'sl': src,
10
+ 'tl': dest,
11
+ 'hl': dest,
12
+ 'dt': ['at', 'bd', 'ex', 'ld', 'md', 'qca', 'rw', 'rm', 'ss', 't'],
13
+ 'ie': 'UTF-8',
14
+ 'oe': 'UTF-8',
15
+ 'otf': 1,
16
+ 'ssel': 0,
17
+ 'tsel': 0,
18
+ 'tk': token,
19
+ 'q': query,
20
+ }
21
+
22
+ if override is not None:
23
+ for key, value in get_items(override):
24
+ params[key] = value
25
+
26
+ return params
27
+
28
+
29
+ def legacy_format_json(original):
30
+ # save state
31
+ states = []
32
+ text = original
33
+
34
+ # save position for double-quoted texts
35
+ for i, pos in enumerate(re.finditer('"', text)):
36
+ # pos.start() is a double-quote
37
+ p = pos.start() + 1
38
+ if i % 2 == 0:
39
+ nxt = text.find('"', p)
40
+ states.append((p, text[p:nxt]))
41
+
42
+ # replace all weired characters in text
43
+ while text.find(',,') > -1:
44
+ text = text.replace(',,', ',null,')
45
+ while text.find('[,') > -1:
46
+ text = text.replace('[,', '[null,')
47
+
48
+ # recover state
49
+ for i, pos in enumerate(re.finditer('"', text)):
50
+ p = pos.start() + 1
51
+ if i % 2 == 0:
52
+ j = int(i / 2)
53
+ nxt = text.find('"', p)
54
+ # replacing a portion of a string
55
+ # use slicing to extract those parts of the original string to be kept
56
+ text = text[:p] + states[j][1] + text[nxt:]
57
+
58
+ converted = json.loads(text)
59
+ return converted
60
+
61
+
62
+ def get_items(dict_object):
63
+ for key in dict_object:
64
+ yield key, dict_object[key]
65
+
66
+
67
+ def format_json(original):
68
+ try:
69
+ converted = json.loads(original)
70
+ except ValueError:
71
+ converted = legacy_format_json(original)
72
+
73
+ return converted
74
+
75
+
76
+ def rshift(val, n):
77
+ """python port for '>>>'(right shift with padding)
78
+ """
79
+ return (val % 0x100000000) >> n