ffplayout/ffplayout.py

157 lines
5.6 KiB
Python
Raw Normal View History

2018-08-27 10:57:14 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2018-01-07 13:58:45 +01:00
# This file is part of ffplayout.
#
# ffplayout is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ffplayout is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ffplayout. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
import os
2020-02-03 17:33:08 +01:00
from subprocess import PIPE, Popen
2018-01-07 13:58:45 +01:00
from threading import Thread
2020-02-03 17:33:08 +01:00
from ffplayout.folder import GetSourceFromFolder, MediaStore, MediaWatcher
from ffplayout.playlist import GetSourceFromPlaylist
2020-02-03 20:49:55 +01:00
from ffplayout.utils import (_ff, _log, _playlist, _playout, _pre_comp, _text,
2020-02-03 17:33:08 +01:00
ffmpeg_stderr_reader, get_date, messenger,
2020-04-24 19:33:04 +02:00
pre_audio_codec, stdin_args, terminate_processes,
validate_ffmpeg_libs)
2018-01-07 13:58:45 +01:00
try:
2019-11-01 15:54:24 +01:00
if os.name != 'posix':
import colorama
colorama.init()
except ImportError:
2020-02-03 20:40:13 +01:00
print('colorama import failed, no colored console output on windows...')
2020-02-03 20:49:55 +01:00
_WINDOWS = os.name == 'nt'
2020-05-09 21:27:08 +02:00
COPY_BUFSIZE = 1024 * 1024 if _WINDOWS else 65424
2020-02-03 20:49:55 +01:00
# ------------------------------------------------------------------------------
# main functions
# ------------------------------------------------------------------------------
2018-01-07 13:58:45 +01:00
def main():
2019-09-09 16:13:18 +02:00
"""
pipe ffmpeg pre-process to final ffmpeg post-process,
or play with ffplay
"""
2019-03-10 21:24:03 +01:00
year = get_date(False).split('-')[0]
overlay = []
2019-09-08 21:24:02 +02:00
ff_pre_settings = [
'-pix_fmt', 'yuv420p', '-r', str(_pre_comp.fps),
'-c:v', 'mpeg2video', '-intra',
'-b:v', '{}k'.format(_pre_comp.v_bitrate),
'-minrate', '{}k'.format(_pre_comp.v_bitrate),
'-maxrate', '{}k'.format(_pre_comp.v_bitrate),
'-bufsize', '{}k'.format(_pre_comp.v_bufsize)
] + pre_audio_codec() + ['-f', 'mpegts', '-']
2019-09-08 21:24:02 +02:00
2019-12-14 19:37:26 +01:00
if _text.add_text:
messenger.info('Using drawtext node, listening on address: {}'.format(
_text.address
))
2019-09-08 21:24:02 +02:00
overlay = [
2020-05-08 20:11:11 +02:00
'-vf',
"null,zmq=b=tcp\\\\://'{}',drawtext=text='':fontfile='{}'".format(
2019-12-14 19:37:26 +01:00
_text.address.replace(':', '\\:'), _text.fontfile)
2019-09-08 21:24:02 +02:00
]
2019-06-07 16:42:11 +02:00
2018-01-07 13:58:45 +01:00
try:
2019-10-15 14:46:30 +02:00
if _playout.preview or stdin_args.desktop:
# preview playout to player
_ff.encoder = Popen([
2019-06-07 16:42:11 +02:00
'ffplay', '-hide_banner', '-nostats', '-i', 'pipe:0'
] + overlay, stderr=PIPE, stdin=PIPE, stdout=None)
else:
_ff.encoder = Popen([
'ffmpeg', '-v', _log.ff_level.lower(), '-hide_banner',
2020-05-10 21:06:52 +02:00
'-nostats', '-re', '-thread_queue_size', '256', '-i', 'pipe:0'
] + overlay + [
'-metadata', 'service_name=' + _playout.name,
'-metadata', 'service_provider=' + _playout.provider,
2019-05-28 21:22:10 +02:00
'-metadata', 'year={}'.format(year)
2020-02-03 12:07:21 +01:00
] + _playout.post_comp_param + [_playout.out_addr],
stdin=PIPE, stderr=PIPE)
enc_err_thread = Thread(target=ffmpeg_stderr_reader,
2020-02-03 20:49:55 +01:00
args=(_ff.encoder.stderr, False))
enc_err_thread.daemon = True
enc_err_thread.start()
2018-01-07 13:58:45 +01:00
2019-08-09 17:55:21 +02:00
if _playlist.mode and not stdin_args.folder:
watcher = None
2019-11-09 22:33:00 +01:00
get_source = GetSourceFromPlaylist()
2019-06-06 18:16:33 +02:00
else:
messenger.info('Start folder mode')
2019-09-03 21:14:40 +02:00
media = MediaStore()
2019-09-08 21:24:02 +02:00
watcher = MediaWatcher(media)
2019-11-09 22:33:00 +01:00
get_source = GetSourceFromFolder(media)
2019-05-28 21:48:53 +02:00
2019-06-06 22:02:20 +02:00
try:
for src_cmd in get_source.next():
2019-10-29 15:28:18 +01:00
messenger.debug('src_cmd: "{}"'.format(src_cmd))
2019-06-06 22:02:20 +02:00
if src_cmd[0] == '-i':
current_file = src_cmd[1]
else:
current_file = src_cmd[3]
messenger.info('Play: "{}"'.format(current_file))
2019-06-06 22:02:20 +02:00
with Popen([
'ffmpeg', '-v', _log.ff_level.lower(), '-hide_banner',
'-nostats'] + src_cmd + ff_pre_settings,
2019-11-07 21:37:16 +01:00
stdout=PIPE, stderr=PIPE) as _ff.decoder:
dec_err_thread = Thread(target=ffmpeg_stderr_reader,
2020-02-03 20:49:55 +01:00
args=(_ff.decoder.stderr, True))
dec_err_thread.daemon = True
dec_err_thread.start()
2019-10-15 17:32:08 +02:00
while True:
buf = _ff.decoder.stdout.read(COPY_BUFSIZE)
if not buf:
2019-10-15 17:32:08 +02:00
break
_ff.encoder.stdin.write(buf)
except BrokenPipeError:
2019-10-29 15:28:18 +01:00
messenger.error('Broken Pipe!')
terminate_processes(watcher)
except SystemExit:
messenger.info('Got close command')
terminate_processes(watcher)
except KeyboardInterrupt:
messenger.warning('Program terminated')
terminate_processes(watcher)
2019-06-24 15:01:49 +02:00
# close encoder when nothing is to do anymore
if _ff.encoder.poll() is None:
_ff.encoder.terminate()
2019-06-24 15:01:49 +02:00
2018-01-07 13:58:45 +01:00
finally:
2019-12-17 09:52:00 +01:00
if _ff.encoder.poll() is None:
_ff.encoder.terminate()
_ff.encoder.wait()
2018-01-07 13:58:45 +01:00
if __name__ == '__main__':
2020-04-24 19:33:04 +02:00
# check if ffmpeg contains all codecs and filters
validate_ffmpeg_libs()
2018-01-07 13:58:45 +01:00
main()