Background Idiopathic pulmonary fibrosis is a progressive and fatal lung disease with inevitable loss of lung function. The CAPACITY programme (studies 004 and 006) was designed to confirm the results of a phase 2 study that suggested that pirfenidone, a novel antifibrotic and anti-inflammatory drug, reduces deterioration in lung function in patients with idiopathic pulmonary fibrosis. Methods In two concurrent trials (004 and 006), patients (aged 40–80 years) with idiopathic pulmonary fibrosis were randomly assigned to oral pirfenidone or placebo for a minimum of 72 weeks in 110 centres in Australia, Europe, and North America. In study 004, patients were assigned in a 2:1:2 ratio to pirfenidone 2403 mg/day, pirfenidone 1197 mg/day, or placebo; in study 006, patients were assigned in a 1:1 ratio to pirfenidone 2403 mg/day or placebo. The randomisation code (permuted block design) was computer generated and stratified by region. All study personnel were masked to treatment group assignment until after final database lock. Treatments were administered orally, 801 mg or 399 mg three times a day. The primary endpoint was change in percentage predicted forced vital capacity (FVC) at week 72. Analysis was by intention to treat. The studies are registered with ClinicalTrials.gov, numbers NCT00287729 and NCT00287716. Findings In study 004, 174 of 435 patients were assigned to pirfenidone 2403 mg/day, 87 to pirfenidone 1197 mg/day, and 174 to placebo. In study 006, 171 of 344 patients were assigned to pirfenidone 2403 mg/day, and 173 to placebo. All patients in both studies were analysed. In study 004, pirfenidone reduced decline in FVC (p=0·001). Mean FVC change at week 72 was −8·0% (SD 16·5) in the pirfenidone 2403 mg/day group and −12·4% (18·5) in the placebo group (difference 4·4%, 95% CI 0·7 to 9·1); 35 (20%) of 174 versus 60 (35%) of 174 patients, respectively, had a decline of at least 10%. A significant treatment effect was noted at all timepoints from week 24 and in an analysis over all study timepoints (p=0·0007). Mean change in percentage FVC in the pirfenidone 1197 mg/day group was intermediate to that in the pirfenidone 2403 mg/day and placebo groups. In study 006, the difference between groups in FVC change at week 72 was not significant (p=0·501). Mean change in FVC at week 72 was −9·0% (SD 19·6) in the pirfenidone group and −9·6% (19·1) in the placebo group, and the difference between groups in predicted FVC change at week 72 was not significant (0·6%, −3·5 to 4·7); however, a consistent pirfenidone effect was apparent until week 48 (p=0·005) and in an analysis of all study timepoints (p=0·007). Patients in the pirfenidone 2403 mg/day group had higher incidences of nausea (125 [36%] of 345 vs 60 [17%] of 347), dyspepsia (66 [19%] vs 26 [7%]), vomiting (47 [14%] vs 15 [4%]), anorexia (37 [11%] vs 13 [4%]), photosensitivity (42 [12%] vs 6 [2%]), rash (111 [32%] vs 40 [12%]), and dizziness (63 [18%] vs 35 [10%]) than did those in the placebo group. Fewer overall deaths (19 [6%] vs 29 [8%]) and fewer deaths related to idiopathic pulmonary fibrosis (12 [3%] vs 25 [7%]) occurred in the pirfenidone 2403 mg/day groups than in the placebo groups. Interpretation The data show pirfenidone has a favourable benefit risk profile and represents an appropriate treatment option for patients with idiopathic pulmonary fibrosis. Funding InterMune.