-
Notifications
You must be signed in to change notification settings - Fork 7
/
rockbox_mp4_convert.py
138 lines (104 loc) · 4.9 KB
/
rockbox_mp4_convert.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Requirements: apt-get install python-kaa-metadata ffmpeg python-dbus
# To use, copy it as a Python script into ~/.config/gpodder/hooks/rockbox_mp4_convert.py
# See the module "gpodder.hooks" for a description of when each hook
# gets called and what the parameters of each hook are.
#Based on Rename files after download based on the episode title
#And patch in Bug https://bugs.gpodder.org/show_bug.cgi?id=1263
# Copyright (c) 2011-04-06 Guy Sheffer <guysoft at gmail.com>
# Copyright (c) 2011-04-04 Thomas Perl <thp.io>
# Licensed under the same terms as gPodder itself
DEFAULT_DEVICE_WIDTH = 224.0 #make sure to include the .0, this is a float
DEFAULT_DEVICE_HEIGHT = 176.0
ROCKBOX_EXTENTION = "mpg"
EXTENTIONS_TO_CONVERT = ['.mp4',"." + ROCKBOX_EXTENTION]
FFMPEG_OPTIONS = '-vcodec mpeg2video -b 500k -ab 192k -ac 2 -ar 44100 -acodec libmp3lame'
from gpodder import util
import os
import os.path
import kaa.metadata
#import subprocess
import time
import dbus #For onscreen messages
#create a session
bus = dbus.SessionBus()
#Get the required notification service
notify_service = bus.get_object('org.freedesktop.Notifications', \
'/org/freedesktop/Notifications')
#interface for a message
notify_interface = dbus.Interface(notify_service, \
'org.freedesktop.Notifications')
def message(title,message):
'''
Send a notify message via Dbus
'''
notify_interface.Notify("test-notify", 0, '', title, \
message, [], {}, -1)
def convertMP4(from_file, to_file):
'''
Convert MP4 file to rockbox mpg file
'''
time.sleep(4)
#input_command = self.decoder_command % input_filename
#output_command = self.encoder_command % output_filename
if not os.path.isfile(to_file):
print "Converting:" + from_file
info = kaa.metadata.parse(from_file)
deviceWidth = DEFAULT_DEVICE_WIDTH
deviceHeight = DEFAULT_DEVICE_HEIGHT
width = info.video[0].width
height = info.video[0].height
try:
if height != None:
destWidth = DEFAULT_DEVICE_WIDTH
destHeight = DEFAULT_DEVICE_HEIGHT
widthRatio = destWidth/width
heightRatio = deviceHeight/height
destWidth = deviceWidth
destHeight = widthRatio*height
if destHeight > deviceHeight:
destHeight = deviceHeight
destWidth = heightRatio*width
message('Running conversion script',"Converting "+ from_file)
convert_command = 'ffmpeg -y -i "' + from_file +'" -s ' + str(int(destWidth))+ 'x' + str(int(destHeight)) + ' ' + FFMPEG_OPTIONS + ' "' + to_file + '"'
#process = subprocess.Popen(convert_command, stdout=subprocess.PIPE, shell=True)
os.system(convert_command)
else:
raise
except:
message('Conversion error',"Could not locate file for Conversion: "+ from_file)
return
print "RockBox mp4 converter hook loaded"
class gPodderHooks(object):
def on_episode_downloaded(self, episode):
try:
current_filename = episode.local_filename(False)
dirname = os.path.dirname(current_filename)
filename = os.path.basename(current_filename)
basename, ext = os.path.splitext(filename)
#print ext + " "+ str(EXTENTIONS_TO_CONVERT)
except:
print "Exception!"
if ext in EXTENTIONS_TO_CONVERT:
print "Converting"
#new_filename = util.sanitize_encoding(episode.title) + ext
new_filename = (current_filename[:- len(ext)]) + "." + ROCKBOX_EXTENTION
new_filebasename = basename + "." + ROCKBOX_EXTENTION
if filename.endswith(ROCKBOX_EXTENTION):
tmpFilename= current_filename + ".tmp"
os.rename(current_filename , tmpFilename)
current_filename = tmpFilename
dirname = os.path.dirname(current_filename)
filename = os.path.basename(current_filename)
basename, ext = os.path.splitext(filename)
print 'Renaming:', filename, '->', new_filename
destination_filename = os.path.join(dirname, new_filename)
convertMP4(current_filename, destination_filename)
#os.rename(current_filename, destination_filename)
episode.filename = os.path.basename(destination_filename)
episode.save()
os.remove(current_filename)
print "done converting!"
else:
print "Not converting"