示例#1
0
def download_stream(link: str, out_path: str = "./dl/") -> bool:

    video = ffmpeg_streaming.input(link)
    hls = video.hls(Formats.h264())
    _360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
    _480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
    _720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))

    hls = video.hls(Formats.h264(), hls_time=3600)
    hls.flags('delete_segments')
    hls.representations(_720p)
    hls.output("/mnt/c/Users/anael/OneDrive/projets/streamFinder/dl/test.mp4")
    return True
示例#2
0
def video_to_dash():
    dash = video.dash(Formats.h264())
    dash.auto_generate_representations()
    dash.output(r'C:\Users\admin\PycharmProjects\test2\dash.mpd')

    _144p = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
    _240p = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
    _360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
    _480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
    _720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
    _1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))
    _2k = Representation(Size(2560, 1440), Bitrate(6144 * 1024, 320 * 1024))
    _4k = Representation(Size(3840, 2160), Bitrate(17408 * 1024, 320 * 1024))

    dash = video.dash(Formats.h264())
    dash.representations(_144p, _240p, _360p, _480p, _720p, _1080p, _2k, _4k)
    dash.output(r'C:\Users\admin\PycharmProjects\test2\dash.mpd')
    atexit.register(print, "Video conversion successfully!")
示例#3
0
def video_to_hls():
    _144p = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
    _240p = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
    _360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
    _480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
    _720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
    _1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))

    hls = video.hls(Formats.h264())
    hls.representations(_144p, _240p, _360p, _480p, _720p, _1080p)
    hls.output(root + out + '.m3u8')
    atexit.register(print, "Video conversion successfully!")
示例#4
0
def async_convert_video_and_extract_meta_data(id):
    """
    Converts the uploaded video into 480 mp4 files that can be streamed using the .m38u file.
    The duration of the files is then extracted from the file and resaved in the Video object
    """
    print('async convert video running')
    print(id)
    instance = Video.objects.get(id=id)
    video_path = instance.url.path

    if instance.url.storage.exists(instance.url.name):
        video = ffmpeg_streaming.input(video_path)
        hls = video.hls(Formats.h264())
        _480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
        hls.representations(_480p)
        hls.output()

        duration_in_seconds = int(float(FFProbe(instance.url.path).streams().video().get('duration', 60)))
        minutes = int(time.strftime('%M', time.gmtime(duration_in_seconds)))
        Video.objects.filter(pk=instance.pk).update(duration=minutes if minutes > 1 else 1)
    def test_hls(self):
        video = ffmpeg_streaming.input(self.src_video)
        hls_obj = video.hls(Formats.h264())
        self.assertIsInstance(hls_obj, HLS)
        self.assertEqual(hls_obj.media.input, self.src_video)

        hls_obj.representations(Representation(Size(256, 144),
                                               Bitrate(102400)))
        rep_1 = hls_obj.reps[0]
        self.assertIsInstance(rep_1, Representation)
        self.assertEqual(str(rep_1.size), '256x144')
        self.assertEqual(rep_1.bitrate.video, '100k')

        hls_obj.auto_generate_representations()
        reps = list(hls_obj.reps)
        self.assertEqual(len(reps), 3)

        for rep_ in reps:
            self.assertIsInstance(rep_, Representation)
        self.assertEqual(str(reps[0].size), '480x270')
        self.assertEqual(reps[0].bitrate.video, '176k')
        self.assertEqual(str(reps[1].size), '426x240')
        self.assertEqual(reps[1].bitrate.video, '88k')
        self.assertEqual(str(reps[2].size), '256x144')
        self.assertEqual(reps[2].bitrate.video, '71k')

        hls_obj.output(os.path.join(self.src_dir, 'hls', 'test.m3u8'))
        with open(os.path.join(self.src_dir,
                               'fixture_test.m3u8')) as test_m3u8:
            expected_m3u8 = test_m3u8.read()
        with open(os.path.join(self.src_dir, 'hls', 'test.m3u8')) as test_m3u8:
            actual_m3u8 = test_m3u8.read()
        self.assertEqual(actual_m3u8, expected_m3u8)
        with open(os.path.join(self.src_dir, 'hls',
                               'test_270p.m3u8')) as test_m3u8:
            actual_270_m3u8 = test_m3u8.readlines()
        self.assertEqual(actual_270_m3u8[0].replace('\n', ''), '#EXTM3U')
        self.assertEqual(actual_270_m3u8[1].replace('\n', ''),
                         '#EXT-X-VERSION:3')
        self.assertEqual(actual_270_m3u8[2].replace('\n', ''),
                         '#EXT-X-ALLOW-CACHE:YES')
class Resolutions:

    R_144P = "144p"
    R_240P = "240p"
    R_360P = "360p"
    R_480P = "480p"
    R_720P = "720p"
    R_1080P = "1080p"
    R_2K = "2k"
    R_4K = "4k"

    QUALITIES = {
        R_144P: Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024)),
        R_240P: Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024)),
        R_360P: Representation(Size(640, 360), Bitrate(276 * 1024,
                                                       128 * 1024)),
        R_480P: Representation(Size(854, 480), Bitrate(750 * 1024,
                                                       192 * 1024)),
        R_720P: Representation(Size(1280, 720),
                               Bitrate(2048 * 1024, 320 * 1024)),
        R_1080P: Representation(Size(1920, 1080),
                                Bitrate(4096 * 1024, 320 * 1024)),
        R_2K: Representation(Size(2560, 1440), Bitrate(6144 * 1024,
                                                       320 * 1024)),
        R_4K: Representation(Size(3840, 2160), Bitrate(17408 * 1024,
                                                       320 * 1024))
    }

    def get_reps(self, names: list[str]):
        reps = []
        for name in names:
            # skip empty strings
            if name and not name.isspace():
                try:
                    reps.append(self.QUALITIES[name])
                except KeyError:
                    raise KeyError(f"{name} is not a valid quality name.")
        return reps
示例#7
0
from django.shortcuts import render

from django.http import HttpResponse

import ffmpeg_streaming
from ffmpeg_streaming import Formats, Bitrate, Representation, Size

from django.conf import settings

stream_link = 'http://ss-b.telek.xyz/a0sn?token=1JD2NZ4445NA'
_240p = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
_360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
_1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))


def index(request):
    return render(request, 'stream/index.html',
                  {'input_video': f'{settings.STATIC_URL}video/450.mp4'})


def run(request):
    video = ffmpeg_streaming.input(stream_link)
    dash = video.dash(Formats.h264())
    dash.representations(_480p)
    dash.output('/app/static/stream/dash.mpd')


def stop(request):
    video = ffmpeg_streaming.input('')
import ffmpeg_streaming
from ffmpeg_streaming import Formats
video = ffmpeg_streaming.input('video.mp4')
from ffmpeg_streaming import Formats, Bitrate, Representation, Size

_360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))

hls = video.hls(Formats.h264())
hls.representations(_360p, _480p, _720p)

hls.output('./flask-hls-demo/video/hls.m3u8')
from ffmpeg_streaming import Formats, Bitrate, Representation, Size, input

video = input('rtsp://localhost:8554/live');

_480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
hls = video.hls(Formats.h264(), hls_list_size=5, hls_time=2)
hls.flags('delete_segments')
hls.representations(_480p)
hls.output('./output/hls1.m3u8')
示例#10
0
    def initial_protocol(self,
                         input_path: str,
                         output_id: str,
                         request_id: str,
                         encode_format: str,
                         video_codec: str = None,
                         audio_codec: str = None,
                         is_hls: bool = None,
                         fragmented: bool = None,
                         quality_names: list[str] = None,
                         custom_qualities: list[dict] = None):
        """build HLS or MPEG ffmpeg command
        using ffmpeg_streaming package
        """
        # checking file is exist and not empty
        try:
            if os.stat(input_path).st_size == 0:
                self.save_output_status(self.output_status.OUTPUT_FAILED,
                                        output_id, request_id)
                self.save_job_stop_reason(
                    self.stop_reason.INPUT_VIDEO_SIZE_CAN_NOT_BE_ZERO,
                    request_id)
                raise self.raise_ignore(
                    message=self.error_messages.INPUT_SIZE_CAN_NOT_BE_ZERO,
                    request_kwargs=self.request.kwargs)
        except FileNotFoundError:
            # TODO notify developer
            self.save_output_status(self.output_status.OUTPUT_FAILED,
                                    output_id, request_id)
            self.save_job_stop_reason(self.stop_reason.INTERNAL_ERROR,
                                      request_id)
            raise self.raise_ignore(
                message=self.error_messages.INPUT_FILE_IS_NOT_FOUND,
                request_kwargs=self.request.kwargs)

        video = ffmpeg_streaming.input(input_path)
        format_instance = VideoEncodingFormats().get_format_class(
            encode_format,
            video=video_codec,
            audio=audio_codec,
        )
        if is_hls:
            # HLS Protocol
            protocol = video.hls(format_instance)
            if fragmented:
                protocol.fragmented_mp4()
        else:
            # MPEG-Dash Protocol
            protocol = video.dash(format_instance)
        """create a list of Representation instances to
        add to the protocol instance
        """

        # generate default representations
        if not (custom_qualities or quality_names):
            try:
                protocol.auto_generate_representations(
                    ffprobe_bin=settings.FFPROBE_BIN_PATH)
                return
            except RuntimeError as e:
                # TODO capture error and notify developer
                raise self.retry(exc=e)
            except Exception as e:
                # FileNotFoundError:
                # [Errno 2] No such file or directory: 'ffprobe'
                # TODO capture error and notify developer
                raise self.retry(exc=e)

        reps = []

        # quality_names is like ["360p","480p","720p"]
        if quality_names:
            reps.extend(Resolutions().get_reps(quality_names))

        # custom_qualities is like :
        # [dict(size=[256, 144], bitrate=[97280, 65536])]
        for quality in custom_qualities:
            size = quality.get('size', None)
            bitrate = quality.get('bitrate', None)

            # when both of them has not valid value, just continue
            if not (size or bitrate):
                continue

            # when just one of them is exist,
            # force client to fill both of them
            if not size or not bitrate:
                self.save_output_status(self.output_status.OUTPUT_FAILED,
                                        output_id, request_id)
                self.save_job_stop_reason(
                    self.stop_reason.
                    REPRESENTATION_NEEDS_BOTH_SIZE_AND_BITRATE, request_id)
                raise self.raise_ignore(
                    message=self.error_messages.
                    REPRESENTATION_NEEDS_BOTH_SIZE_AND_BITRATE,
                    request_kwargs=self.request.kwargs)

            reps.append(Representation(Size(*size), Bitrate(*bitrate)))

        # generate representations
        protocol.representations(*reps)

        return protocol
import ffmpeg_streaming

video_path = "C:\\Users\\Admin\\Downloads\\Video\\people.mp4"
video = ffmpeg_streaming.input(video_path)

from ffmpeg_streaming import Formats, Bitrate, Representation, Size

_144p = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
_240p = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
_360p = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
_1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))
_2k = Representation(Size(2560, 1440), Bitrate(6144 * 1024, 320 * 1024))
_4k = Representation(Size(3840, 2160), Bitrate(17408 * 1024, 320 * 1024))

dash = video.dash(Formats.h264())
dash.representations(_144p, _240p, _360p, _480p, _720p, _1080p, _2k, _4k)
dash.output('dash.mpd')
示例#12
0
import ffmpeg_streaming
from ffmpeg_streaming import Formats, Bitrate, Representation, Size

_144p  = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
_240p  = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
_360p  = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
_480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
_720p  = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))


#video = ffmpeg_streaming.input('./bunny.mp4')
video = ffmpeg_streaming.input('./test_input.mp4')
dash = video.dash(ffmpeg_streaming.Formats.h264())
#dash.auto_generate_representations()
dash.representations(_144p, _240p, _360p, _480p, _720p)
dash.output('./dash.mpd')
import ffmpeg_streaming
from ffmpeg_streaming import Formats, Bitrate, Representation, Size


video = ffmpeg_streaming.input('file_example_MP4_480_1_5MG.mp4')
_144p  = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
# _240p  = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
# _360p  = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
# _480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
# _720p  = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
# _1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))
# _2k    = Representation(Size(2560, 1440), Bitrate(6144 * 1024, 320 * 1024))
# _4k    = Representation(Size(3840, 2160), Bitrate(17408 * 1024, 320 * 1024))

dash = video.dash(Formats.h264())
dash.representations(_144p)
dash.output('../../static/videos/dash/dash.mpd')
示例#14
0
文件: convert.py 项目: gh936/hls
    sys.stdout.write("\rTranscoding...(%s%%) [%s%s]" % (per, '#' * per, '-' * (100 - per)))
    sys.stdout.flush()


with open('movies.txt') as file:
    lines = [line.rstrip('\n') for line in file]

print(f'converting {lines}')

for filename in lines:
    try:
        video = ffmpeg_streaming.input(f'./{filename}')

        # uncomment the resolutions you want
        # _144p  = Representation(Size(256, 144), Bitrate(95 * 1024, 64 * 1024))
        # _240p  = Representation(Size(426, 240), Bitrate(150 * 1024, 94 * 1024))
        # _360p  = Representation(Size(640, 360), Bitrate(276 * 1024, 128 * 1024))
        # _480p  = Representation(Size(854, 480), Bitrate(750 * 1024, 192 * 1024))
        # _720p  = Representation(Size(1280, 720), Bitrate(2048 * 1024, 320 * 1024))
        _1080p = Representation(Size(1920, 1080), Bitrate(4096 * 1024, 320 * 1024))
        # _2k    = Representation(Size(2560, 1440), Bitrate(6144 * 1024, 320 * 1024))
        # _4k    = Representation(Size(3840, 2160), Bitrate(17408 * 1024, 320 * 1024))

        hls = video.hls(Formats.h264())
        hls.representations(_1080p)
        folder_name = filename.split('.')[0]
        hls.output(f'./{folder_name}/{filename}.m3u8', monitor=monitor)
    except Exception:
        print(f'could not convert {filename}')