Both these parts seem to be well described in their datasheets, so there really shouldn't be much question about what they do and how to use them.
The MID400 is specifically designed for detecting AC line voltage, but note that it requires 4 mA to turn on. That means at 240 VAC, a series resistor will dissipate over 950 mW. Yikes!
The PC817 isn't much better with a current transfer ratio of only 1/4. That's really poor, especially when speed is not a requirement.
I recently had to do this and used two FOD817D opto-isolators. The inputs were connected opposite in parallel and the outputs directly in parallel. Those have a minimum guaranteed CTR of 3, which makes them much easier to use in this application. They are also cheap and available. Two of these can often cost less than one fancier opto, especially if you are already stocking them.
In my case I needed the outputs to sink 2.2 mA. You can easily arrange for much less if the signal is only going into a microcontroller on the same board. Even so, I could use 120 kΩ series resistance for a worst case dissipation of 502 mW. For production and stocking reasons, I used 6x 20 kΩ resistors to make the 120 kΩ. That comes out to 56 V and 84 mW max per resistor. I could confidently spec the overall detector as being good for 90-240 VAC, 20-500 Hz.
The output will pulse at twice the line frequency. However, since this is going into a micro, that's no problem. In my case I used a 50 ms debounce time, although you could use less if speed is important and the lower frequency is known to be 50 Hz or just below.
Remember that speed of detecting AC is inherently limited by the cycle period. At 50 Hz, each cycle lasts 20 ms, and each half-cycle therefore 10 ms. At low voltages you may only get a short blip every 10 ms.