You can try sending a test tensor through the encoding conv / pooling layers before declaring your linear layer, and check its output shape, then use that shape for calculating the flat size of the linear layer.
For example, it’s done like that here: https://github.com/yjlolo/vae-audio/blob/3a43e3122da55daf89fffaf5bbf11fdb805a597f/model/model.py#L180 19