Multiply ffprobe fps as value in powershell - powershell

I'm trying to print the multiplied fps of a video with ffprobe,
.\ffprobe.exe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=avg_frame_rate -i "$videopath" > rate 2>&1
$script:rate = [IO.File]::ReadAllText(".\rate")
$script:framerate = ($rate/2)
echo "Multiplied = "$framerate
What this is supposed to do is to write the fps into a file, which ffprobe does as a fraction (1/10 in this case), and then multiply it by 2.
Since the variable is read as literal text I can't multiply it by 2, and instead it just gives me this.
Multiplied =
1/10
1/10
Is there any way to make it print Multiplied = 20 instead

Powershell doesn't understand fractions, so you'll need to convert the string into numbers and then do the math.
$rate = "1/10";
# convert the string into numbers
$parts = $rate.Split("/"); # #( "1", "10")
$numerator = [int] $parts[0]; # 1
$denominator = [int] $parts[1]; # 10
# do the math
$framerate = $denominator * 2; # 20
write-host "Multiplied = $framerate"
# Multiplied = 20

Related

Calculate two audio duration with frame

I am stuck, I need a small logic.
I have two audio durations
x = "00:00:07:18"
y = "00:00:06:00" H : M: S: F
Answer should be x + y = 00:00:13:18
H=hours S= seconds M=minutes F=frame
My question is
if x = "00:00:03:14"
y = "00:00:13:18"
answer should be x + y = **00:00:17:02**
If the frame is greater than 30 it should increase 1 in second.
I am using power shell. How can I determine the logic to calculate both of this?
Calculation of the first 3 parts (hour:minute:second), we can offload to the [timespan] type, then all we need to worry about is carrying excess frames over:
# Simple helper function to turn our input strings into a [timespan] + frame count
function Parse-FrameDuration
{
param(
[string]$Duration
)
$hour,$minute,$second,$frame = ($Duration -split ':') -as [int[]]
[PSCustomObject]#{
Time = New-TimeSpan -Hours $hour -Minutes $minute -Seconds $second
Frame = $frame
}
}
# function to do the actual calculation
function Add-Frame
{
param(
[string]$Base,
[string]$Offset
)
# Parse our two timestamps
$a = Parse-FrameDuration $Base
$b = Parse-FrameDuration $Offset
# Calculate frames % frame rate, remember to carry any excess seconds
$frames = 0
$carry = [math]::DivRem($a.Frame + $b.Frame , 30, [ref]$frames)
# Calculate time difference, add any extra second carried from frame count
$new = ($a.Time + $b.Time).Add($(New-TimeSpan -Seconds $carry))
# Stitch output string together from new timespan + remaining frames
return "{0:hh\:mm\:ss}:{1:00}" -f $new,$frames
}
Now we can do:
PS C:\> Add-Frame -Base 00:00:03:14 -Offset 00:00:13:18
00:00:17:02

How to isolate leftmost bytes in integer

This has to be done in Perl:
I have integers on the order of e.g. 30_146_890_129 and 17_181_116_691 and 21_478_705_663.
These are supposedly made up of 6 bytes, where:
bytes 0-1 : value a
bytes 2-3 : value b
bytes 4-5 : value c
I want to isolate what value a is. How can I do this in Perl?
I've tried using the >> operator:
perl -e '$a = 330971351478 >> 16; print "$a\n";'
5050222
perl -e '$a = 17181116691 >> 16; print "$a\n";'
262163
But these numbers are not on the order of what I am expecting, more like 0-1000.
Bonus if I can also get values b and c but I don't really need those.
Thanks!
number >> 16 returns number shifted by 16 bit and not the shifted bits as you seem to assume. To get the last 16 bit you might for example use number % 2**16 or number & 0xffff. To get to b and c you can just shift before getting the last 16 bits, i.e.
$a = $number & 0xffff;
$b = ($number >> 16) & 0xffff;
$c = ($number >> 32) & 0xffff;
If you have 6 bytes, you don't need to convert them to a number first. You can use one the following depending on the order of the bytes: (Uppercase represents the most significant byte.)
my ($num_c, $num_b, $num_a) = unpack('nnn', "\xCC\xcc\xBB\xbb\xAA\xaa");
my ($num_a, $num_b, $num_c) = unpack('nnn', "\xAA\xaa\xBB\xbb\xAA\xaa");
my ($num_c, $num_b, $num_a) = unpack('vvv', "\xcc\xCC\xbb\xBB\xaa\xAA");
my ($num_a, $num_b, $num_c) = unpack('vvv', "\xaa\xAA\xbb\xBB\xcc\xCC");
If you are indeed provided with a number 0xCCccBBbbAAaa), you can convert it to bytes then extract the numbers you want from it as follows:
my ($num_c, $num_b, $num_a) = unpack('xxnnn', pack('Q>', $num));
Alternatively, you could also use an arithmetic approach like you attempted.
my $num_a = $num & 0xFFFF;
my $num_b = ( $num >> 16 ) & 0xFFFF;
my $num_c = $num >> 32;
While the previous two solutions required a Perl built to use 64-bit integers, the following will work with any build of Perl:
my $num_a = $num % 2**16;
my $num_b = ( $num / 2**16 ) % 2**16;
my $num_c = int( $num / 2**32 );
Let's look at ( $num >> 16 ) & 0xFFFF in detail.
Original number: 0x0000CCccBBbbAAaa
After shifting: 0x00000000CCccBBbb
After masking: 0x000000000000BBbb

Convert hex string to a base36 string

In AWS each instance has an ID that looks something like this: i-1234567890abcdef ; the last 16 characters being a hexadecimal number. I would like to treat the "1234567890abcdef" part as a hex number and convert it to base36, so a-z0-9. This way I can use is as the computer's name and not go over the 15 character limit. How is that done in Powershell ?
Converting the input from hex is easy enough: skip the first two characters, and convert to UInt64:
[convert]::ToUInt64($text.Substring(2), 16)
but PowerShell (.Net) has no built-in way to convert to base 36. You'll need to implement it yourself, e.g. this code taken from https://ss64.com/ps/syntax-base36.html and adjusted for larger numbers:
function convertTo-Base36
{
[CmdletBinding()]
param (
[parameter(valuefrompipeline=$true, HelpMessage="Integer number to convert")]
[uint64]$DecimalNumber=""
)
$alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
do
{
$remainder = ($DecimalNumber % 36)
$char = $alphabet.Substring($remainder, 1)
$base36Num = "$char$base36Num"
$DecimalNumber = ($DecimalNumber - $remainder) / 36
}
while ($DecimalNumber -gt 0)
$base36Num
}
Then:
$x='i-1234567890abcdef'
$hexPart = $x.Substring(2)
$decimal = [convert]::ToUInt64($hexPart, 16)
convertTo-Base36 $decimal
# -> 9YS742MX86WF
or:
[convert]::ToUInt64('i-1234567890abcdef'.Substring(2), 16) | Convertto-Base36

Calculate result based on percentage in Powershell

I want to calculate the following in Powershell
$x = 100
$y = 25
result = x * y%
Should be really simple, but I can't seem to come up with the right way to calculate the correct result in Powershell
Divide by 100 to get the percentage:
$x = 100
$y = 25
$result = $x * ($y / 100)

Perl function for negative integers using the 2's complement

I am trying to convert AD maxpwdAge (a 64-bit integer) into a number of days.
According to Microsoft:
Uses the IADs interface's Get method to retrieve the value of the domain's maxPwdAge attribute (line 5).
Notice we use the Set keyword in VBScript to initialize the variable named objMaxPwdAge—the variable used to store the value returned by Get. Why is that?
When you fetch a 64-bit large integer, ADSI does not return one giant scalar value. Instead, ADSI automatically returns an IADsLargeInteger object. You use the IADsLargeInteger interface's HighPart and LowPart properties to calculate the large integer's value. As you may have guessed, HighPart gets the high order 32 bits, and LowPart gets the low order 32 bits. You use the following formula to convert HighPart and LowPart to the large integer's value.
The existing code in VBScript from the same page:
Const ONE_HUNDRED_NANOSECOND = .000000100 ' .000000100 is equal to 10^-7
Const SECONDS_IN_DAY = 86400
Set objDomain = GetObject("LDAP://DC=fabrikam,DC=com") ' LINE 4
Set objMaxPwdAge = objDomain.Get("maxPwdAge") ' LINE 5
If objMaxPwdAge.LowPart = 0 Then
WScript.Echo "The Maximum Password Age is set to 0 in the " & _
"domain. Therefore, the password does not expire."
WScript.Quit
Else
dblMaxPwdNano = Abs(objMaxPwdAge.HighPart * 2^32 + objMaxPwdAge.LowPart)
dblMaxPwdSecs = dblMaxPwdNano * ONE_HUNDRED_NANOSECOND ' LINE 13
dblMaxPwdDays = Int(dblMaxPwdSecs / SECONDS_IN_DAY) ' LINE 14
WScript.Echo "Maximum password age: " & dblMaxPwdDays & " days"
End If
How can I do this in Perl?
Endianness may come into this, but you may be able to say
#!/usr/bin/perl
use strict;
use warnings;
my $num = -37_108_517_437_440;
my $binary = sprintf "%064b", $num;
my ($high, $low) = $binary =~ /(.{32})(.{32})/;
$high = oct "0b$high";
$low = oct "0b$low";
my $together = unpack "q", pack "LL", $low, $high;
print "num $num, low $low, high $high, together $together\n";
Am I missing something? As far as I can tell from your question, your problem has nothing at all to do with 2’s complement. As far as I can tell, all you need/want to do is
use Math::BigInt;
use constant MAXPWDAGE_UNIT_PER_SEC => (
1000 # milliseconds
* 1000 # microseconds
* 10 # 100 nanoseconds
);
use constant SECS_PER_DAY => (
24 # hours
* 60 # minutes
* 60 # seconds
);
my $maxpwdage_full = ( Math::BigInt->new( $maxpwdage_highpart ) << 32 ) + $maxpwdage_lowpart;
my $days = $maxpwdage_full / MAXPWDAGE_UNIT_PER_SEC / SECS_PER_DAY;
Note that I deliberately use 2 separate constants, and I divide by them in sequence, because that keeps the divisors smaller than the range of a 32-bit integer. If you want to write this another way and you want it to work correctly on 32-bit perls, you’ll have to keep all the precision issues in mind.