In this work, a mathematical model is presented for the irreversible Otto and Diesel cycles using finite time thermodynamics. The cycle is analyzed between two reservoirs with infinite thermal capacitances, where the processes of heat exchange occur in the heat exchangers between the working fluid and the thermal reservoirs at constant temperatures. The irreversibilities follow from the heat exchange processes occurring in finite time, the leakage of heat from the hot source to the cold source and the non-isentropic compression and expansion processes. The ecological optimization criterion represents the best compromise between power output of an engine and the environment that surrounds it. The results are presented through the power curves and ecological criteria, efficiency and ecological criteria and entropy generation rate and ecological criteria. Analysis is conducted to behavior of power, thermal efficiency and entropy generation rate ecologically optimized through which are evaluated the influences of some parameters on their behavior. Finally, maximum and ecological criteria are compared graphically. The analysis shows that the ecological optimizations present the best compromise between power and environment. The results can be used as an important criterion in developing projects of internal combustion engines.