Perturbative treatments of the lattice dynamics are widely successful for many crystalline materials, their applicability is, however, limited for strongly anharmonic systems, metastable crystal structures and liquids. The full dynamics of these systems can, however, be accessed via molecular dynamics (MD) simulations using correlation functions, which includes dynamical structure factors providing a direct bridge to experiment. To simplify the analysis of correlation functions, here the dynasor package is presented as a flexible and efficient tool that enables the calculation of static and dynamical structure factors, current correlation functions as well as their partial counterparts from MD trajectories. The dynasor code can handle input from several major open source MD packages and thanks to its C/Python structure can be readily extended to support additional codes. The utility of dynasor is demonstrated via examples for both solid and liquid single and multi-component systems. In particular, the possibility to extract the full temperature dependence of phonon frequencies and lifetimes is emphasized.