How to calculate segwit transaction fee in bytes?

I'm trying to calculate SegWit transaction fees in bytes, I have two Segwit inputs and 2 segwit outputs. can anyone calculate this transaction fees if I'm paying 100 Sat per byte?

That depends on what those inputs and outputs look like.

The formula, as specified in BIP141, is:

• Call base_size the number of bytes needed to serialize the transaction in legacy format (which does not include the witnesses).
• Call total_size the number of bytes needed to serialize the transaction including witnesses.
• The weight of the transaction equals 3*base_size + total_size
• The virtual size of the transaction (which is what feerates are usually expressed against) equals weight/4 or 0.75base_size + 0.25total_size.

Some reference numbers:

• Outputs:
• A P2PKH (1... address) output is 34 vbytes.
• A P2SH (3... address) output is 32 vbytes.
• A P2WPKH (bc1q... address of length 42) output is 31 vbytes.
• A P2WSH (bc1q... address of length 62) output is 43 vbytes.
• Inputs:
• A P2PKH spend with a compressed public key is 148 vbytes.
• A P2WPKH spend is 68 vbytes.
• A P2SH-P2WPKH spend is 91 vbytes.
• Thanks for your expensive explanation, if I have 2 P2WPKH inputs and 2 P2WPKH output, how can I estimate the fees for this transaction? also practically can we convert vbytes to legacy bytes? Commented Apr 25, 2019 at 0:38
• @Adam You can't convert vbyte to bytes as they're independent metrics. Thankfully, legacy bytes are irrelevant for fee calculations. If you have two 2 P2WPKH outputs and inputs, you'll have 10 + 2*31 + 2*101.25 = 274.5 vbytes. Whatever fee you're paying divided by 275 (rounding 274.5 up) gives you the fee/vbyte metric, which is what matters for miners. Commented Apr 25, 2019 at 0:42
• Thanks again for your explanation, as I see in this transaction on blockchain they says transaction size in bytes 191, do you know how did they calculate that? blockchain.com/btc/tx/… Commented Apr 25, 2019 at 0:51
• @Adam Stop looking at transaction size, as you've been told elsewhere. The relevant metric for feerate is either virtual size or weight. Commented Apr 25, 2019 at 0:53
• @Adam How many times do I need to repeat this? No, you cannot convert vbytes into bytes. They compute bytes by just counting the bytes. They count the vbytes by using the formula in my answer. Commented Apr 25, 2019 at 2:17