示例#1
0
    def do_it():

        if HC.PLATFORM_WINDOWS:

            cmd = ['explorer', '/select,', path]

        elif HC.PLATFORM_MACOS:

            cmd = ['open', '-R', path]

        elif HC.PLATFORM_LINUX:

            raise NotImplementedError('Linux cannot open file locations!')

        elif HC.PLATFORM_HAIKU:

            raise NotImplementedError('Haiku cannot open file locations!')

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(hide_terminal=False)

        HydrusData.CheckProgramIsNotShuttingDown()

        process = subprocess.Popen(cmd, **sbp_kwargs)

        HydrusThreading.SubprocessCommunicate(process)
示例#2
0
def GetUPnPMappings():

    cmd = [upnpc_path, '-l']

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if stderr is not None and len(stderr) > 0:

        raise Exception('Problem while trying to fetch UPnP mappings:' +
                        os.linesep * 2 + stderr)

    else:

        return GetUPnPMappingsParseResponse(stdout)
示例#3
0
    def do_it():

        if HC.PLATFORM_WINDOWS:

            os.startfile(path)

        else:

            if HC.PLATFORM_MACOS:

                cmd = ['open', path]

            elif HC.PLATFORM_LINUX:

                cmd = ['xdg-open', path]

            elif HC.PLATFORM_HAIKU:

                cmd = ['open', path]

            # setsid call un-childs this new process

            sbp_kwargs = HydrusData.GetSubprocessKWArgs()

            preexec_fn = getattr(os, 'setsid', None)

            HydrusData.CheckProgramIsNotShuttingDown()

            process = subprocess.Popen(cmd,
                                       preexec_fn=preexec_fn,
                                       **sbp_kwargs)

            HydrusThreading.SubprocessCommunicate(process)
示例#4
0
def RemoveUPnPMapping(external_port, protocol):

    cmd = [UPNPC_PATH, '-d', str(external_port), protocol]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    try:

        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             **sbp_kwargs)

    except FileNotFoundError:

        RaiseMissingUPnPcError('remove UPnP port forward')

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if stderr is not None and len(stderr) > 0:

        raise Exception('Problem while trying to remove UPnP mapping:' +
                        os.linesep * 2 + stderr)
示例#5
0
def GetUPnPMappings():

    cmd = [UPNPC_PATH, '-l']

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    try:

        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             **sbp_kwargs)

    except FileNotFoundError:

        RaiseMissingUPnPcError('get current UPnP port forward mappings')

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if stderr is not None and len(stderr) > 0:

        raise Exception(
            'Problem while trying to fetch UPnP mappings (if it says No IGD UPnP Device, you are either on a VPN or your router does not seem to support UPnP):'
            + os.linesep * 2 + stderr)

    else:

        return GetUPnPMappingsParseResponse(stdout)
示例#6
0
    def do_it():

        if HC.PLATFORM_WINDOWS:

            os.startfile(path)

        else:

            if HC.PLATFORM_MACOS:

                cmd = ['open', path]

            elif HC.PLATFORM_LINUX:

                cmd = ['xdg-open', path]

            # setsid call un-childs this new process

            sbp_kwargs = HydrusData.GetSubprocessKWArgs()

            preexec_fn = getattr(os, 'setsid', None)

            process = subprocess.Popen(cmd,
                                       preexec_fn=preexec_fn,
                                       **sbp_kwargs)

            process.communicate()
def RenderPageToFile( path, temp_path, page_index ):
    
    cmd = [ SWFRENDER_PATH, path,  '-o', temp_path, '-p', str( page_index ) ]
    
    timeout = HydrusData.GetNow() + 60
    
    sbp_kwargs = HydrusData.GetSubprocessKWArgs()
    
    HydrusData.CheckProgramIsNotShuttingDown()
    
    p = subprocess.Popen( cmd, **sbp_kwargs )
    
    while p.poll() is None:
        
        if HydrusData.TimeHasPassed( timeout ):
            
            p.terminate()
            
            raise Exception( 'Could not render the swf page within 60 seconds!' )
            
        
        time.sleep( 0.5 )
        
    
    HydrusThreading.SubprocessCommunicate( p )
示例#8
0
def AddUPnPMapping(internal_client,
                   internal_port,
                   external_port,
                   protocol,
                   description,
                   duration=3600):

    cmd = [
        upnpc_path, '-e', description, '-a', internal_client,
        str(internal_port),
        str(external_port), protocol,
        str(duration)
    ]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    AddUPnPMappingCheckResponse(internal_client, internal_port, external_port,
                                protocol, stdout, stderr)
示例#9
0
def GetExternalIP():

    if HydrusData.TimeHasPassed(EXTERNAL_IP['time'] + (3600 * 24)):

        cmd = [UPNPC_PATH, '-l']

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

        HydrusData.CheckProgramIsNotShuttingDown()

        try:

            p = subprocess.Popen(cmd,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 **sbp_kwargs)

        except FileNotFoundError:

            RaiseMissingUPnPcError('fetch external IP')

        HydrusData.WaitForProcessToFinish(p, 30)

        (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

        if stderr is not None and len(stderr) > 0:

            raise Exception(
                'Problem while trying to fetch External IP (if it says No IGD UPnP Device, you are either on a VPN or your router does not seem to support UPnP):'
                + os.linesep * 2 + str(stderr))

        else:

            try:

                lines = HydrusText.DeserialiseNewlinedTexts(stdout)

                i = lines.index(
                    'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
                )

                # ExternalIPAddress = ip
                (gumpf, external_ip_address) = lines[i - 1].split(' = ')

            except ValueError:

                raise Exception('Could not parse external IP!')

            if external_ip_address == '0.0.0.0':

                raise Exception(
                    'Your UPnP device returned your external IP as 0.0.0.0! Try rebooting it, or overwrite it in options!'
                )

            EXTERNAL_IP['ip'] = external_ip_address
            EXTERNAL_IP['time'] = HydrusData.GetNow()

    return EXTERNAL_IP['ip']
示例#10
0
    def initialize(self, start_index=0):

        self.close()

        if self._mime in (HC.IMAGE_APNG, HC.IMAGE_GIF):

            do_ss = False
            ss = 0
            self.pos = 0
            skip_frames = start_index

        else:

            if start_index == 0:

                do_ss = False

            else:

                do_ss = True

            ss = start_index / self.fps
            self.pos = start_index
            skip_frames = 0

        (w, h) = self._target_resolution

        cmd = [FFMPEG_PATH]

        if do_ss:

            cmd.extend(['-ss', "%.03f" % ss])

        cmd.extend([
            '-i', self._path, '-loglevel', 'quiet', '-f', 'image2pipe',
            "-pix_fmt", self.pix_fmt, "-s",
            str(w) + 'x' + str(h), '-vsync', '0', '-vcodec', 'rawvideo', '-'
        ])

        sbp_kwargs = HydrusData.GetSubprocessKWArgs()

        try:

            self.process = subprocess.Popen(cmd,
                                            bufsize=self.bufsize,
                                            stdin=subprocess.PIPE,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE,
                                            **sbp_kwargs)

        except FileNotFoundError as e:

            HydrusData.ShowText('Cannot render video--FFMPEG not found!')

            raise

        if skip_frames > 0:

            self.skip_frames(skip_frames)
def AddUPnPMapping(internal_client,
                   internal_port,
                   external_port,
                   protocol,
                   description,
                   duration=3600):

    cmd = [
        upnpc_path, '-e', description, '-a', internal_client,
        str(internal_port),
        str(external_port), protocol,
        str(duration)
    ]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if 'x.x.x.x:' + str(
            external_port
    ) + ' TCP is redirected to internal ' + internal_client + ':' + str(
            internal_port) in stdout:

        raise HydrusExceptions.FirewallException(
            'The UPnP mapping of ' + internal_client + ':' +
            str(internal_port) + '->external:' + str(external_port) +
            ' already exists as a port forward. If this UPnP mapping is automatic, please disable it.'
        )

    if stdout is not None and 'failed with code' in stdout:

        if 'UnknownError' in stdout:

            raise HydrusExceptions.FirewallException(
                'Problem while trying to add UPnP mapping:' + os.linesep * 2 +
                stdout)

        else:

            raise Exception('Problem while trying to add UPnP mapping:' +
                            os.linesep * 2 + stdout)

    if stderr is not None and len(stderr) > 0:

        raise Exception('Problem while trying to add UPnP mapping:' +
                        os.linesep * 2 + stderr)
示例#12
0
def GetExternalIP():

    if HydrusData.TimeHasPassed(EXTERNAL_IP['time'] + (3600 * 24)):

        cmd = [upnpc_path, '-l']

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             **sbp_kwargs)

        HydrusData.WaitForProcessToFinish(p, 30)

        (stdout, stderr) = p.communicate()

        if stderr is not None and len(stderr) > 0:

            raise Exception('Problem while trying to fetch External IP:' +
                            os.linesep * 2 + str(stderr))

        else:

            try:

                lines = HydrusText.DeserialiseNewlinedTexts(stdout)

                i = lines.index(
                    'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
                )

                # ExternalIPAddress = ip
                (gumpf, external_ip_address) = lines[i - 1].split(' = ')

            except ValueError:

                raise Exception('Could not parse external IP!')

            if external_ip_address == '0.0.0.0':

                raise Exception(
                    'Your UPnP device returned your external IP as 0.0.0.0! Try rebooting it, or overwrite it in options!'
                )

            EXTERNAL_IP['ip'] = external_ip_address
            EXTERNAL_IP['time'] = HydrusData.GetNow()

    return EXTERNAL_IP['ip']
示例#13
0
def RenderImageToPNGPath(path, temp_png_path):

    # -y to overwrite the temp path
    cmd = [FFMPEG_PATH, '-y', "-i", path, temp_png_path]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs()

    HydrusData.CheckProgramIsNotShuttingDown()

    try:

        process = subprocess.Popen(cmd,
                                   bufsize=10**5,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   **sbp_kwargs)

    except FileNotFoundError as e:

        global FFMPEG_MISSING_ERROR_PUBBED

        if not FFMPEG_MISSING_ERROR_PUBBED:

            message = 'FFMPEG, which hydrus uses to parse and render video, was not found! This may be due to it not being available on your system, or hydrus being unable to find it.'
            message += os.linesep * 2

            if HC.PLATFORM_WINDOWS:

                message += 'You are on Windows, so there should be a copy of ffmpeg.exe in your install_dir/bin folder. If not, please check if your anti-virus has removed it and restore it through a new install.'

            else:

                message += 'If you are certain that FFMPEG is installed on your OS and accessible in your PATH, please let hydrus_dev know, as this problem is likely due to an environment problem. You may be able to solve this problem immediately by putting a static build of the ffmpeg executable in your install_dir/bin folder.'

            message += os.linesep * 2
            message += 'You can check your current FFMPEG status through help->about.'

            HydrusData.ShowText(message)

            FFMPEG_MISSING_ERROR_PUBBED = True

        raise FileNotFoundError(
            'Cannot interact with video because FFMPEG not found--are you sure it is installed? Full error: '
            + str(e))

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(process)
示例#14
0
def RemoveUPnPMapping(external_port, protocol):

    cmd = [upnpc_path, '-d', str(external_port), protocol]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = p.communicate()

    if stderr is not None and len(stderr) > 0:
        raise Exception('Problem while trying to remove UPnP mapping:' +
                        os.linesep * 2 + stderr)
示例#15
0
    def do_it():

        if HC.PLATFORM_WINDOWS:

            cmd = ['explorer', '/select,', path]

        elif HC.PLATFORM_MACOS:

            cmd = ['open', '-R', path]

        elif HC.PLATFORM_LINUX:

            raise NotImplementedError('Linux cannot open file locations!')

        sbp_kwargs = HydrusData.GetSubprocessKWArgs(hide_terminal=False)

        process = subprocess.Popen(cmd, **sbp_kwargs)

        process.communicate()
示例#16
0
def AddUPnPMapping(internal_client,
                   internal_port,
                   external_port,
                   protocol,
                   description,
                   duration=3600):

    if UPNPC_IS_MISSING:

        RaiseMissingUPnPcError('add UPnP port forward')

    cmd = [
        UPNPC_PATH, '-e', description, '-a', internal_client,
        str(internal_port),
        str(external_port), protocol,
        str(duration)
    ]

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    try:

        p = subprocess.Popen(cmd,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE,
                             **sbp_kwargs)

    except FileNotFoundError:

        RaiseMissingUPnPcError('add UPnP port forward')

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    AddUPnPMappingCheckResponse(internal_client, internal_port, external_port,
                                protocol, stdout, stderr)
def GetUPnPMappings():

    cmd = [upnpc_path, '-l']

    sbp_kwargs = HydrusData.GetSubprocessKWArgs(text=True)

    HydrusData.CheckProgramIsNotShuttingDown()

    p = subprocess.Popen(cmd,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE,
                         **sbp_kwargs)

    HydrusData.WaitForProcessToFinish(p, 30)

    (stdout, stderr) = HydrusThreading.SubprocessCommunicate(p)

    if stderr is not None and len(stderr) > 0:

        raise Exception('Problem while trying to fetch UPnP mappings:' +
                        os.linesep * 2 + stderr)

    else:

        try:

            lines = HydrusText.DeserialiseNewlinedTexts(stdout)

            i = lines.index(
                'i protocol exPort->inAddr:inPort description remoteHost leaseTime'
            )

            data_lines = []

            i += 1

            while i < len(lines):

                if not lines[i][0] in (' ', '0', '1', '2', '3', '4', '5', '6',
                                       '7', '8', '9'):
                    break

                data_lines.append(lines[i])

                i += 1

            processed_data = []

            for line in data_lines:

                # 0 UDP 65533->192.168.0.197:65533 'Skype UDP at 192.168.0.197:65533 (2665)' '' 0

                while '  ' in line:

                    line = line.replace('  ', ' ')

                if line.startswith(' '):

                    (empty, number, protocol, mapping_data,
                     rest_of_line) = line.split(' ', 4)

                else:

                    (number, protocol, mapping_data,
                     rest_of_line) = line.split(' ', 3)

                (external_port,
                 rest_of_mapping_data) = mapping_data.split('->')

                external_port = int(external_port)

                if rest_of_mapping_data.count(':') == 1:

                    (internal_client,
                     internal_port) = rest_of_mapping_data.split(':')

                else:

                    parts = rest_of_mapping_data.split(':')

                    internal_port = parts.pop(-1)

                    internal_client = ':'.join(parts)

                internal_port = int(internal_port)

                (empty, description, space, remote_host,
                 rest_of_line) = rest_of_line.split('\'', 4)

                lease_time = int(rest_of_line[1:])

                processed_data.append(
                    (description, internal_client, internal_port,
                     external_port, protocol, lease_time))

            return processed_data

        except Exception as e:

            HydrusData.Print('UPnP problem:')
            HydrusData.Print(traceback.format_exc())
            HydrusData.Print('Full response follows:')
            HydrusData.Print(stdout)

            raise Exception('Problem while trying to parse UPnP mappings:' +
                            os.linesep * 2 + str(e))
示例#18
0
    def initialize(self, start_index=0):

        self.close()

        if self._mime in (HC.IMAGE_APNG, HC.IMAGE_GIF):

            do_ss = False
            ss = 0
            self.pos = 0
            skip_frames = start_index

        else:

            if start_index == 0:

                do_ss = False

            else:

                do_ss = True

            ss = start_index / self.fps
            self.pos = start_index
            skip_frames = 0

        do_fast_seek = True

        (w, h) = self._target_resolution

        cmd = [FFMPEG_PATH]

        if do_ss and do_fast_seek:  # fast seek

            cmd.extend(['-ss', "%.03f" % ss])

        cmd.extend(['-i', self._path])

        if do_ss and not do_fast_seek:  # slow seek

            cmd.extend(['-ss', "%.03f" % ss])

        if self._clip_rect is not None:

            (clip_x, clip_y, clip_width, clip_height) = self._clip_rect

            cmd.extend([
                '-vf', 'crop={}:{}:{}:{}'.format(clip_width, clip_height,
                                                 clip_x, clip_y)
            ])

        cmd.extend([
            '-loglevel', 'quiet', '-f', 'image2pipe', "-pix_fmt", self.pix_fmt,
            "-s",
            str(w) + 'x' + str(h), '-vsync', '0', '-vcodec', 'rawvideo', '-'
        ])

        sbp_kwargs = HydrusData.GetSubprocessKWArgs()

        HydrusData.CheckProgramIsNotShuttingDown()

        try:

            self.process = subprocess.Popen(cmd,
                                            bufsize=self.bufsize,
                                            stdin=subprocess.PIPE,
                                            stdout=subprocess.PIPE,
                                            stderr=subprocess.PIPE,
                                            **sbp_kwargs)

        except FileNotFoundError as e:

            HydrusData.ShowText('Cannot render video--FFMPEG not found!')

            raise

        if skip_frames > 0:

            self.skip_frames(skip_frames)
示例#19
0
    def do_it(launch_path):

        if HC.PLATFORM_WINDOWS and launch_path is None:

            os.startfile(path)

        else:

            if launch_path is None:

                launch_path = GetDefaultLaunchPath()

            complete_launch_path = launch_path.replace('%path%', path)

            hide_terminal = False

            if HC.PLATFORM_WINDOWS:

                cmd = complete_launch_path

                preexec_fn = None

            else:

                cmd = shlex.split(complete_launch_path)

                preexec_fn = getattr(os, 'setsid', None)

            if HG.subprocess_report_mode:

                message = 'Attempting to launch ' + path + ' using command ' + repr(
                    cmd) + '.'

                HydrusData.ShowText(message)

            try:

                sbp_kwargs = HydrusData.GetSubprocessKWArgs(
                    hide_terminal=hide_terminal, text=True)

                HydrusData.CheckProgramIsNotShuttingDown()

                process = subprocess.Popen(cmd,
                                           preexec_fn=preexec_fn,
                                           stdin=subprocess.PIPE,
                                           stdout=subprocess.PIPE,
                                           stderr=subprocess.PIPE,
                                           **sbp_kwargs)

                (stdout,
                 stderr) = HydrusThreading.SubprocessCommunicate(process)

                if HG.subprocess_report_mode:

                    if stdout is None and stderr is None:

                        HydrusData.ShowText('No stdout or stderr came back.')

                    if stdout is not None:

                        HydrusData.ShowText('stdout: ' + repr(stdout))

                    if stderr is not None:

                        HydrusData.ShowText('stderr: ' + repr(stderr))

            except Exception as e:

                HydrusData.ShowText(
                    'Could not launch a file! Command used was:' + os.linesep +
                    str(cmd))

                HydrusData.ShowException(e)
示例#20
0
def GetFFMPEGInfoLines( path, count_frames_manually = False, only_first_second = False ):
    
    # open the file in a pipe, provoke an error, read output
    
    cmd = [ FFMPEG_PATH, "-i", path ]
    
    if only_first_second:
        
        cmd.insert( 1, '-t' )
        cmd.insert( 2, '1' )
        
    
    if count_frames_manually:
        
        # added -an here to remove audio component, which was sometimes causing convert fails on single-frame music webms
        
        if HC.PLATFORM_WINDOWS:
            
            cmd += [ "-vf", "scale=-2:120", "-an", "-f", "null", "NUL" ]
            
        else:
            
            cmd += [ "-vf", "scale=-2:120", "-an", "-f", "null", "/dev/null" ]
            
        
    
    sbp_kwargs = HydrusData.GetSubprocessKWArgs()
    
    HydrusData.CheckProgramIsNotShuttingDown()
    
    try:
        
        process = subprocess.Popen( cmd, bufsize = 10**5, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, **sbp_kwargs )
        
    except FileNotFoundError as e:
        
        global FFMPEG_MISSING_ERROR_PUBBED
        
        if not FFMPEG_MISSING_ERROR_PUBBED:
            
            message = 'FFMPEG, which hydrus uses to parse and render video, was not found! This may be due to it not being available on your system, or hydrus being unable to find it.'
            message += os.linesep * 2
            
            if HC.PLATFORM_WINDOWS:
                
                message += 'You are on Windows, so there should be a copy of ffmpeg.exe in your install_dir/bin folder. If not, please check if your anti-virus has removed it and restore it through a new install.'
                
            else:
                
                message += 'If you are certain that FFMPEG is installed on your OS and accessible in your PATH, please let hydrus_dev know, as this problem is likely due to an environment problem. You may be able to solve this problem immediately by putting a static build of the ffmpeg executable in your install_dir/bin folder.'
                
            
            message += os.linesep * 2
            message += 'You can check your current FFMPEG status through help->about.'
            
            HydrusData.ShowText( message )
            
            FFMPEG_MISSING_ERROR_PUBBED = True
            
        
        raise FileNotFoundError( 'Cannot interact with video because FFMPEG not found--are you sure it is installed? Full error: ' + str( e ) )
        
    
    ( stdout, stderr ) = HydrusThreading.SubprocessCommunicate( process )
    
    data_bytes = stderr
    
    if len( data_bytes ) == 0:
        
        global FFMPEG_NO_CONTENT_ERROR_PUBBED
        
        if not FFMPEG_NO_CONTENT_ERROR_PUBBED:
            
            message = 'FFMPEG, which hydrus uses to parse and render video, did not return any data on a recent file metadata check! More debug info has been written to the log.'
            message += os.linesep * 2
            message += 'You can check this info again through help->about.'
            
            HydrusData.ShowText( message )
            
            message += os.linesep * 2
            message += str( sbp_kwargs )
            message += os.linesep * 2
            message += str( os.environ )
            message += os.linesep * 2
            message += 'STDOUT Response: {}'.format( stdout )
            message += os.linesep * 2
            message += 'STDERR Response: {}'.format( stderr )
            
            HydrusData.DebugPrint( message )
            
            FFMPEG_NO_CONTENT_ERROR_PUBBED = True
            
        
        raise HydrusExceptions.DataMissing( 'Cannot interact with video because FFMPEG did not return any content.' )
        
    
    del process
    
    ( text, encoding ) = HydrusText.NonFailingUnicodeDecode( data_bytes, 'utf-8' )
    
    lines = text.splitlines()
    
    CheckFFMPEGError( lines )
    
    return lines
示例#21
0
def GetFFMPEGVersion():
    
    cmd = [ FFMPEG_PATH, '-version' ]
    
    HydrusData.CheckProgramIsNotShuttingDown()
    
    try:
        
        sbp_kwargs = HydrusData.GetSubprocessKWArgs( text = True )
        
        process = subprocess.Popen( cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, **sbp_kwargs )
        
    except FileNotFoundError:
        
        return 'no ffmpeg found at path "{}"'.format( FFMPEG_PATH )
        
    except Exception as e:
        
        HydrusData.ShowException( e )
        
        return 'unable to execute ffmpeg at path "{}"'.format( FFMPEG_PATH )
        
    
    ( stdout, stderr ) = HydrusThreading.SubprocessCommunicate( process )
    
    del process
    
    lines = stdout.splitlines()
    
    if len( lines ) > 0:
        
        # typically 'ffmpeg version [VERSION] Copyright ...
        top_line = lines[0]
        
        if top_line.startswith( 'ffmpeg version ' ):
            
            top_line = top_line.replace( 'ffmpeg version ', '' )
            
            if ' ' in top_line:
                
                version_string = top_line.split( ' ' )[0]
                
                return version_string
                
            
        
    
    message = 'FFMPEG was recently contacted to fetch version information. While FFMPEG could be found, the response could not be understood. Significant debug information has been printed to the log, which hydrus_dev would be interested in.'
    
    HydrusData.ShowText( message )
    
    message += os.linesep * 2
    message += str( sbp_kwargs )
    message += os.linesep * 2
    message += str( os.environ )
    message += os.linesep * 2
    message += 'STDOUT Response: {}'.format( stdout )
    message += os.linesep * 2
    message += 'STDERR Response: {}'.format( stderr )
    
    HydrusData.Print( message )
    
    global FFMPEG_NO_CONTENT_ERROR_PUBBED
    
    FFMPEG_NO_CONTENT_ERROR_PUBBED = True
    
    return 'unknown'
def VideoHasAudio(path):

    info_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path)

    (audio_found, audio_format) = ParseFFMPEGAudio(info_lines)

    if not audio_found:

        return False

    # just because video metadata has an audio stream doesn't mean it has audio. some vids have silent audio streams lmao
    # so, let's read it as PCM and see if there is any noise
    # this obviously only works for single audio stream vids, we'll adapt this if someone discovers a multi-stream mkv with a silent channel that doesn't work here

    cmd = [HydrusVideoHandling.FFMPEG_PATH]

    # this is perhaps not sensible for eventual playback and I should rather go for wav file-like and feed into python 'wave' in order to maintain stereo/mono and so on and have easy chunk-reading

    cmd.extend(['-i', path, '-loglevel', 'quiet', '-f', 's16le', '-'])

    sbp_kwargs = HydrusData.GetSubprocessKWArgs()

    HydrusData.CheckProgramIsNotShuttingDown()

    try:

        process = subprocess.Popen(cmd,
                                   bufsize=65536,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   **sbp_kwargs)

    except FileNotFoundError as e:

        HydrusData.ShowText('Cannot render audio--FFMPEG not found!')

        raise

    # silent PCM data is just 00 bytes
    # every now and then, you'll get a couple ffs for some reason, but this is not legit audio data

    try:

        chunk_of_pcm_data = process.stdout.read(65536)

        while len(chunk_of_pcm_data) > 0:

            # iterating over bytes gives you ints, recall
            if True in (b != 0 and b != 255 for b in chunk_of_pcm_data):

                return True

            chunk_of_pcm_data = process.stdout.read(65536)

        return False

    finally:

        process.terminate()

        process.stdout.close()
        process.stderr.close()