Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mp4conv
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Klaus-Uwe Mitterer
mp4conv
Commits
812b5fc3
Commit
812b5fc3
authored
Aug 18, 2018
by
Klaus-Uwe Mitterer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
117 additions
and
0 deletions
+117
-0
.gitignore
.gitignore
+3
-0
mp4conv.py
mp4conv.py
+114
-0
No files found.
.gitignore
0 → 100644
View file @
812b5fc3
**/__pycache__
**/*.pyc
upload/
mp4conv.py
0 → 100755
View file @
812b5fc3
#!/usr/bin/env python3
import
subprocess
import
pyinotify
import
logging
import
io
import
time
import
os
import
shutil
import
multiprocessing
import
logging.handlers
indir
=
"upload/convert/"
outdir
=
"upload/"
faildir
=
"upload/fail/"
logf
=
"[
%(name)
s]
%(levelname)
s:
%(message)
s"
logging
.
basicConfig
(
format
=
logf
)
globq
=
multiprocessing
.
Queue
()
class
Worker
(
multiprocessing
.
Process
):
def
__init__
(
self
,
path
,
q
):
super
(
Worker
,
self
)
.
__init__
()
self
.
path
=
path
self
.
basename
=
path
.
rpartition
(
"/"
)[
-
1
]
.
rpartition
(
"."
)[
0
]
self
.
ext
=
path
.
rpartition
(
"."
)[
-
1
]
self
.
time
=
int
(
time
.
time
())
self
.
string
=
io
.
StringIO
()
self
.
logger
=
logging
.
getLogger
(
"mp4conv"
)
self
.
handler
=
logging
.
StreamHandler
(
self
.
string
)
self
.
queue
=
logging
.
handlers
.
QueueHandler
(
q
)
self
.
logger
.
addHandler
(
self
.
handler
)
self
.
logger
.
addHandler
(
self
.
queue
)
self
.
logger
.
setLevel
(
logging
.
DEBUG
)
def
subprocess_logger
(
self
,
output
):
for
line
in
io
.
TextIOWrapper
(
output
,
encoding
=
"utf-8"
):
self
.
logger
.
debug
(
"(ffmpeg -
%
s)
%
r"
%
(
self
.
basename
,
line
))
def
build_command
(
self
):
return
[
"ffmpeg"
,
"-i"
,
self
.
path
,
"-vcodec"
,
"h264"
,
"-acodec"
,
"aac"
,
"-strict"
,
"-2"
,
"
%
s/
%
s_
%
i.tmp.mp4"
%
(
outdir
,
self
.
basename
,
self
.
time
),
"-nostats"
,
"-hide_banner"
]
def
process
(
self
):
self
.
logger
.
info
(
"Discovered file
%
s."
%
self
.
path
)
time
.
sleep
(
5
)
if
not
os
.
path
.
exists
(
self
.
path
):
self
.
logger
.
info
(
"File
%
s disappeared."
%
self
.
path
)
return
self
.
string
self
.
logger
.
info
(
"Starting to process
%
s."
%
self
.
path
)
try
:
sub
=
subprocess
.
Popen
(
self
.
build_command
(),
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
STDOUT
)
with
sub
.
stdout
as
output
:
self
.
subprocess_logger
(
output
)
assert
not
sub
.
wait
()
os
.
rename
(
"
%
s/
%
s_
%
i.tmp.mp4"
%
(
outdir
,
self
.
basename
,
self
.
time
),
"
%
s/
%
s_
%
i.mp4"
%
(
outdir
,
self
.
basename
,
self
.
time
))
if
os
.
path
.
exists
(
self
.
path
):
os
.
unlink
(
self
.
path
)
except
Exception
as
e
:
self
.
logger
.
exception
(
"Something went wrong. See the log file."
)
with
open
(
"
%
s/
%
s_
%
i.log"
%
(
faildir
,
self
.
basename
,
self
.
time
),
"w+"
)
as
logfile
:
self
.
string
.
seek
(
0
)
shutil
.
copyfileobj
(
self
.
string
,
logfile
)
os
.
rename
(
self
.
path
,
"
%
s/
%
s_
%
i.
%
s"
%
(
faildir
,
self
.
basename
,
self
.
time
,
self
.
ext
))
finally
:
return
self
.
string
def
run
(
self
):
self
.
process
()
class
Handler
(
pyinotify
.
ProcessEvent
):
def
my_init
(
self
,
q
):
self
.
logq
=
q
def
process_IN_CLOSE_WRITE
(
self
,
event
):
Worker
(
event
.
pathname
,
self
.
logq
)
.
start
()
def
process_IN_MOVED_TO
(
self
,
event
):
self
.
process_IN_CLOSE_WRITE
(
event
)
def
runner
(
q
=
globq
):
watch
=
pyinotify
.
WatchManager
()
event
=
pyinotify
.
IN_CLOSE_WRITE
|
pyinotify
.
IN_MOVED_TO
#handler = logging.StreamHandler()
#listener = logging.handlers.QueueListener(q, handler)
#listener.start()
notifier
=
pyinotify
.
ThreadedNotifier
(
watch
,
Handler
(
q
=
q
))
watch
.
add_watch
(
indir
,
event
,
rec
=
True
)
notifier
.
start
()
if
__name__
==
"__main__"
:
runner
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment